diff --git a/src/app/toolkit/add-tool/components/AddToolInputs.tsx b/src/app/toolkit/add-tool/components/AddToolInputs.tsx index 63d07d9b..cd89ed58 100644 --- a/src/app/toolkit/add-tool/components/AddToolInputs.tsx +++ b/src/app/toolkit/add-tool/components/AddToolInputs.tsx @@ -21,7 +21,6 @@ export default function Inputs() { const [confirmationModalOpen, setConfirmationModalOpen] = useState(false); const [unusedCategoryModalOpen, setUnusedCategoryModalOpen] = useState(false); const [unusedCategory, setUnusedCategory] = useState([""]); - const [saveUnusedCategory, setSaveUnusedCategory] = useState(false); const [categoryErrorModal, setCategoryErrorModal] = useState(false); const [infoUrlErrorModal, setInfoUrlErrorModal] = useState(false); const [imageUrlErrorModal, setImageUrlErrorModal] = useState(false); @@ -29,93 +28,85 @@ export default function Inputs() { const [submitErrorModal, setSubmitErrorModal] = useState(false); const [submitErrorMessage, setSubmitErrorMessage] = useState(""); - function SubmitButton() { - const handleSubmit = async () => { - console.log(`Validating form with state: ${JSON.stringify(formState)}`); - - if (!formState.name || formState.name.trim() === "") { - setNameErrorModalOpen(true); + const handleSubmit = async () => { + console.log(`Validating form with state: ${JSON.stringify(formState)}`); + + if (!formState.name || formState.name.trim() === "") { + setNameErrorModalOpen(true); + return; + } + + if (formState.categories.length === 0) { + setCategoryErrorModal(true); + return; + } + + if (formState.infoUrl) { + const infoUrlValidation = validateUrl(formState.infoUrl, "Info URL"); + if (!infoUrlValidation.isValid) { + console.error(`Info URL validation failed: ${infoUrlValidation.error}`); + setSubmitErrorMessage(infoUrlValidation.error || ""); + setInfoUrlErrorModal(true); return; } - - if (formState.categories.length === 0) { - setCategoryErrorModal(true); - return; - } - - if (formState.infoUrl) { - const infoUrlValidation = validateUrl(formState.infoUrl, "Info URL"); - if (!infoUrlValidation.isValid) { - console.error( - `Info URL validation failed: ${infoUrlValidation.error}` - ); - setSubmitErrorMessage(infoUrlValidation.error || ""); - setInfoUrlErrorModal(true); - return; - } - console.log( - `Info URL validated successfully: ${infoUrlValidation.url}` - ); - } - - if (formState.imageUrl) { - const imageUrlValidation = validateUrl(formState.imageUrl, "Image URL"); - if (!imageUrlValidation.isValid) { - console.error( - `Image URL validation failed: ${imageUrlValidation.error}` - ); - setSubmitErrorMessage(imageUrlValidation.error || ""); - setImageUrlErrorModal(true); - return; - } - console.log( - `Image URL validated successfully: ${imageUrlValidation.url}` + console.log(`Info URL validated successfully: ${infoUrlValidation.url}`); + } + + if (formState.imageUrl) { + const imageUrlValidation = validateUrl(formState.imageUrl, "Image URL"); + if (!imageUrlValidation.isValid) { + console.error( + `Image URL validation failed: ${imageUrlValidation.error}` ); + setSubmitErrorMessage(imageUrlValidation.error || ""); + setImageUrlErrorModal(true); + return; } - - try { - for (const category of formState.pendingCategories) { - if (formState.categories.includes(category)) { - await database.addCategories(category); - } else { - await setUnusedCategoryModalOpen(true); - if (saveUnusedCategory) { - setUnusedCategory(unusedCategory.concat(category)); - await database.addCategories(category); - } - } + console.log( + `Image URL validated successfully: ${imageUrlValidation.url}` + ); + } + + // Submit categories + try { + for (const category of formState.pendingCategories) { + if (formState.categories.includes(category)) { + await database.addCategories(category); + } else { + setUnusedCategoryModalOpen(true); } - - await database.addToDb("toolkit_items", { - id: crypto.randomUUID(), - name: formState.name, - categories: formState.categories, - description: formState.description, - checked: false, - infoUrl: formState.infoUrl, - imageUrl: formState.imageUrl, - timestamp: new Date().toISOString(), - }); - - console.log(`Created ${formState.name} in the database`); - - setConfirmationModalOpen(true); - } catch (error) { - console.error("Error submitting form:", error); - setSubmitErrorMessage( - error instanceof Error ? error.message : "Unknown error" - ); - setSubmitErrorModal(true); } - }; - return ( - - ); - } + await database.addToDb("toolkit_items", { + id: crypto.randomUUID(), + name: formState.name, + categories: formState.categories, + description: formState.description, + checked: false, + infoUrl: formState.infoUrl, + imageUrl: formState.imageUrl, + timestamp: new Date().toISOString(), + }); + + console.log(`Created ${formState.name} in the database`); + + setConfirmationModalOpen(true); + } catch (error) { + console.error("Error submitting form:", error); + setSubmitErrorMessage( + error instanceof Error ? error.message : "Unknown error" + ); + setSubmitErrorModal(true); + } + }; + + const saveUnusedCategory = async () => { + for (const category of formState.pendingCategories) { + console.log("I am saving the unused category"); + setUnusedCategory(unusedCategory.concat(category)); + await database.addCategories(category); + } + }; return (