diff --git a/src/pages/notification/blocks/AddDialog.tsx b/src/pages/notification/blocks/AddDialog.tsx index 8f3b248..35bbff4 100644 --- a/src/pages/notification/blocks/AddDialog.tsx +++ b/src/pages/notification/blocks/AddDialog.tsx @@ -36,6 +36,7 @@ const AddDialog = () => { const { reload } = useDataGrid(); const { GetData, PostData } = useCallApi(); const [alert, setAlert] = useState({ show: false, message: '' }); + const isSubmittingRef = useRef(false); const initialState = { customers: [] as string[], @@ -48,9 +49,11 @@ const AddDialog = () => { const [formField, setFormField] = useState(initialState); const [open, setOpen] = useState(false); + const [isSubmitting, setIsSubmitting] = useState(false); const [customers, setCustomers] = useState([]); const resetForm = () => { - setFormField(initialState); + setFormField({ ...initialState }); + setIsSubmitting(false); setAlert({ show: false, message: '' }); }; @@ -62,36 +65,41 @@ const AddDialog = () => { } }; - const doCreateNotification = async (e: React.FormEvent) => { - e.preventDefault(); + const doCreateNotification = useCallback( + async (e: React.FormEvent) => { + if (isSubmittingRef.current) return; + isSubmittingRef.current = true; + setIsSubmitting(true); - // const payload = { - // ...formField, - // subject: - // formField.subject.trim() === '' && formField.via === 'sms' - // ? 'SMS Notification' - // : formField.subject - // }; + try { + const response = await PostData(`${API_URL_NOTIFICATION}/send`, formField); + // console.log('send response :', response); - const response = await PostData(`${API_URL_NOTIFICATION}/send`, formField); - // console.log('send response :', response); - - if (response?.status) { - handleAddDialog(false); - resetForm(); - reload(); - toast.success('Notification Send Successfully!'); - const createActivity = { - module: 'Manage Notification', - description: `Send New Notification => ${formField.via}`, - action: 'C' - }; - doSaveLogActivity(createActivity); - } else { - toast.error('Failed to create notification'); - setAlert({ show: true, message: 'Failed to create notification. Please Try Again.' }); - } - }; + if (response?.status) { + handleAddDialog(false); + resetForm(); + reload(); + toast.success('Notification Send Successfully!'); + const createActivity = { + module: 'Manage Notification', + description: `Send New Notification => ${formField.via}`, + action: 'C' + }; + doSaveLogActivity(createActivity); + } else { + toast.error('Failed to create notification'); + setAlert({ show: true, message: 'Failed to create notification. Please Try Again.' }); + } + } catch (err) { + toast.error('Something went wrong'); + console.log(err); + } finally { + isSubmittingRef.current = false; + setIsSubmitting(false); + } + }, + [PostData, formField, handleAddDialog, reload] + ); const getCustomerList = async (sorting: any) => { try { @@ -113,6 +121,8 @@ const AddDialog = () => { const handleSubmit = (e: React.FormEvent) => { e.preventDefault(); + if (isSubmittingRef.current) return; + if ( formField.all_customer.trim() === '' || formField.via.trim() === '' || @@ -132,6 +142,7 @@ const AddDialog = () => { return; } + setIsSubmitting(true); doCreateNotification(e); // console.log(formField); setAlert({ show: false, message: '' }); @@ -155,213 +166,223 @@ const AddDialog = () => { -
- {alert.show && ( - -

{alert.message}

-
- )} +
+ + {alert.show && ( + +

{alert.message}

+
+ )} -
-
- -
-
- -
-
- - - - - - - - - { - e.currentTarget.scrollTop += e.deltaY; - }} - > - No Customer found. - - {customers.map((customer) => { - const isSelected = formField.customers.includes(customer.id); - return ( - { - const customerId = customer.id; - setFormField((prev) => { - const isSelected = prev.customers.includes(customerId); - return { - ...prev, - customers: isSelected - ? prev.customers.filter((id) => id !== customerId) - : [...prev.customers, customerId] - }; - }); - }} - className={isSelected ? 'bg-accent font-semibold' : ''} - > - - {isSelected && } - {customer.username} - - - ); - })} - - - - - -
-
- -
-
- -
- {['info', 'promo'].map((type) => ( -
-
-
-
- -
- - - +
+
+ + + + + + + + + { + e.currentTarget.scrollTop += e.deltaY; + }} + > + No Customer found. + + {customers.map((customer) => { + const isSelected = formField.customers.includes(customer.id); + return ( + { + const customerId = customer.id; + setFormField((prev) => { + const isSelected = prev.customers.includes(customerId); + return { + ...prev, + customers: isSelected + ? prev.customers.filter((id) => id !== customerId) + : [...prev.customers, customerId] + }; + }); + }} + className={isSelected ? 'bg-accent font-semibold' : ''} + > + + {isSelected && } + {customer.username} + + + ); + })} + + + + +
-
-
-
- - +
+
+ +
+ {['info', 'promo'].map((type) => ( + + ))} +
+
-
-
-
- -