Compare commits

...

2 Commits

3 changed files with 88 additions and 27 deletions

View File

@@ -91,6 +91,25 @@ const searchData = async (queryParams) => {
return response.data; 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 { export {
getAllNotification, getAllNotification,
getNotificationById, getNotificationById,
@@ -102,4 +121,6 @@ export {
resendChatByUser, resendChatByUser,
resendChatAllUser, resendChatAllUser,
searchData, searchData,
reminderMonthly,
reminderCustom,
}; };

View File

@@ -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 = ({ const NotifConfirmDialog = ({
icon, icon,
title, title,
@@ -67,4 +80,4 @@ const QuestionConfirmSubmit = ({ icon, title, message, onConfirm, onCancel }) =>
}); });
}; };
export { NotifAlert, NotifOk, NotifConfirmDialog, QuestionConfirmSubmit }; export { NotifAlert, NotifOk, NotifSmall, NotifConfirmDialog, QuestionConfirmSubmit };

View File

@@ -10,8 +10,9 @@ import {
Select, Select,
DatePicker, DatePicker,
} from 'antd'; } 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 { createDevice, updateDevice } from '../../../../api/master-device';
import { reminderMonthly, reminderCustom } from '../../../../api/notification';
import { getAllBrands } from '../../../../api/master-brand'; import { getAllBrands } from '../../../../api/master-brand';
import { validateRun } from '../../../../Utils/validate'; import { validateRun } from '../../../../Utils/validate';
import { toApiDateFormatter } from '../../../../components/Global/Formatter'; import { toApiDateFormatter } from '../../../../components/Global/Formatter';
@@ -40,6 +41,7 @@ const DetailDevice = (props) => {
reminder_at_monthly: null, reminder_at_monthly: null,
start_date: null, start_date: null,
end_date: null, end_date: null,
period: null,
}; };
const [formData, setFormData] = useState(defaultData); const [formData, setFormData] = useState(defaultData);
@@ -85,7 +87,7 @@ const DetailDevice = (props) => {
listen_channel: formData.listen_channel, listen_channel: formData.listen_channel,
listen_channel_reminder: formData.listen_channel_reminder, listen_channel_reminder: formData.listen_channel_reminder,
reminder_at: formData.reminder_at, 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, start_date: formData.start_date,
end_date: formData.end_date, end_date: formData.end_date,
}; };
@@ -96,6 +98,13 @@ const DetailDevice = (props) => {
? await updateDevice(formData.device_id, payload) ? await updateDevice(formData.device_id, payload)
: await createDevice(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 // Check if response is successful
if (response && (response.statusCode === 200 || response.statusCode === 201)) { if (response && (response.statusCode === 200 || response.statusCode === 201)) {
const deviceName = response.data?.device_name || formData.device_name; const deviceName = response.data?.device_name || formData.device_name;
@@ -116,6 +125,25 @@ const DetailDevice = (props) => {
message: response?.message || 'Terjadi kesalahan saat menyimpan data.', 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) { } catch (error) {
console.error('Save Device Error:', error); console.error('Save Device Error:', error);
NotifAlert({ NotifAlert({
@@ -178,10 +206,6 @@ const DetailDevice = (props) => {
} }
}; };
const [period, setPeriod] = useState({
period: undefined,
});
useEffect(() => { useEffect(() => {
if (formData.start_date && formData.end_date) { if (formData.start_date && formData.end_date) {
const start = dayjs(formData.start_date); const start = dayjs(formData.start_date);
@@ -197,37 +221,40 @@ const DetailDevice = (props) => {
periodValue = '6'; periodValue = '6';
} }
setPeriod({ setFormData((prev) => ({
...prev,
period: periodValue, period: periodValue,
}); }));
} }
}, [formData.start_date, formData.end_date]); }, [formData.start_date, formData.end_date]);
const handleMonthly = (field, value) => { const handleMonthly = (field, value) => {
setPeriod((prev) => ({ setFormData((prev) => {
const data = {
...prev, ...prev,
[field]: value, [field]: value,
})); };
if (value !== 'custom') { if (value !== 'custom') {
const months = Number(value); const months = Number(value);
const startDate = dayjs().startOf('day'); 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) => ({ data.start_date = startDate.toISOString();
...prev, data.end_date = endDate.toISOString();
start_date: startDate.toISOString(), data.reminder_at_monthly = null;
end_date: endDate.toISOString(),
}));
} else { } else {
setFormData((prev) => ({ data.start_date = null;
...prev, data.end_date = null;
start_date: null,
end_date: null,
}));
} }
};
return data;
});
};
useEffect(() => { useEffect(() => {
if (props.showModal && (props.actionMode === 'add' || props.actionMode === 'edit')) { if (props.showModal && (props.actionMode === 'add' || props.actionMode === 'edit')) {
@@ -475,7 +502,7 @@ const DetailDevice = (props) => {
<Text style={{ color: 'red' }}> *</Text> <Text style={{ color: 'red' }}> *</Text>
<Select <Select
value={period.period} value={formData.period}
onChange={(value) => handleMonthly('period', value)} onChange={(value) => handleMonthly('period', value)}
placeholder="Select Period" placeholder="Select Period"
style={{ width: '100%' }} style={{ width: '100%' }}
@@ -486,7 +513,7 @@ const DetailDevice = (props) => {
<Select.Option value="custom">Custom</Select.Option> <Select.Option value="custom">Custom</Select.Option>
</Select> </Select>
</div> </div>
{period.period === 'custom' && ( {formData.period === 'custom' && (
<div style={{ display: 'flex', gap: 8, marginBottom: 12 }}> <div style={{ display: 'flex', gap: 8, marginBottom: 12 }}>
<DatePicker <DatePicker
placeholder="Start Date" placeholder="Start Date"
@@ -525,7 +552,7 @@ const DetailDevice = (props) => {
<Text strong>Reminder At(Monthly)</Text> <Text strong>Reminder At(Monthly)</Text>
<div style={{ display: 'flex', gap: 8, alignItems: 'center' }}> <div style={{ display: 'flex', gap: 8, alignItems: 'center' }}>
<DatePicker <DatePicker
disabled={period.period === 'custom'} disabled={formData.period === 'custom'}
value={ value={
formData.reminder_at_monthly formData.reminder_at_monthly
? dayjs(formData.reminder_at_monthly) // ✅ langsung parse ISO ? dayjs(formData.reminder_at_monthly) // ✅ langsung parse ISO
@@ -543,7 +570,7 @@ const DetailDevice = (props) => {
/> />
<Button <Button
disabled={period.period === 'custom'} disabled={formData.period === 'custom'}
danger danger
onClick={() => onClick={() =>
setFormData((prev) => ({ setFormData((prev) => ({