infra refactor
This commit is contained in:
1 parent
df707ab194
commit
4dfeb71a9d
14 files changed
+195
-187
No files matched your search
@@ -1,21 +1,15 @@
|
||||
import { monitorSchemaInfra } from "@/validation/zod";
|
||||
import { monitorSchemaInfra } from "@/validation";
|
||||
import { useGet, usePatch } from "@/hooks/UseApi";
|
||||
import { InfraForm } from "@/pages/infrastructure/InfraForm";
|
||||
|
||||
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/infrastructure/InfraForm";
|
||||
|
||||
const InfraConfigurePage = () => {
|
||||
type FormValues = z.infer<typeof monitorSchemaInfra>;
|
||||
type SubmitValues = Omit<FormValues, "interval"> & {
|
||||
interval: number | undefined;
|
||||
};
|
||||
|
||||
const { id } = useParams();
|
||||
const navigate = useNavigate();
|
||||
const { response } = useGet<ApiResponse<INotificationChannel[]>>(
|
||||
@@ -29,20 +23,12 @@ const InfraConfigurePage = () => {
|
||||
|
||||
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,
|
||||
secret: data.secret,
|
||||
name: data.name,
|
||||
notificationChannels: data.notificationChannels,
|
||||
thresholds: data.thresholds,
|
||||
interval,
|
||||
};
|
||||
if (!monitor) {
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
const result = await patch(`/monitors/${id}`, submitData);
|
||||
const onSubmit = async (data: SubmitValues) => {
|
||||
const result = await patch(`/monitors/${id}`, data);
|
||||
if (result) {
|
||||
navigate(`/infrastructure/${id}`);
|
||||
} else {
|
||||
|
||||
@@ -1,27 +1,19 @@
|
||||
import { InfraForm } from "@/pages/infrastructure/InfraForm";
|
||||
import humanInterval from "human-interval";
|
||||
import { useNavigate } from "react-router";
|
||||
import { z } from "zod";
|
||||
import { monitorSchemaInfra } from "@/validation/zod";
|
||||
import { useGet, usePost } from "@/hooks/UseApi";
|
||||
import type { ApiResponse } from "@/types/api";
|
||||
import type { INotificationChannel } from "@/types/notification-channel";
|
||||
import type { SubmitValues } from "@/pages/infrastructure/InfraForm";
|
||||
|
||||
const InfraCreatePage = () => {
|
||||
type FormValues = z.infer<typeof monitorSchemaInfra>;
|
||||
type SubmitValues = Omit<FormValues, "interval"> & {
|
||||
interval: number | undefined;
|
||||
};
|
||||
const { response } = useGet<ApiResponse<INotificationChannel[]>>(
|
||||
"/notification-channels"
|
||||
);
|
||||
const { post, loading, error } = usePost<SubmitValues, any>();
|
||||
const navigate = useNavigate();
|
||||
|
||||
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("/infrastructure");
|
||||
} else {
|
||||
|
||||
@@ -13,7 +13,7 @@ import { Typography } from "@mui/material";
|
||||
|
||||
import { useEffect, useState, useRef } from "react";
|
||||
import { useTranslation } from "react-i18next";
|
||||
import { monitorSchemaInfra } from "@/validation/zod";
|
||||
import { monitorSchemaInfra } from "@/validation";
|
||||
import { z } from "zod";
|
||||
import { zodResolver } from "@hookform/resolvers/zod";
|
||||
import {
|
||||
@@ -24,8 +24,12 @@ import {
|
||||
} from "react-hook-form";
|
||||
import { useTheme } from "@mui/material/styles";
|
||||
import { useInitForm } from "@/hooks/forms/UseInitInfraForm";
|
||||
import type { IMonitor } from "@/types/monitor";
|
||||
|
||||
type FormValues = z.infer<typeof monitorSchemaInfra>;
|
||||
export type FormValues = z.infer<typeof monitorSchemaInfra>;
|
||||
export type SubmitValues = Omit<FormValues, "interval"> & {
|
||||
interval: number | undefined;
|
||||
};
|
||||
|
||||
export const InfraForm = ({
|
||||
mode = "create",
|
||||
@@ -35,14 +39,14 @@ export const InfraForm = ({
|
||||
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");
|
||||
|
||||
@@ -97,8 +101,12 @@ export const InfraForm = ({
|
||||
name: "notificationChannels",
|
||||
});
|
||||
|
||||
const submitForm = (formData: FormValues) => {
|
||||
onSubmit(formToApi(formData));
|
||||
};
|
||||
|
||||
return (
|
||||
<BasePage component={"form"} onSubmit={handleSubmit(onSubmit)}>
|
||||
<BasePage component={"form"} onSubmit={handleSubmit(submitForm)}>
|
||||
<ConfigBox
|
||||
title={t("monitors.common.form.general.title")}
|
||||
subtitle={t(`monitors.infrastructure.form.general.description`)}
|
||||
@@ -303,7 +311,7 @@ export const InfraForm = ({
|
||||
)}
|
||||
/>
|
||||
<Stack gap={theme.spacing(2)} mt={theme.spacing(2)}>
|
||||
{notificationChannels.map((notificationId) => {
|
||||
{notificationChannels.map((notificationId: string) => {
|
||||
const option = notificationOptions.find(
|
||||
(o: any) => o._id === notificationId
|
||||
);
|
||||
@@ -321,7 +329,7 @@ export const InfraForm = ({
|
||||
strokeWidth={1.5}
|
||||
onClick={() => {
|
||||
const updated = notificationChannels.filter(
|
||||
(id) => id !== notificationId
|
||||
(id: string) => id !== notificationId
|
||||
);
|
||||
setValue("notificationChannels", updated);
|
||||
}}
|
||||
|
||||
@@ -24,7 +24,7 @@ import { useLimitReached } from "@/hooks/UsePlanEntitlements";
|
||||
|
||||
const GLOBAL_REFRESH = config.GLOBAL_REFRESH;
|
||||
|
||||
const PageSpeedMonitorsPage = () => {
|
||||
const InfraMonitorsPage = () => {
|
||||
const theme = useTheme();
|
||||
const { t } = useTranslation();
|
||||
const isSmall = useMediaQuery(theme.breakpoints.down("md"));
|
||||
@@ -131,4 +131,4 @@ const PageSpeedMonitorsPage = () => {
|
||||
);
|
||||
};
|
||||
|
||||
export default PageSpeedMonitorsPage;
|
||||
export default InfraMonitorsPage;
|
||||
@@ -104,7 +104,6 @@ export const UptimeForm = ({
|
||||
} else {
|
||||
setValue("url", `${selectedType}://${url}`);
|
||||
}
|
||||
// eslint-disable-next-line react-hooks/exhaustive-deps
|
||||
}, [selectedType, setValue]);
|
||||
|
||||
useEffect(() => {
|
||||
|
||||
Reference in new issue
Block a user