From ed3585f9c8fac473a1acd7b46bf5b9e26fb8825d Mon Sep 17 00:00:00 2001 From: enes Date: Wed, 8 Jan 2025 13:59:35 +0100 Subject: [PATCH] fix(mod): reset form --- src/components/ModForm.tsx | 18 +++++------------- src/utils/mod.ts | 26 ++++++++++++++------------ 2 files changed, 19 insertions(+), 25 deletions(-) diff --git a/src/components/ModForm.tsx b/src/components/ModForm.tsx index 914db43..f539739 100644 --- a/src/components/ModForm.tsx +++ b/src/components/ModForm.tsx @@ -154,20 +154,12 @@ export const ModForm = () => { // Cancel if not confirmed if (!confirm) return - // Editing - if (mod) { - const initial = initializeFormState(mod) + // Reset fields to the initial or original existing data + const newState = initializeFormState(mod) - // Reset editor - editorRef.current?.setMarkdown(initial.body) - - // Reset fields to the original existing data - setFormState(initial) - return - } - - // New - set form state to the initial (clear form state) - setFormState(initializeFormState()) + // Reset editor + editorRef.current?.setMarkdown(newState.body) + setFormState(newState) } const handlePublish = () => { submit(JSON.stringify(formState), { diff --git a/src/utils/mod.ts b/src/utils/mod.ts index 71ceb39..3d40002 100644 --- a/src/utils/mod.ts +++ b/src/utils/mod.ts @@ -131,16 +131,18 @@ export const initializeFormState = ( originalAuthor: existingModData?.originalAuthor || undefined, screenshotsUrls: existingModData?.screenshotsUrls || [''], tags: existingModData?.tags.join(',') || '', - lTags: existingModData?.lTags || [], - LTags: existingModData?.LTags || [], - downloadUrls: existingModData?.downloadUrls || [ - { - url: '', - hash: '', - signatureKey: '', - malwareScanLink: '', - modVersion: '', - customNote: '' - } - ] + lTags: existingModData ? [...existingModData.lTags] : [], + LTags: existingModData ? [...existingModData.LTags] : [], + downloadUrls: existingModData + ? [...existingModData.downloadUrls.map((url) => ({ ...url }))] + : [ + { + url: '', + hash: '', + signatureKey: '', + malwareScanLink: '', + modVersion: '', + customNote: '' + } + ] })