refactor: remove flush sync

This commit is contained in:
Mauricio Siu
2024-07-26 01:04:52 -06:00
parent ee58672d58
commit fe51dd6b0a

View File

@@ -34,7 +34,6 @@ import { toast } from "sonner";
import { domain } from "@/server/db/validations"; import { domain } from "@/server/db/validations";
import { zodResolver } from "@hookform/resolvers/zod"; import { zodResolver } from "@hookform/resolvers/zod";
import { flushSync } from "react-dom";
import type z from "zod"; import type z from "zod";
type Domain = z.infer<typeof domain>; type Domain = z.infer<typeof domain>;
@@ -52,7 +51,7 @@ export const AddDomain = ({
}: Props) => { }: Props) => {
const [isOpen, setIsOpen] = useState(false); const [isOpen, setIsOpen] = useState(false);
const utils = api.useUtils(); const utils = api.useUtils();
const { data } = api.domain.one.useQuery( const { data, refetch } = api.domain.one.useQuery(
{ {
domainId, domainId,
}, },
@@ -61,7 +60,7 @@ export const AddDomain = ({
}, },
); );
const { mutateAsync, isError, error } = domainId const { mutateAsync, isError, error, isLoading } = domainId
? api.domain.update.useMutation() ? api.domain.update.useMutation()
: api.domain.create.useMutation(); : api.domain.create.useMutation();
@@ -74,11 +73,15 @@ export const AddDomain = ({
form.reset({ form.reset({
...data, ...data,
/* Convert null to undefined */ /* Convert null to undefined */
path: data.path || undefined, path: data?.path || undefined,
port: data.port || undefined, port: data?.port || undefined,
}); });
} }
}, [form, form.reset, data]);
if (!domainId) {
form.reset({});
}
}, [form, form.reset, data, isLoading]);
const dictionary = { const dictionary = {
success: domainId ? "Domain Updated" : "Domain Created", success: domainId ? "Domain Updated" : "Domain Created",
@@ -104,13 +107,8 @@ export const AddDomain = ({
}); });
await utils.application.readTraefikConfig.invalidate({ applicationId }); await utils.application.readTraefikConfig.invalidate({ applicationId });
/* if (domainId) {
Reset form if it was a new domain refetch();
Flushsync is needed for a bug witht he react-hook-form reset method
https://github.com/orgs/react-hook-form/discussions/7589#discussioncomment-10060621
*/
if (!domainId) {
flushSync(() => form.reset());
} }
setIsOpen(false); setIsOpen(false);
}) })