fix(notif): fixing for notification Monthly
This commit is contained in:
@@ -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) => {
|
||||
<Text style={{ color: 'red' }}> *</Text>
|
||||
|
||||
<Select
|
||||
value={period.period}
|
||||
value={formData.period}
|
||||
onChange={(value) => handleMonthly('period', value)}
|
||||
placeholder="Select Period"
|
||||
style={{ width: '100%' }}
|
||||
@@ -486,7 +513,7 @@ const DetailDevice = (props) => {
|
||||
<Select.Option value="custom">Custom</Select.Option>
|
||||
</Select>
|
||||
</div>
|
||||
{period.period === 'custom' && (
|
||||
{formData.period === 'custom' && (
|
||||
<div style={{ display: 'flex', gap: 8, marginBottom: 12 }}>
|
||||
<DatePicker
|
||||
placeholder="Start Date"
|
||||
@@ -525,7 +552,7 @@ const DetailDevice = (props) => {
|
||||
<Text strong>Reminder At(Monthly)</Text>
|
||||
<div style={{ display: 'flex', gap: 8, alignItems: 'center' }}>
|
||||
<DatePicker
|
||||
disabled={period.period === 'custom'}
|
||||
disabled={formData.period === 'custom'}
|
||||
value={
|
||||
formData.reminder_at_monthly
|
||||
? dayjs(formData.reminder_at_monthly) // ✅ langsung parse ISO
|
||||
@@ -543,7 +570,7 @@ const DetailDevice = (props) => {
|
||||
/>
|
||||
|
||||
<Button
|
||||
disabled={period.period === 'custom'}
|
||||
disabled={formData.period === 'custom'}
|
||||
danger
|
||||
onClick={() =>
|
||||
setFormData((prev) => ({
|
||||
|
||||
Reference in New Issue
Block a user