mirror of
https://github.com/blakeblackshear/frigate.git
synced 2026-05-05 13:07:44 +03:00
Simplify dialog
This commit is contained in:
parent
1d72516296
commit
1fbdbd05c1
@ -1,3 +1,4 @@
|
|||||||
|
import TextEntry from "@/components/input/TextEntry";
|
||||||
import { Button } from "@/components/ui/button";
|
import { Button } from "@/components/ui/button";
|
||||||
import {
|
import {
|
||||||
Dialog,
|
Dialog,
|
||||||
@ -7,15 +8,8 @@ import {
|
|||||||
DialogHeader,
|
DialogHeader,
|
||||||
DialogTitle,
|
DialogTitle,
|
||||||
} from "@/components/ui/dialog";
|
} from "@/components/ui/dialog";
|
||||||
import { Form, FormControl, FormField, FormItem } from "@/components/ui/form";
|
|
||||||
import { Input } from "@/components/ui/input";
|
|
||||||
import { zodResolver } from "@hookform/resolvers/zod";
|
|
||||||
import { useCallback, useEffect } from "react";
|
|
||||||
import { useForm } from "react-hook-form";
|
|
||||||
import { useTranslation } from "react-i18next";
|
import { useTranslation } from "react-i18next";
|
||||||
|
|
||||||
import { z } from "zod";
|
|
||||||
|
|
||||||
type TextEntryDialogProps = {
|
type TextEntryDialogProps = {
|
||||||
open: boolean;
|
open: boolean;
|
||||||
title: string;
|
title: string;
|
||||||
@ -35,36 +29,8 @@ export default function TextEntryDialog({
|
|||||||
defaultValue = "",
|
defaultValue = "",
|
||||||
allowEmpty = false,
|
allowEmpty = false,
|
||||||
}: TextEntryDialogProps) {
|
}: TextEntryDialogProps) {
|
||||||
const formSchema = z.object({
|
|
||||||
text: z.string(),
|
|
||||||
});
|
|
||||||
|
|
||||||
const { t } = useTranslation("components/dialog");
|
const { t } = useTranslation("components/dialog");
|
||||||
|
|
||||||
const form = useForm<z.infer<typeof formSchema>>({
|
|
||||||
resolver: zodResolver(formSchema),
|
|
||||||
defaultValues: { text: defaultValue },
|
|
||||||
});
|
|
||||||
const fileRef = form.register("text");
|
|
||||||
|
|
||||||
// upload handler
|
|
||||||
|
|
||||||
const onSubmit = useCallback(
|
|
||||||
(data: z.infer<typeof formSchema>) => {
|
|
||||||
if (!allowEmpty && !data["text"]) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
onSave(data["text"]);
|
|
||||||
},
|
|
||||||
[onSave, allowEmpty],
|
|
||||||
);
|
|
||||||
|
|
||||||
useEffect(() => {
|
|
||||||
if (open) {
|
|
||||||
form.reset({ text: defaultValue });
|
|
||||||
}
|
|
||||||
}, [open, defaultValue, form]);
|
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<Dialog open={open} defaultOpen={false} onOpenChange={setOpen}>
|
<Dialog open={open} defaultOpen={false} onOpenChange={setOpen}>
|
||||||
<DialogContent>
|
<DialogContent>
|
||||||
@ -72,33 +38,20 @@ export default function TextEntryDialog({
|
|||||||
<DialogTitle>{title}</DialogTitle>
|
<DialogTitle>{title}</DialogTitle>
|
||||||
{description && <DialogDescription>{description}</DialogDescription>}
|
{description && <DialogDescription>{description}</DialogDescription>}
|
||||||
</DialogHeader>
|
</DialogHeader>
|
||||||
<Form {...form}>
|
<TextEntry
|
||||||
<form onSubmit={form.handleSubmit(onSubmit)}>
|
defaultValue={defaultValue}
|
||||||
<FormField
|
allowEmpty={allowEmpty}
|
||||||
control={form.control}
|
onSave={onSave}
|
||||||
name="text"
|
>
|
||||||
render={() => (
|
<DialogFooter className="pt-4">
|
||||||
<FormItem>
|
<Button type="button" onClick={() => setOpen(false)}>
|
||||||
<FormControl>
|
{t("button.cancel", { ns: "common" })}
|
||||||
<Input
|
</Button>
|
||||||
className="aspect-video h-8 w-full"
|
<Button variant="select" type="submit">
|
||||||
type="text"
|
{t("button.save", { ns: "common" })}
|
||||||
{...fileRef}
|
</Button>
|
||||||
/>
|
</DialogFooter>
|
||||||
</FormControl>
|
</TextEntry>
|
||||||
</FormItem>
|
|
||||||
)}
|
|
||||||
/>
|
|
||||||
<DialogFooter className="pt-4">
|
|
||||||
<Button type="button" onClick={() => setOpen(false)}>
|
|
||||||
{t("button.cancel", { ns: "common" })}
|
|
||||||
</Button>
|
|
||||||
<Button variant="select" type="submit">
|
|
||||||
{t("button.save", { ns: "common" })}
|
|
||||||
</Button>
|
|
||||||
</DialogFooter>
|
|
||||||
</form>
|
|
||||||
</Form>
|
|
||||||
</DialogContent>
|
</DialogContent>
|
||||||
</Dialog>
|
</Dialog>
|
||||||
);
|
);
|
||||||
|
|||||||
@ -1,3 +1,4 @@
|
|||||||
|
import ImageEntry from "@/components/input/ImageEntry";
|
||||||
import { Button } from "@/components/ui/button";
|
import { Button } from "@/components/ui/button";
|
||||||
import {
|
import {
|
||||||
Dialog,
|
Dialog,
|
||||||
@ -7,12 +8,6 @@ import {
|
|||||||
DialogHeader,
|
DialogHeader,
|
||||||
DialogTitle,
|
DialogTitle,
|
||||||
} from "@/components/ui/dialog";
|
} from "@/components/ui/dialog";
|
||||||
import { Form, FormControl, FormField, FormItem } from "@/components/ui/form";
|
|
||||||
import { Input } from "@/components/ui/input";
|
|
||||||
import { zodResolver } from "@hookform/resolvers/zod";
|
|
||||||
import { useCallback } from "react";
|
|
||||||
import { useForm } from "react-hook-form";
|
|
||||||
import { z } from "zod";
|
|
||||||
|
|
||||||
type UploadImageDialogProps = {
|
type UploadImageDialogProps = {
|
||||||
open: boolean;
|
open: boolean;
|
||||||
@ -28,28 +23,6 @@ export default function UploadImageDialog({
|
|||||||
setOpen,
|
setOpen,
|
||||||
onSave,
|
onSave,
|
||||||
}: UploadImageDialogProps) {
|
}: UploadImageDialogProps) {
|
||||||
const formSchema = z.object({
|
|
||||||
file: z.instanceof(FileList, { message: "Please select an image file." }),
|
|
||||||
});
|
|
||||||
|
|
||||||
const form = useForm<z.infer<typeof formSchema>>({
|
|
||||||
resolver: zodResolver(formSchema),
|
|
||||||
});
|
|
||||||
const fileRef = form.register("file");
|
|
||||||
|
|
||||||
// upload handler
|
|
||||||
|
|
||||||
const onSubmit = useCallback(
|
|
||||||
(data: z.infer<typeof formSchema>) => {
|
|
||||||
if (!data["file"] || Object.keys(data.file).length == 0) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
onSave(data["file"]["0"]);
|
|
||||||
},
|
|
||||||
[onSave],
|
|
||||||
);
|
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<Dialog open={open} defaultOpen={false} onOpenChange={setOpen}>
|
<Dialog open={open} defaultOpen={false} onOpenChange={setOpen}>
|
||||||
<DialogContent>
|
<DialogContent>
|
||||||
@ -57,31 +30,14 @@ export default function UploadImageDialog({
|
|||||||
<DialogTitle>{title}</DialogTitle>
|
<DialogTitle>{title}</DialogTitle>
|
||||||
{description && <DialogDescription>{description}</DialogDescription>}
|
{description && <DialogDescription>{description}</DialogDescription>}
|
||||||
</DialogHeader>
|
</DialogHeader>
|
||||||
<Form {...form}>
|
<ImageEntry onSave={onSave}>
|
||||||
<form onSubmit={form.handleSubmit(onSubmit)}>
|
<DialogFooter className="pt-4">
|
||||||
<FormField
|
<Button onClick={() => setOpen(false)}>Cancel</Button>
|
||||||
control={form.control}
|
<Button variant="select" type="submit">
|
||||||
name="file"
|
Save
|
||||||
render={() => (
|
</Button>
|
||||||
<FormItem>
|
</DialogFooter>
|
||||||
<FormControl>
|
</ImageEntry>
|
||||||
<Input
|
|
||||||
className="aspect-video h-40 w-full"
|
|
||||||
type="file"
|
|
||||||
{...fileRef}
|
|
||||||
/>
|
|
||||||
</FormControl>
|
|
||||||
</FormItem>
|
|
||||||
)}
|
|
||||||
/>
|
|
||||||
<DialogFooter className="pt-4">
|
|
||||||
<Button onClick={() => setOpen(false)}>Cancel</Button>
|
|
||||||
<Button variant="select" type="submit">
|
|
||||||
Save
|
|
||||||
</Button>
|
|
||||||
</DialogFooter>
|
|
||||||
</form>
|
|
||||||
</Form>
|
|
||||||
</DialogContent>
|
</DialogContent>
|
||||||
</Dialog>
|
</Dialog>
|
||||||
);
|
);
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user