update manage notification

This commit is contained in:
Raja Oktafrianto
2025-04-17 13:30:41 +07:00
parent 9e67880a9e
commit 522951cfbb
2 changed files with 71 additions and 70 deletions

View File

@ -32,14 +32,14 @@ const API_URL_CUSTOMER = apiConfig.service_customer;
const API_URL_NOTIFICATION = apiConfig.service_notification;
const AddDialog = () => {
const parentRef = useRef<any | null>(null);
const { handleAddDialog, showAddDialog } = useManageNotificationContext();
const { reload } = useDataGrid();
const { GetData, PostData } = useCallApi();
const [alert, setAlert] = useState({ show: false, message: '' });
const initialState = {
customers: [],
customers: [] as string[],
all_customer: '',
type: '',
via: '',
subject: '',
@ -56,12 +56,25 @@ const AddDialog = () => {
const handleChange = (e: React.ChangeEvent<HTMLInputElement | HTMLTextAreaElement>) => {
setFormField({ ...formField, [e.target.name]: e.target.value });
if (e.target.name === 'all_customer' && e.target.value === 'true') {
setFormField((prev) => ({ ...prev, customers: [] }));
}
};
const doCreateNotification = async (e: React.FormEvent<HTMLFormElement>) => {
e.preventDefault();
const response = await PostData(`${API_URL_NOTIFICATION}/send`, formField);
console.log('API Response :', response);
const payload = {
...formField,
subject:
formField.subject.trim() === '' && formField.via === 'sms'
? 'SMS Notification'
: formField.subject
};
const response = await PostData(`${API_URL_NOTIFICATION}/send`, payload);
// console.log('send response :', response);
if (response?.status) {
handleAddDialog(false);
@ -80,22 +93,22 @@ const AddDialog = () => {
}
};
// const getCustomerList = async (sorting: any) => {
// try {
// sorting = sorting.length == 0 ? [{ id: 'name', desc: false }] : sorting;
// const response = await GetData(`${API_URL_CUSTOMER}/customer/list`, {
// limit: 100,
// page: 1,
// with_deleted: false,
// order_field: sorting[0].id,
// order_direction: sorting[0].desc == false ? 'ASC' : 'DESC'
// });
const getCustomerList = async (sorting: any) => {
try {
sorting = sorting.length == 0 ? [{ id: 'name', desc: false }] : sorting;
const response = await GetData(`${API_URL_CUSTOMER}/customer/list`, {
limit: 100,
page: 1,
with_deleted: false,
order_field: sorting[0].id,
order_direction: sorting[0].desc == false ? 'ASC' : 'DESC'
});
// setCustomers(response?.data.list);
// } catch (error) {
// console.error('Error fetching customer', error);
// }
// };
setCustomers(response?.data.list);
} catch (error) {
console.error('Error fetching customer', error);
}
};
const handleSubmit = (e: React.FormEvent<HTMLFormElement>) => {
e.preventDefault();
@ -103,21 +116,30 @@ const AddDialog = () => {
if (
formField.type.trim() === '' ||
formField.via.trim() === '' ||
formField.subject.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;
}
doCreateNotification(e);
// console.log(formField);
setAlert({ show: false, message: '' });
};
// useEffect(() => {
// getCustomerList([{ id: 'id', desc: false }]);
// }, []);
useEffect(() => {
getCustomerList([{ id: 'id', desc: false }]);
}, []);
useEffect(() => {
if (showAddDialog === false) {
@ -140,7 +162,7 @@ const AddDialog = () => {
</Alert>
)}
{/* <div className="w-full">
<div className="w-full">
<div className="flex items-baseline flex-wrap lg:flex-nowrap gap-2.5">
<label className="form-label flex items-center gap-1 max-w-56">
Send To<span className="text-red-500">*</span>
@ -149,20 +171,20 @@ const AddDialog = () => {
<label className="flex items-center space-x-2">
<input
type="radio"
name="sendTo"
value="all"
checked={formField.sendTo === 'all'}
name="all_customer"
value="true"
checked={formField.all_customer === 'true'}
onChange={handleChange}
className="accent-blue-600"
/>
<span>All Users</span>
<span>All Customers</span>
</label>
<label className="flex items-center space-x-2">
<input
type="radio"
name="sendTo"
value="selected"
checked={formField.sendTo === 'selected'}
name="all_customer"
value="false"
checked={formField.all_customer === 'false'}
onChange={handleChange}
className="accent-blue-600"
/>
@ -170,9 +192,9 @@ const AddDialog = () => {
</label>
</div>
</div>
</div> */}
</div>
{/* <div className="w-full">
<div className="w-full">
<div className="flex items-baseline flex-wrap lg:flex-nowrap gap-2.5">
<label className="form-label flex items-center gap-1 max-w-56">Customer</label>
<Popover open={open} onOpenChange={setOpen}>
@ -181,10 +203,12 @@ const AddDialog = () => {
type="button"
className="input col-span-5 text-left flex justify-between"
style={{ color: 'inherit' }}
disabled={formField.all_customer !== 'false'}
>
<span>
{customers.find((customer) => customer.id === formField.customers)
?.username || 'Select Customer'}
{formField.customers.length === 0
? 'Select Customers'
: `${formField.customers.length} Customers Selected`}
</span>
<ChevronDown className="w-4 h-4 opacity-70" />
</button>
@ -206,11 +230,16 @@ const AddDialog = () => {
key={customer.id}
value={customer.username}
onSelect={() => {
setFormField({
...formField,
customers: customer.id
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]
};
});
setOpen(false);
}}
>
{customer.username}
@ -222,7 +251,7 @@ const AddDialog = () => {
</PopoverContent>
</Popover>
</div>
</div> */}
</div>
<div className="w-full">
<div className="flex items-baseline flex-wrap lg:flex-nowrap gap-2.5">
@ -299,7 +328,7 @@ const AddDialog = () => {
placeholder="Enter Subject"
value={formField.subject}
onChange={handleChange}
disabled={formField.via !== 'email'}
disabled={formField.via !== 'email' && formField.via !== 'fcm'}
/>
</div>
</div>

View File

@ -101,34 +101,6 @@ const ManageNotifContextProvider = ({ children }: { children: React.ReactNode })
enableHiding: false,
cell: ({ row }) => moment(row.original.created_at).format('YYYY-MM-DD HH:mm:ss')
}
// {
// id: 'actions',
// header: ({ column }) => <DataGridColumnHeader title="Actions" column={column} />,
// meta: {
// headerClassName: 'w-[100px]',
// cellClassName: 'text-center'
// },
// cell: (data: any) => {
// const row = data.row.original;
// return (
// <>
// <button
// className="btn btn-sm btn-icon btn-clear btn-light"
// onClick={() => handleEditDialog(true, row.id)}
// >
// <KeenIcon icon="notepad-edit" />
// </button>
// <button
// className="btn btn-sm btn-icon btn-clear btn-light"
// onClick={() => handleDeleteDialog(true, row.id)}
// >
// <KeenIcon icon="trash" />
// </button>
// </>
// );
// }
// }
],
[handleEditDialog, handleDeleteDialog]
);
@ -146,7 +118,7 @@ const ManageNotifContextProvider = ({ children }: { children: React.ReactNode })
order_direction: sorting[0].desc == false ? 'ASC' : 'DESC',
filter: JSON.stringify(filter)
});
console.log('API Response notif: ', response);
// console.log('API Response notif: ', response);
return { data: response?.data.list, totalCount: response?.data.total_count };
} catch (error) {
console.error('Error fetching notification', error);