mirror of
https://litchi.icu/ngc2207/judge4c.git
synced 2025-05-18 19:46:57 +00:00
feat: refactor AdminCreateUserForm to use defaultValues and add reset functionality
This commit is contained in:
parent
dfa5179056
commit
b8be02025e
@ -25,6 +25,7 @@ import {
|
||||
SelectValue,
|
||||
} from "@/components/ui/select";
|
||||
import logger from "@/lib/logger";
|
||||
import { useEffect } from "react";
|
||||
import { Loader2 } from "lucide-react";
|
||||
import { useForm } from "react-hook-form";
|
||||
import { useToast } from "@/hooks/use-toast";
|
||||
@ -53,20 +54,22 @@ const formSchema = z.object({
|
||||
must_change_password: z.coerce.boolean().default(true),
|
||||
});
|
||||
|
||||
export default function AdminCreateUserForm() {
|
||||
const { toast } = useToast();
|
||||
const { user, error, loading, adminCreateUser } = adminCreateUserStore();
|
||||
|
||||
const form = useForm<z.infer<typeof formSchema>>({
|
||||
resolver: zodResolver(formSchema),
|
||||
defaultValues: {
|
||||
const defaultValues: z.infer<typeof formSchema> = {
|
||||
source_id: 0,
|
||||
visibility: "public",
|
||||
username: "",
|
||||
email: "",
|
||||
password: "",
|
||||
must_change_password: true,
|
||||
},
|
||||
};
|
||||
|
||||
export default function AdminCreateUserForm() {
|
||||
const { toast } = useToast();
|
||||
const { user, error, loading, adminCreateUser } = adminCreateUserStore();
|
||||
|
||||
const form = useForm<z.infer<typeof formSchema>>({
|
||||
resolver: zodResolver(formSchema),
|
||||
defaultValues,
|
||||
});
|
||||
|
||||
function onSubmit(values: z.infer<typeof formSchema>) {
|
||||
@ -91,6 +94,14 @@ export default function AdminCreateUserForm() {
|
||||
}
|
||||
}
|
||||
|
||||
function onReset() {
|
||||
form.reset(defaultValues);
|
||||
}
|
||||
|
||||
useEffect(() => {
|
||||
form.reset(defaultValues);
|
||||
}, [form]);
|
||||
|
||||
return (
|
||||
<div className="h-full w-full flex items-center justify-center px-4">
|
||||
<Card className="mx-auto min-w-80">
|
||||
@ -109,7 +120,7 @@ export default function AdminCreateUserForm() {
|
||||
<FormLabel>Authentication Source</FormLabel>
|
||||
<Select
|
||||
onValueChange={field.onChange}
|
||||
defaultValue={field.value.toString()}
|
||||
value={field.value.toString()}
|
||||
>
|
||||
<FormControl>
|
||||
<SelectTrigger>
|
||||
@ -130,10 +141,7 @@ export default function AdminCreateUserForm() {
|
||||
render={({ field }) => (
|
||||
<FormItem>
|
||||
<FormLabel>User visibility</FormLabel>
|
||||
<Select
|
||||
onValueChange={field.onChange}
|
||||
defaultValue={field.value}
|
||||
>
|
||||
<Select onValueChange={field.onChange} value={field.value}>
|
||||
<FormControl>
|
||||
<SelectTrigger>
|
||||
<SelectValue />
|
||||
@ -212,6 +220,10 @@ export default function AdminCreateUserForm() {
|
||||
</FormItem>
|
||||
)}
|
||||
/>
|
||||
<div className="flex items-center justify-between">
|
||||
<Button variant="outline" onClick={onReset}>
|
||||
Reset
|
||||
</Button>
|
||||
<Button type="submit" disabled={loading}>
|
||||
{loading ? (
|
||||
<>
|
||||
@ -221,6 +233,7 @@ export default function AdminCreateUserForm() {
|
||||
"Submit"
|
||||
)}
|
||||
</Button>
|
||||
</div>
|
||||
</form>
|
||||
</Form>
|
||||
</CardContent>
|
||||
|
Loading…
Reference in New Issue
Block a user