From 3e847297b11d52bb7ce51aa650911ddbd6765730 Mon Sep 17 00:00:00 2001 From: Ronni Skansing Date: Tue, 21 Oct 2025 18:20:54 +0200 Subject: [PATCH] Fix check name before moving on in campaign wizard Signed-off-by: Ronni Skansing --- frontend/src/routes/campaign/+page.svelte | 62 ++++++++++++++++------- 1 file changed, 45 insertions(+), 17 deletions(-) diff --git a/frontend/src/routes/campaign/+page.svelte b/frontend/src/routes/campaign/+page.svelte index 5e831b5..cc2fd81 100644 --- a/frontend/src/routes/campaign/+page.svelte +++ b/frontend/src/routes/campaign/+page.svelte @@ -261,6 +261,7 @@ let isSubmitting = false; let isTableLoading = false; let modalText = ''; + let isValidatingName = false; let weekDaysAvailable = []; let isDeleteAlertVisible = false; @@ -334,7 +335,7 @@ }); const nextStep = async () => { - if (validateCurrentStep()) { + if (await validateCurrentStep()) { currentStep = Math.min(currentStep + 1, campaignSteps.length); modalError = ''; // reset tab focus after dom update - only for explicit step navigation @@ -372,10 +373,10 @@ }, 0); }; - const validateCurrentStep = () => { + const validateCurrentStep = async () => { switch (currentStep) { case 1: - return validateBasicInfo(); + return await validateBasicInfo(); case 2: return validateRecipients(); case 3: @@ -402,8 +403,31 @@ return true; }; - const validateBasicInfo = () => { - return checkCurrentStepValidity(); + const validateBasicInfo = async () => { + if (!checkCurrentStepValidity()) { + return false; + } + + // check if campaign name exists + if (formValues.name?.length) { + isValidatingName = true; + try { + const nameExists = await campaignNameExists(formValues.name); + if (nameExists) { + /** @type {HTMLInputElement} */ + const ele = document.querySelector('#campaignName'); + if (ele) { + ele.setCustomValidity('Name is used by another campaign'); + ele.reportValidity(); + } + return false; + } + } finally { + isValidatingName = false; + } + } + + return true; }; const validateRecipients = () => { @@ -733,23 +757,22 @@ }; /** @param {string} name */ - const campaignNameExits = async (name) => { + const campaignNameExists = async (name) => { + if (!name?.length) return false; + try { const res = await api.campaign.getByName(name, contextCompanyID); - /** @type {HTMLInputElement} */ - const ele = document.querySelector('#campaignName'); if ( res.data && (modalMode === 'create' || modalMode === 'copy' || res.data.id !== formValues.id) ) { - ele.setCustomValidity('Name is used by another campaign'); - ele.reportValidity(); - } else { - ele.setCustomValidity(''); + return true; } + return false; } catch (e) { addToast('Failed to check if campaign name is used', 'Error'); console.error('Failed to check if campaign name is used', e); + return false; } }; @@ -816,6 +839,7 @@ modalMode = null; isModalVisible = false; currentStep = 1; + isValidatingName = false; if (form) form.reset(); resetFormValues(); }; @@ -1193,10 +1217,9 @@ const ele = document.querySelector('#campaignName'); ele.setCustomValidity(''); }} - onBlur={() => { - formValues.name.length && campaignNameExits(formValues.name); - }}>Name + Name + - Next + {#if isValidatingName} + Checking... + {:else} + Next + {/if}