Skip to content

Commit 04819d1

Browse files
committed
Form default values
1 parent 2a7cf9b commit 04819d1

File tree

4 files changed

+56
-16
lines changed

4 files changed

+56
-16
lines changed

src/pages/instance/Proxy/index.tsx

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/* eslint-disable @typescript-eslint/no-explicit-any */
22
import { zodResolver } from "@hookform/resolvers/zod";
33
import { Separator } from "@radix-ui/react-dropdown-menu";
4-
import { useState } from "react";
4+
import { useEffect, useState } from "react";
55
import { useForm } from "react-hook-form";
66
import { useTranslation } from "react-i18next";
77
import { toast } from "react-toastify";
@@ -42,15 +42,28 @@ function Proxy() {
4242
const form = useForm<FormSchemaType>({
4343
resolver: zodResolver(formSchema),
4444
defaultValues: {
45-
enabled: proxy?.enabled ?? false,
46-
host: proxy?.host ?? "",
47-
port: proxy?.port ?? "",
48-
protocol: proxy?.protocol ?? "http",
49-
username: proxy?.username ?? "",
50-
password: proxy?.password ?? "",
45+
enabled: false,
46+
host: "",
47+
port: "",
48+
protocol: "http",
49+
username: "",
50+
password: "",
5151
},
5252
});
5353

54+
useEffect(() => {
55+
if (proxy) {
56+
form.reset({
57+
enabled: proxy.enabled,
58+
host: proxy.host,
59+
port: proxy.port,
60+
protocol: proxy.protocol,
61+
username: proxy.username,
62+
password: proxy.password,
63+
});
64+
}
65+
}, [proxy]); // eslint-disable-line react-hooks/exhaustive-deps
66+
5467
const onSubmit = async (data: FormSchemaType) => {
5568
if (!instance) return;
5669

src/pages/instance/Rabbitmq/index.tsx

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/* eslint-disable @typescript-eslint/no-explicit-any */
22
import { zodResolver } from "@hookform/resolvers/zod";
33
import { Separator } from "@radix-ui/react-dropdown-menu";
4-
import { useState } from "react";
4+
import { useEffect, useState } from "react";
55
import { useForm } from "react-hook-form";
66
import { useTranslation } from "react-i18next";
77
import { toast } from "react-toastify";
@@ -47,11 +47,20 @@ function Rabbitmq() {
4747
const form = useForm<FormSchemaType>({
4848
resolver: zodResolver(formSchema),
4949
defaultValues: {
50-
enabled: rabbitmq?.enabled ?? false,
51-
events: rabbitmq?.events ?? [],
50+
enabled: false,
51+
events: [],
5252
},
5353
});
5454

55+
useEffect(() => {
56+
if (rabbitmq) {
57+
form.reset({
58+
enabled: rabbitmq.enabled,
59+
events: rabbitmq.events,
60+
});
61+
}
62+
}, [rabbitmq]); // eslint-disable-line react-hooks/exhaustive-deps
63+
5564
const onSubmit = async (data: FormSchemaType) => {
5665
if (!instance) return;
5766

src/pages/instance/Sqs/index.tsx

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/* eslint-disable @typescript-eslint/no-explicit-any */
22
import { zodResolver } from "@hookform/resolvers/zod";
33
import { Separator } from "@radix-ui/react-dropdown-menu";
4-
import { useState } from "react";
4+
import { useEffect, useState } from "react";
55
import { useForm } from "react-hook-form";
66
import { useTranslation } from "react-i18next";
77
import { toast } from "react-toastify";
@@ -47,11 +47,20 @@ function Sqs() {
4747
const form = useForm<FormSchemaType>({
4848
resolver: zodResolver(FormSchema),
4949
defaultValues: {
50-
enabled: sqs?.enabled ?? false,
51-
events: sqs?.events ?? [],
50+
enabled: false,
51+
events: [],
5252
},
5353
});
5454

55+
useEffect(() => {
56+
if (sqs) {
57+
form.reset({
58+
enabled: sqs.enabled,
59+
events: sqs.events,
60+
});
61+
}
62+
}, [sqs]); // eslint-disable-line react-hooks/exhaustive-deps
63+
5564
const onSubmit = async (data: FormSchemaType) => {
5665
if (!instance) return;
5766
setLoading(true);

src/pages/instance/Websocket/index.tsx

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/* eslint-disable @typescript-eslint/no-explicit-any */
22
import { zodResolver } from "@hookform/resolvers/zod";
33
import { Separator } from "@radix-ui/react-dropdown-menu";
4-
import { useState } from "react";
4+
import { useEffect, useState } from "react";
55
import { useForm } from "react-hook-form";
66
import { useTranslation } from "react-i18next";
77
import { toast } from "react-toastify";
@@ -47,11 +47,20 @@ function Websocket() {
4747
const form = useForm<FormSchemaType>({
4848
resolver: zodResolver(FormSchema),
4949
defaultValues: {
50-
enabled: websocket?.enabled ?? false,
51-
events: websocket?.events ?? [],
50+
enabled: false,
51+
events: [],
5252
},
5353
});
5454

55+
useEffect(() => {
56+
if (websocket) {
57+
form.reset({
58+
enabled: websocket.enabled,
59+
events: websocket.events,
60+
});
61+
}
62+
}, [websocket]); // eslint-disable-line react-hooks/exhaustive-deps
63+
5564
const onSubmit = async (data: FormSchemaType) => {
5665
if (!instance) return;
5766

0 commit comments

Comments
 (0)