pagespeed cleanup
This commit is contained in:
1 parent
16aec18c28
commit
df707ab194
8 files changed
+106
-127
No files matched your search
@@ -1,21 +1,14 @@
|
||||
import { monitorSchemaPageSpeed } from "@/validation/zod";
|
||||
import { useGet, usePatch } from "@/hooks/UseApi";
|
||||
import { PageSpeedForm } from "@/pages/pagespeed/PageSpeedForm";
|
||||
|
||||
import { useParams } from "react-router";
|
||||
import type { ApiResponse } from "@/types/api";
|
||||
import humanInterval from "human-interval";
|
||||
import { z } from "zod";
|
||||
import { useNavigate } from "react-router";
|
||||
import type { INotificationChannel } from "@/types/notification-channel";
|
||||
import type { IMonitor } from "@/types/monitor";
|
||||
import type { SubmitValues } from "@/pages/pagespeed/PageSpeedForm";
|
||||
|
||||
const PageSpeedConfigurePage = () => {
|
||||
type FormValues = z.infer<typeof monitorSchemaPageSpeed>;
|
||||
type SubmitValues = Omit<FormValues, "interval"> & {
|
||||
interval: number | undefined;
|
||||
};
|
||||
|
||||
const { id } = useParams();
|
||||
const navigate = useNavigate();
|
||||
const { response } = useGet<ApiResponse<INotificationChannel[]>>(
|
||||
@@ -29,16 +22,12 @@ const PageSpeedConfigurePage = () => {
|
||||
|
||||
const { patch, loading, error } = usePatch<Partial<SubmitValues>, IMonitor>();
|
||||
|
||||
const onSubmit = async (data: FormValues) => {
|
||||
let interval = humanInterval(data.interval);
|
||||
if (!interval) interval = 180000;
|
||||
const submitData = {
|
||||
type: data.type,
|
||||
name: data.name,
|
||||
notificationChannels: data.notificationChannels,
|
||||
interval,
|
||||
};
|
||||
const result = await patch(`/monitors/${id}`, submitData);
|
||||
if (!monitor) {
|
||||
return null;
|
||||
}
|
||||
|
||||
const onSubmit = async (data: SubmitValues) => {
|
||||
const result = await patch(`/monitors/${id}`, data);
|
||||
if (result) {
|
||||
navigate(`/pagespeed/${id}`);
|
||||
} else {
|
||||
|
||||
@@ -1,28 +1,19 @@
|
||||
import { PageSpeedForm } from "@/pages/pagespeed/PageSpeedForm";
|
||||
|
||||
import humanInterval from "human-interval";
|
||||
import { z } from "zod";
|
||||
import { monitorSchema } from "@/validation";
|
||||
import { useGet, usePost } from "@/hooks/UseApi";
|
||||
import type { ApiResponse } from "@/types/api";
|
||||
import { useNavigate } from "react-router";
|
||||
import type { INotificationChannel } from "@/types/notification-channel";
|
||||
import type { SubmitValues } from "@/pages/pagespeed/PageSpeedForm";
|
||||
const UptimeCreatePage = () => {
|
||||
type FormValues = z.infer<typeof monitorSchema>;
|
||||
type SubmitValues = Omit<FormValues, "interval"> & {
|
||||
interval: number | undefined;
|
||||
};
|
||||
const navigate = useNavigate();
|
||||
const { response } = useGet<ApiResponse<INotificationChannel[]>>(
|
||||
"/notification-channels"
|
||||
);
|
||||
const { post, loading, error } = usePost<SubmitValues>();
|
||||
|
||||
const onSubmit = async (data: FormValues) => {
|
||||
let interval = humanInterval(data.interval);
|
||||
if (!interval) interval = 60000;
|
||||
const submitData = { ...data, interval };
|
||||
const result = await post("/monitors", submitData);
|
||||
const onSubmit = async (data: SubmitValues) => {
|
||||
const result = await post("/monitors", data);
|
||||
if (result) {
|
||||
navigate("/pagespeed");
|
||||
} else {
|
||||
|
||||
@@ -13,7 +13,7 @@ import { Typography } from "@mui/material";
|
||||
|
||||
import { useEffect, useState, useRef } from "react";
|
||||
import { useTranslation } from "react-i18next";
|
||||
import { monitorSchemaPageSpeed } from "@/validation/zod";
|
||||
import { monitorSchemaPageSpeed } from "@/validation";
|
||||
import { z } from "zod";
|
||||
import { zodResolver } from "@hookform/resolvers/zod";
|
||||
import {
|
||||
@@ -24,8 +24,15 @@ import {
|
||||
} from "react-hook-form";
|
||||
import { useTheme } from "@mui/material/styles";
|
||||
import { useInitForm } from "@/hooks/forms/UseInitPageSpeedForm";
|
||||
import type { IMonitor } from "@/types/monitor";
|
||||
|
||||
type FormValues = z.infer<typeof monitorSchemaPageSpeed>;
|
||||
export type FormValues = z.infer<typeof monitorSchemaPageSpeed>;
|
||||
export type SubmitValues = Omit<
|
||||
z.infer<typeof monitorSchemaPageSpeed>,
|
||||
"interval"
|
||||
> & {
|
||||
interval: number | undefined;
|
||||
};
|
||||
|
||||
export const PageSpeedForm = ({
|
||||
mode = "create",
|
||||
@@ -35,14 +42,14 @@ export const PageSpeedForm = ({
|
||||
loading,
|
||||
}: {
|
||||
mode?: "create" | "configure";
|
||||
initialData?: Partial<FormValues>;
|
||||
onSubmit: SubmitHandler<FormValues>;
|
||||
initialData?: IMonitor;
|
||||
onSubmit: SubmitHandler<SubmitValues>;
|
||||
notificationOptions: any[];
|
||||
loading: boolean;
|
||||
}) => {
|
||||
const { t } = useTranslation();
|
||||
const theme = useTheme();
|
||||
const { defaults } = useInitForm({ initialData: initialData });
|
||||
const { defaults, formToApi } = useInitForm({ initialData: initialData });
|
||||
const urlInputRef = useRef<HTMLInputElement>(null);
|
||||
const [selectedProtocol, setSelectedProtocol] = useState<string>("https");
|
||||
|
||||
@@ -92,13 +99,13 @@ export const PageSpeedForm = ({
|
||||
}
|
||||
}, [selectedProtocol]);
|
||||
|
||||
const notificationChannels = useWatch({
|
||||
control,
|
||||
name: "notificationChannels",
|
||||
});
|
||||
const notificationChannels =
|
||||
useWatch({ control, name: "notificationChannels" }) || [];
|
||||
|
||||
const submitForm = (data: FormValues) => onSubmit(formToApi(data) as any);
|
||||
|
||||
return (
|
||||
<BasePage component={"form"} onSubmit={handleSubmit(onSubmit)}>
|
||||
<BasePage component={"form"} onSubmit={handleSubmit(submitForm)}>
|
||||
<ConfigBox
|
||||
title={t("monitors.common.form.general.title")}
|
||||
subtitle={t("monitors.pageSpeed.form.general.description")}
|
||||
|
||||
Reference in new issue
Block a user