) => {
e.preventDefault();
-
- if (isSubmittingRef.current) return;
-
- if (
- formField.all_customer.trim() === '' ||
- formField.via.trim() === '' ||
- formField.content.trim() === ''
- ) {
- setAlert({ show: true, message: 'Please fill all required fields.' });
- return;
- }
-
- if (formField.via === 'email' && formField.subject.trim() === '') {
- setAlert({ show: true, message: 'Subject is required for E-Mail' });
- return;
- }
-
- if (formField.via === 'fcm' && formField.subject.trim() === '') {
- setAlert({ show: true, message: 'Subject is required form FCM' });
- return;
- }
-
- setIsSubmitting(true);
+ if (!validateFormNotification(formField, setErrors)) return;
doCreateNotification(e);
- // console.log(formField);
- setAlert({ show: false, message: '' });
};
useEffect(() => {
@@ -179,7 +158,11 @@ const AddDialog = () => {
-
+ {errors.all_customer && (
+
+
+ {errors.all_customer}
+
+
+ )}
@@ -277,7 +267,11 @@ const AddDialog = () => {
-
+ {errors.type && (
+
+
+ {errors.type}
+
+
+ )}
@@ -300,7 +301,11 @@ const AddDialog = () => {
-
+ {errors.via && (
+
+
+ {errors.via}
+
+
+ )}
@@ -358,13 +370,20 @@ const AddDialog = () => {
Content*
+ {errors.content && (
+
+
+ {errors.content}
+
+
+ )}
diff --git a/src/pages/notification/blocks/Types.ts b/src/pages/notification/blocks/Types.ts
new file mode 100644
index 0000000..bbf1fd8
--- /dev/null
+++ b/src/pages/notification/blocks/Types.ts
@@ -0,0 +1,55 @@
+import { toast } from 'sonner';
+
+export interface selectedNotification {
+ customers: string[];
+ all_customer: string;
+ type: string;
+ via: string;
+ subject: string;
+ content: string;
+ id: string;
+ created_by: string;
+ created_at: string;
+}
+
+export const initialStateNotification: selectedNotification = {
+ customers: [],
+ all_customer: '',
+ type: '',
+ via: '',
+ subject: '',
+ content: '',
+ id: '',
+ created_by: '',
+ created_at: ''
+};
+
+export const validateFormNotification = (
+ formField: typeof initialStateNotification,
+ setErrors: React.Dispatch>>
+) => {
+ const requiredFields = [
+ { key: 'all_customer', label: 'Send To' },
+ { key: 'type', label: 'Type' },
+ { key: 'via', label: 'Via' },
+ { key: 'content', label: 'Content' }
+ ];
+
+ const newErrors: Record = {};
+ let isValid = true;
+
+ requiredFields.forEach(({ key, label }) => {
+ if (
+ formField[key as keyof typeof formField] === '' ||
+ formField[key as keyof typeof formField] === null ||
+ formField[key as keyof typeof formField] === undefined
+ ) {
+ newErrors[key] = `${label} is required`;
+ toast.error(`${label} is required`);
+ isValid = false;
+ }
+ });
+
+ setErrors(newErrors);
+ return isValid;
+};
diff --git a/src/pages/notification/hooks/ManageNotificationContext.tsx b/src/pages/notification/hooks/ManageNotificationContext.tsx
index aad0a33..d43702d 100644
--- a/src/pages/notification/hooks/ManageNotificationContext.tsx
+++ b/src/pages/notification/hooks/ManageNotificationContext.tsx
@@ -7,17 +7,6 @@ import { ListToolBar } from '../blocks/ListToolbar';
import { useCallApi } from '@/hooks';
import moment from 'moment';
-interface SelectedNotification {
- all_customers: string;
- customer_name: string;
- id: string;
- content: string;
- subject: string;
- type: string;
- via: string;
- created_at: string;
-}
-
interface ContextProps {
showAddDialog: boolean;
handleAddDialog: (show: boolean) => void;
@@ -197,4 +186,3 @@ const ManageNotifContextProvider = ({ children }: { children: React.ReactNode })
};
export { ManageNotifContext, ManageNotifContextProvider };
-export type { SelectedNotification };