refactor uptime forms'
This commit is contained in:
1 parent
4ed89ace23
commit
16aec18c28
9 files changed
+137
-110
No files matched your search
@@ -2,7 +2,7 @@ import { PageSpeedForm } from "@/pages/pagespeed/PageSpeedForm";
|
||||
|
||||
import humanInterval from "human-interval";
|
||||
import { z } from "zod";
|
||||
import { monitorSchema } from "@/validation/zod";
|
||||
import { monitorSchema } from "@/validation";
|
||||
import { useGet, usePost } from "@/hooks/UseApi";
|
||||
import type { ApiResponse } from "@/types/api";
|
||||
import { useNavigate } from "react-router";
|
||||
|
||||
@@ -1,21 +1,14 @@
|
||||
import { monitorSchema } from "@/validation/zod";
|
||||
import { useGet, usePatch } from "@/hooks/UseApi";
|
||||
import { UptimeForm } from "@/pages/uptime/UptimeForm";
|
||||
|
||||
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/uptime/UptimeForm";
|
||||
|
||||
const UptimeConfigurePage = () => {
|
||||
type FormValues = z.infer<typeof monitorSchema>;
|
||||
type SubmitValues = Omit<FormValues, "interval"> & {
|
||||
interval: number | undefined;
|
||||
};
|
||||
|
||||
const { id } = useParams();
|
||||
const navigate = useNavigate();
|
||||
const { response } = useGet<ApiResponse<INotificationChannel[]>>(
|
||||
@@ -24,23 +17,17 @@ const UptimeConfigurePage = () => {
|
||||
const { response: monitorResponse } = useGet<ApiResponse<IMonitor>>(
|
||||
`/monitors/${id}`
|
||||
);
|
||||
const monitor = monitorResponse?.data || {};
|
||||
const monitor = monitorResponse?.data;
|
||||
const notificationOptions = response?.data ?? [];
|
||||
|
||||
const { patch, loading, error } = usePatch<Partial<SubmitValues>, IMonitor>();
|
||||
|
||||
const onSubmit = async (data: FormValues) => {
|
||||
let interval = humanInterval(data.interval);
|
||||
if (!interval) interval = 60000;
|
||||
const submitData = {
|
||||
type: data.type,
|
||||
name: data.name,
|
||||
n: data.n,
|
||||
notificationChannels: data.notificationChannels,
|
||||
interval,
|
||||
rejectUnauthorized: data.rejectUnauthorized,
|
||||
};
|
||||
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(`/uptime/${id}`);
|
||||
} else {
|
||||
@@ -50,9 +37,7 @@ const UptimeConfigurePage = () => {
|
||||
return (
|
||||
<UptimeForm
|
||||
mode="configure"
|
||||
initialData={{
|
||||
...monitor,
|
||||
}}
|
||||
initialData={monitor}
|
||||
onSubmit={onSubmit}
|
||||
notificationOptions={notificationOptions}
|
||||
loading={loading}
|
||||
|
||||
@@ -1,28 +1,19 @@
|
||||
import { UptimeForm } from "./UptimeForm";
|
||||
|
||||
import humanInterval from "human-interval";
|
||||
import { z } from "zod";
|
||||
import { monitorSchema } from "@/validation/zod";
|
||||
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/uptime/UptimeForm";
|
||||
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("/uptime");
|
||||
} else {
|
||||
|
||||
@@ -14,7 +14,7 @@ import { Typography } from "@mui/material";
|
||||
|
||||
import { useEffect, useRef } from "react";
|
||||
import { useTranslation } from "react-i18next";
|
||||
import { monitorSchema } from "@/validation/zod";
|
||||
import { monitorSchema } from "@/validation";
|
||||
import { z } from "zod";
|
||||
import { zodResolver } from "@hookform/resolvers/zod";
|
||||
import {
|
||||
@@ -23,10 +23,14 @@ import {
|
||||
useWatch,
|
||||
type SubmitHandler,
|
||||
} from "react-hook-form";
|
||||
import type { IMonitor } from "@/types/monitor";
|
||||
import { useTheme } from "@mui/material/styles";
|
||||
import { useInitForm } from "@/hooks/forms/UseInitMonitorFrom";
|
||||
|
||||
type FormValues = z.infer<typeof monitorSchema>;
|
||||
export type FormValues = z.infer<typeof monitorSchema>;
|
||||
export type SubmitValues = Omit<z.infer<typeof monitorSchema>, "interval"> & {
|
||||
interval: number | undefined;
|
||||
};
|
||||
|
||||
export const UptimeForm = ({
|
||||
mode = "create",
|
||||
@@ -36,14 +40,14 @@ export const UptimeForm = ({
|
||||
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 {
|
||||
@@ -73,6 +77,10 @@ export const UptimeForm = ({
|
||||
name: "notificationChannels",
|
||||
});
|
||||
|
||||
const submitForm = (formData: FormValues) => {
|
||||
onSubmit(formToApi(formData));
|
||||
};
|
||||
|
||||
useEffect(() => {
|
||||
if (!selectedType) return;
|
||||
|
||||
@@ -108,7 +116,7 @@ export const UptimeForm = ({
|
||||
}, [selectedType]);
|
||||
|
||||
return (
|
||||
<BasePage component={"form"} onSubmit={handleSubmit(onSubmit)}>
|
||||
<BasePage component={"form"} onSubmit={handleSubmit(submitForm)}>
|
||||
{mode === "create" && (
|
||||
<ConfigBox
|
||||
title={t("monitors.uptime.form.type.title")}
|
||||
|
||||
Reference in new issue
Block a user