From f91ae2eb194b742e61477b5e0e67331689c5d9a0 Mon Sep 17 00:00:00 2001 From: Izan Gil <66965250+SrIzan10@users.noreply.github.com> Date: Fri, 20 Dec 2024 16:58:20 +0100 Subject: [PATCH] fix: uncontrolled form and fix refresh loop --- .../app/ProjectSettings/ProjectSettings.tsx | 12 ++++++---- .../app/UniversalForm/UniversalForm.tsx | 23 ++++++++++++------- 2 files changed, 22 insertions(+), 13 deletions(-) diff --git a/src/components/app/ProjectSettings/ProjectSettings.tsx b/src/components/app/ProjectSettings/ProjectSettings.tsx index 13abbee..34343cd 100644 --- a/src/components/app/ProjectSettings/ProjectSettings.tsx +++ b/src/components/app/ProjectSettings/ProjectSettings.tsx @@ -23,12 +23,14 @@ import { import GithubRepoChooser from '../GithubRepoChooser/GithubRepoChooser'; import React from 'react'; import Link from 'next/link'; -import { useRouter } from 'next/navigation'; export default function ProjectSettings(project: Project) { - const router = useRouter(); const [ghRepo, setGhRepo] = React.useState(''); + const [hasSubmitted, setHasSubmitted] = React.useState(false); const apiUrl = `https://${window.location.hostname}/api/feedback/${project.id}`; + React.useEffect(() => { + setHasSubmitted(project.github !== ''); + }, [project.github]); return (
@@ -133,7 +135,7 @@ export default function ProjectSettings(project: Project) { onSelect={(repo) => { setGhRepo(`https://github.com/${repo}`); }} - selected={project.github!.replace('https://github.com/', '')} + selected={project.github ? project.github.replace('https://github.com/', '') : ''} />

Not the results you were expecting? You may have not allowed your user in the{' '} @@ -164,7 +166,7 @@ export default function ProjectSettings(project: Project) { key={ghRepo} schemaName={'githubSettings'} action={githubSettings} - onActionComplete={() => router.refresh()} + onActionComplete={() => setHasSubmitted(true)} /> @@ -174,7 +176,7 @@ export default function ProjectSettings(project: Project) { Make sure your setup works! - {project.github ? ( + {hasSubmitted ? ( ({ submitText = 'Submit', submitClassname, }: UniversalFormProps) { - const [state, formAction] = useActionState(action, null); + // @ts-ignore idk why this error is happening, first apprearing on the react 19 update. + const [state, formAction] = useActionState<{ success: boolean; error?: string }>(action, null); const schema = schemaDb.find((s) => s.name === schemaName)?.zod; if (!schema) { throw new Error(`Schema "${schemaName}" not found`); } + // Initialize default values for all fields + const initialValues = React.useMemo(() => { + const values: Record = {}; + fields.forEach((field) => { + values[field.name] = field.value ?? ''; // Use empty string as fallback + }); + return { ...values, ...defaultValues }; + }, [fields, defaultValues]); + const form = useForm>({ resolver: zodResolver(schema), - defaultValues: (defaultValues || {}) as z.infer, + defaultValues: initialValues as z.infer, }); - // pretend nothing is happening on here React.useEffect(() => { - // @ts-ignore if (state && !state.success) { - // @ts-ignore toast.error(state.error); } if (state) { onActionComplete?.(state); } - }, [state]); + }, [state, onActionComplete]); return (

@@ -78,7 +85,6 @@ export function UniversalForm({ key={field.name} control={form.control} name={field.name as Path>} - defaultValue={field.value as PathValue, Path>>} render={({ field: formField }) => ( {field.type !== 'hidden' && {field.label}} @@ -87,6 +93,7 @@ export function UniversalForm({ type={field.type || 'text'} placeholder={field.placeholder} {...formField} + value={formField.value ?? ''} /> {field.description && {field.description}} @@ -99,4 +106,4 @@ export function UniversalForm({ ); -} +} \ No newline at end of file