refactor: remove read-only mode from notification form
This commit is contained in:
parent
fb74aebd12
commit
87082398b0
5 changed files with 17 additions and 34 deletions
|
|
@ -59,7 +59,6 @@ type Props = {
|
||||||
initialValues?: Partial<NotificationFormValues>;
|
initialValues?: Partial<NotificationFormValues>;
|
||||||
formId?: string;
|
formId?: string;
|
||||||
className?: string;
|
className?: string;
|
||||||
readOnly?: boolean;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
const defaultValuesForType = {
|
const defaultValuesForType = {
|
||||||
|
|
@ -123,14 +122,7 @@ const defaultValuesForType = {
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
export const CreateNotificationForm = ({
|
export const CreateNotificationForm = ({ onSubmit, mode = "create", initialValues, formId, className }: Props) => {
|
||||||
onSubmit,
|
|
||||||
mode = "create",
|
|
||||||
initialValues,
|
|
||||||
formId,
|
|
||||||
className,
|
|
||||||
readOnly = false,
|
|
||||||
}: Props) => {
|
|
||||||
const form = useForm<NotificationFormValues>({
|
const form = useForm<NotificationFormValues>({
|
||||||
resolver: zodResolver(formSchema, undefined, { raw: true }),
|
resolver: zodResolver(formSchema, undefined, { raw: true }),
|
||||||
defaultValues: initialValues || {
|
defaultValues: initialValues || {
|
||||||
|
|
@ -153,7 +145,7 @@ export const CreateNotificationForm = ({
|
||||||
onSubmit={form.handleSubmit(onSubmit, scrollToFirstError)}
|
onSubmit={form.handleSubmit(onSubmit, scrollToFirstError)}
|
||||||
className={cn("space-y-4", className)}
|
className={cn("space-y-4", className)}
|
||||||
>
|
>
|
||||||
<fieldset disabled={readOnly} className="space-y-4">
|
<fieldset className="space-y-4">
|
||||||
<FormField
|
<FormField
|
||||||
control={form.control}
|
control={form.control}
|
||||||
name="name"
|
name="name"
|
||||||
|
|
@ -161,7 +153,7 @@ export const CreateNotificationForm = ({
|
||||||
<FormItem>
|
<FormItem>
|
||||||
<FormLabel>Name</FormLabel>
|
<FormLabel>Name</FormLabel>
|
||||||
<FormControl>
|
<FormControl>
|
||||||
<Input {...field} placeholder="My notification" max={32} min={2} disabled={readOnly} />
|
<Input {...field} placeholder="My notification" max={32} min={2} />
|
||||||
</FormControl>
|
</FormControl>
|
||||||
<FormDescription>Unique identifier for this notification destination.</FormDescription>
|
<FormDescription>Unique identifier for this notification destination.</FormDescription>
|
||||||
<FormMessage />
|
<FormMessage />
|
||||||
|
|
@ -186,10 +178,10 @@ export const CreateNotificationForm = ({
|
||||||
}
|
}
|
||||||
}}
|
}}
|
||||||
value={field.value ?? ""}
|
value={field.value ?? ""}
|
||||||
disabled={mode === "update" || readOnly}
|
disabled={mode === "update"}
|
||||||
>
|
>
|
||||||
<FormControl>
|
<FormControl>
|
||||||
<SelectTrigger className={mode === "update" || readOnly ? "bg-gray-50" : ""}>
|
<SelectTrigger className={mode === "update" ? "bg-gray-50" : ""}>
|
||||||
<SelectValue placeholder="Select notification type" />
|
<SelectValue placeholder="Select notification type" />
|
||||||
</SelectTrigger>
|
</SelectTrigger>
|
||||||
</FormControl>
|
</FormControl>
|
||||||
|
|
@ -211,14 +203,14 @@ export const CreateNotificationForm = ({
|
||||||
)}
|
)}
|
||||||
/>
|
/>
|
||||||
|
|
||||||
{watchedType === "email" && <EmailForm form={form} readOnly={readOnly} />}
|
{watchedType === "email" && <EmailForm form={form} />}
|
||||||
{watchedType === "slack" && <SlackForm form={form} />}
|
{watchedType === "slack" && <SlackForm form={form} />}
|
||||||
{watchedType === "discord" && <DiscordForm form={form} />}
|
{watchedType === "discord" && <DiscordForm form={form} />}
|
||||||
{watchedType === "gotify" && <GotifyForm form={form} />}
|
{watchedType === "gotify" && <GotifyForm form={form} />}
|
||||||
{watchedType === "ntfy" && <NtfyForm form={form} readOnly={readOnly} />}
|
{watchedType === "ntfy" && <NtfyForm form={form} />}
|
||||||
{watchedType === "pushover" && <PushoverForm form={form} readOnly={readOnly} />}
|
{watchedType === "pushover" && <PushoverForm form={form} />}
|
||||||
{watchedType === "telegram" && <TelegramForm form={form} />}
|
{watchedType === "telegram" && <TelegramForm form={form} />}
|
||||||
{watchedType === "generic" && <GenericForm form={form} readOnly={readOnly} />}
|
{watchedType === "generic" && <GenericForm form={form} />}
|
||||||
{watchedType === "custom" && <CustomForm form={form} />}
|
{watchedType === "custom" && <CustomForm form={form} />}
|
||||||
</fieldset>
|
</fieldset>
|
||||||
</form>
|
</form>
|
||||||
|
|
|
||||||
|
|
@ -7,10 +7,9 @@ import type { NotificationFormValues } from "../create-notification-form";
|
||||||
|
|
||||||
type Props = {
|
type Props = {
|
||||||
form: UseFormReturn<NotificationFormValues>;
|
form: UseFormReturn<NotificationFormValues>;
|
||||||
readOnly?: boolean;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
export const EmailForm = ({ form, readOnly = false }: Props) => {
|
export const EmailForm = ({ form }: Props) => {
|
||||||
return (
|
return (
|
||||||
<>
|
<>
|
||||||
<FormField
|
<FormField
|
||||||
|
|
@ -129,7 +128,7 @@ export const EmailForm = ({ form, readOnly = false }: Props) => {
|
||||||
render={({ field }) => (
|
render={({ field }) => (
|
||||||
<FormItem className="flex flex-row items-center space-x-3">
|
<FormItem className="flex flex-row items-center space-x-3">
|
||||||
<FormControl>
|
<FormControl>
|
||||||
<Checkbox checked={field.value} disabled={readOnly} onCheckedChange={field.onChange} />
|
<Checkbox checked={field.value} onCheckedChange={field.onChange} />
|
||||||
</FormControl>
|
</FormControl>
|
||||||
<div className="space-y-1 leading-none">
|
<div className="space-y-1 leading-none">
|
||||||
<FormLabel>Use TLS</FormLabel>
|
<FormLabel>Use TLS</FormLabel>
|
||||||
|
|
|
||||||
|
|
@ -10,7 +10,6 @@ import type { NotificationFormValues } from "../create-notification-form";
|
||||||
|
|
||||||
type Props = {
|
type Props = {
|
||||||
form: UseFormReturn<NotificationFormValues>;
|
form: UseFormReturn<NotificationFormValues>;
|
||||||
readOnly?: boolean;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
const WebhookPreview = ({ values }: { values: Partial<NotificationFormValues> }) => {
|
const WebhookPreview = ({ values }: { values: Partial<NotificationFormValues> }) => {
|
||||||
|
|
@ -49,7 +48,7 @@ ${body}`;
|
||||||
);
|
);
|
||||||
};
|
};
|
||||||
|
|
||||||
export const GenericForm = ({ form, readOnly = false }: Props) => {
|
export const GenericForm = ({ form }: Props) => {
|
||||||
const watchedValues = useWatch({ control: form.control });
|
const watchedValues = useWatch({ control: form.control });
|
||||||
const useJson = useWatch({ control: form.control, name: "useJson" });
|
const useJson = useWatch({ control: form.control, name: "useJson" });
|
||||||
|
|
||||||
|
|
@ -75,7 +74,7 @@ export const GenericForm = ({ form, readOnly = false }: Props) => {
|
||||||
render={({ field }) => (
|
render={({ field }) => (
|
||||||
<FormItem>
|
<FormItem>
|
||||||
<FormLabel>Method</FormLabel>
|
<FormLabel>Method</FormLabel>
|
||||||
<Select disabled={readOnly} onValueChange={field.onChange} value={field.value ?? ""}>
|
<Select onValueChange={field.onChange} value={field.value ?? ""}>
|
||||||
<FormControl>
|
<FormControl>
|
||||||
<SelectTrigger>
|
<SelectTrigger>
|
||||||
<SelectValue placeholder="Select method" />
|
<SelectValue placeholder="Select method" />
|
||||||
|
|
@ -128,11 +127,7 @@ export const GenericForm = ({ form, readOnly = false }: Props) => {
|
||||||
render={({ field }) => (
|
render={({ field }) => (
|
||||||
<FormItem className="flex flex-row items-center space-x-3">
|
<FormItem className="flex flex-row items-center space-x-3">
|
||||||
<FormControl>
|
<FormControl>
|
||||||
<Checkbox
|
<Checkbox checked={field.value} onCheckedChange={(checked) => field.onChange(checked)} />
|
||||||
checked={field.value}
|
|
||||||
disabled={readOnly}
|
|
||||||
onCheckedChange={(checked) => field.onChange(checked)}
|
|
||||||
/>
|
|
||||||
</FormControl>
|
</FormControl>
|
||||||
<div className="space-y-1 leading-none">
|
<div className="space-y-1 leading-none">
|
||||||
<FormLabel>Use JSON Template</FormLabel>
|
<FormLabel>Use JSON Template</FormLabel>
|
||||||
|
|
|
||||||
|
|
@ -7,10 +7,9 @@ import type { NotificationFormValues } from "../create-notification-form";
|
||||||
|
|
||||||
type Props = {
|
type Props = {
|
||||||
form: UseFormReturn<NotificationFormValues>;
|
form: UseFormReturn<NotificationFormValues>;
|
||||||
readOnly?: boolean;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
export const NtfyForm = ({ form, readOnly = false }: Props) => {
|
export const NtfyForm = ({ form }: Props) => {
|
||||||
return (
|
return (
|
||||||
<>
|
<>
|
||||||
<FormField
|
<FormField
|
||||||
|
|
@ -91,7 +90,7 @@ export const NtfyForm = ({ form, readOnly = false }: Props) => {
|
||||||
render={({ field }) => (
|
render={({ field }) => (
|
||||||
<FormItem>
|
<FormItem>
|
||||||
<FormLabel>Priority</FormLabel>
|
<FormLabel>Priority</FormLabel>
|
||||||
<Select disabled={readOnly} onValueChange={field.onChange} value={String(field.value)}>
|
<Select onValueChange={field.onChange} value={String(field.value)}>
|
||||||
<FormControl>
|
<FormControl>
|
||||||
<SelectTrigger>
|
<SelectTrigger>
|
||||||
<SelectValue placeholder="Select priority" />
|
<SelectValue placeholder="Select priority" />
|
||||||
|
|
|
||||||
|
|
@ -7,10 +7,9 @@ import type { NotificationFormValues } from "../create-notification-form";
|
||||||
|
|
||||||
type Props = {
|
type Props = {
|
||||||
form: UseFormReturn<NotificationFormValues>;
|
form: UseFormReturn<NotificationFormValues>;
|
||||||
readOnly?: boolean;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
export const PushoverForm = ({ form, readOnly = false }: Props) => {
|
export const PushoverForm = ({ form }: Props) => {
|
||||||
return (
|
return (
|
||||||
<>
|
<>
|
||||||
<FormField
|
<FormField
|
||||||
|
|
@ -62,7 +61,6 @@ export const PushoverForm = ({ form, readOnly = false }: Props) => {
|
||||||
<FormItem>
|
<FormItem>
|
||||||
<FormLabel>Priority</FormLabel>
|
<FormLabel>Priority</FormLabel>
|
||||||
<Select
|
<Select
|
||||||
disabled={readOnly}
|
|
||||||
onValueChange={(value) => field.onChange(Number(value))}
|
onValueChange={(value) => field.onChange(Number(value))}
|
||||||
defaultValue={String(field.value)}
|
defaultValue={String(field.value)}
|
||||||
value={String(field.value)}
|
value={String(field.value)}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue