diff --git a/src/api/notification.jsx b/src/api/notification.jsx index fa8efdf..7a05d71 100644 --- a/src/api/notification.jsx +++ b/src/api/notification.jsx @@ -91,6 +91,25 @@ const searchData = async (queryParams) => { return response.data; }; +// Reminder Monthly +const reminderMonthly = async (id, queryParams) => { + const response = await SendRequest({ + method: 'post', + prefix: `notifikasi-wa/reminder-monthly/${id}`, + params: queryParams, + }); + return response.data; +}; + +const reminderCustom= async (id, queryParams) => { + const response = await SendRequest({ + method: 'post', + prefix: `notifikasi-wa/reminder-custom/${id}`, + params: queryParams, + }); + return response.data; +}; + export { getAllNotification, getNotificationById, @@ -102,4 +121,6 @@ export { resendChatByUser, resendChatAllUser, searchData, + reminderMonthly, + reminderCustom, }; diff --git a/src/components/Global/ToastNotif.jsx b/src/components/Global/ToastNotif.jsx index 2cdeaf9..305291a 100644 --- a/src/components/Global/ToastNotif.jsx +++ b/src/components/Global/ToastNotif.jsx @@ -20,6 +20,19 @@ const NotifOk = ({ icon, title, message }) => { }); }; +const NotifSmall = ({ icon, title, message }) => { + Swal.fire({ + toast: true, + position: 'top-end', + icon: icon, + title: title, + text: message, + showConfirmButton: false, + timer: 2000, + timerProgressBar: true, + }); +}; + const NotifConfirmDialog = ({ icon, title, @@ -67,4 +80,4 @@ const QuestionConfirmSubmit = ({ icon, title, message, onConfirm, onCancel }) => }); }; -export { NotifAlert, NotifOk, NotifConfirmDialog, QuestionConfirmSubmit }; +export { NotifAlert, NotifOk, NotifSmall, NotifConfirmDialog, QuestionConfirmSubmit }; diff --git a/src/pages/master/device/component/DetailDevice.jsx b/src/pages/master/device/component/DetailDevice.jsx index 8c9613c..298ac94 100644 --- a/src/pages/master/device/component/DetailDevice.jsx +++ b/src/pages/master/device/component/DetailDevice.jsx @@ -10,8 +10,9 @@ import { Select, DatePicker, } from 'antd'; -import { NotifAlert, NotifOk } from '../../../../components/Global/ToastNotif'; +import { NotifAlert, NotifOk, NotifSmall } from '../../../../components/Global/ToastNotif'; import { createDevice, updateDevice } from '../../../../api/master-device'; +import { reminderMonthly, reminderCustom } from '../../../../api/notification'; import { getAllBrands } from '../../../../api/master-brand'; import { validateRun } from '../../../../Utils/validate'; import { toApiDateFormatter } from '../../../../components/Global/Formatter'; @@ -40,6 +41,7 @@ const DetailDevice = (props) => { reminder_at_monthly: null, start_date: null, end_date: null, + period: null, }; const [formData, setFormData] = useState(defaultData); @@ -85,7 +87,7 @@ const DetailDevice = (props) => { listen_channel: formData.listen_channel, listen_channel_reminder: formData.listen_channel_reminder, reminder_at: formData.reminder_at, - reminder_at_monthly: formData.reminder_at_monthly, + reminder_at_monthly: formData.period !== 'custom' ? formData.reminder_at_monthly : null, start_date: formData.start_date, end_date: formData.end_date, }; @@ -96,6 +98,13 @@ const DetailDevice = (props) => { ? await updateDevice(formData.device_id, payload) : await createDevice(payload); + const response2 = formData.period !== 'custom' + ? await updateDevice(formData.device_id, { reminder_at_monthly: formData.reminder_at_monthly,}) + : await updateDevice(formData.device_id, { + start_date: formData.start_date, + end_date: formData.end_date, + }); + // Check if response is successful if (response && (response.statusCode === 200 || response.statusCode === 201)) { const deviceName = response.data?.device_name || formData.device_name; @@ -116,6 +125,25 @@ const DetailDevice = (props) => { message: response?.message || 'Terjadi kesalahan saat menyimpan data.', }); } + + // Check if response Reminder At(Monthly) or Period are successful + const tipe = formData.period !== 'custom' ? 'Reminder At(Monthly)' : 'Period'; + if (response2 && (response2.statusCode === 200 || response2.statusCode === 201)) { + NotifSmall({ + icon: 'success', + title: 'Berhasil', + message: `${tipe} berhasil disimpan`, + }); + } else { + NotifSmall({ + icon: 'error', + title: 'Gagal', + message: + response?.message || + `Terjadi kesalahan saat menyimpan ${tipe}`, + }); + } + } catch (error) { console.error('Save Device Error:', error); NotifAlert({ @@ -178,10 +206,6 @@ const DetailDevice = (props) => { } }; - const [period, setPeriod] = useState({ - period: undefined, - }); - useEffect(() => { if (formData.start_date && formData.end_date) { const start = dayjs(formData.start_date); @@ -197,37 +221,40 @@ const DetailDevice = (props) => { periodValue = '6'; } - setPeriod({ + setFormData((prev) => ({ + ...prev, period: periodValue, - }); + })); } }, [formData.start_date, formData.end_date]); const handleMonthly = (field, value) => { - setPeriod((prev) => ({ + setFormData((prev) => { + const data = { ...prev, [field]: value, - })); + }; if (value !== 'custom') { const months = Number(value); const startDate = dayjs().startOf('day'); - const endDate = startDate.add(months, 'month').subtract(1, 'day').endOf('day'); + const endDate = startDate + .add(months, 'month') + .subtract(1, 'day') + .endOf('day'); - setFormData((prev) => ({ - ...prev, - start_date: startDate.toISOString(), - end_date: endDate.toISOString(), - })); + data.start_date = startDate.toISOString(); + data.end_date = endDate.toISOString(); + data.reminder_at_monthly = null; } else { - setFormData((prev) => ({ - ...prev, - start_date: null, - end_date: null, - })); + data.start_date = null; + data.end_date = null; } - }; + + return data; + }); +}; useEffect(() => { if (props.showModal && (props.actionMode === 'add' || props.actionMode === 'edit')) { @@ -475,7 +502,7 @@ const DetailDevice = (props) => { * - {period.period === 'custom' && ( + {formData.period === 'custom' && (
{ Reminder At(Monthly)
{ />