diff --git a/src/api/notification.jsx b/src/api/notification.jsx index 67daa9a..3c526ed 100644 --- a/src/api/notification.jsx +++ b/src/api/notification.jsx @@ -46,6 +46,15 @@ const getNotificationLogByNotificationId = async (notificationId) => { return response.data; }; +// update is_read status +const updateIsRead = async (notificationId) => { + const response = await SendRequest({ + method: 'put', + prefix: `notification/${notificationId}`, + }); + return response.data; +}; + // Resend notification to specific user const resendNotificationToUser = async (notificationId, userId) => { const response = await SendRequest({ @@ -55,11 +64,32 @@ const resendNotificationToUser = async (notificationId, userId) => { return response.data; }; +// Resend Chat by User +const resendChatByUser = async (notificationId, userPhone) => { + const response = await SendRequest({ + method: 'post', + prefix: `notification-user/resend/${notificationId}/${userPhone}`, + }); + return response.data; +}; + +// Resend Chat All User +const resendChatAllUser = async (notificationId) => { + const response = await SendRequest({ + method: 'post', + prefix: `notification/resend/${notificationId}`, + }); + return response.data; +}; + export { getAllNotification, getNotificationById, getNotificationDetail, createNotificationLog, getNotificationLogByNotificationId, + updateIsRead, resendNotificationToUser, + resendChatByUser, + resendChatAllUser, }; diff --git a/src/pages/notification/component/ListNotification.jsx b/src/pages/notification/component/ListNotification.jsx index 91c0ecf..5a590ed 100644 --- a/src/pages/notification/component/ListNotification.jsx +++ b/src/pages/notification/component/ListNotification.jsx @@ -42,6 +42,8 @@ import { getAllNotification, getNotificationLogByNotificationId, getNotificationDetail, + resendChatByUser, + resendChatAllUser, } from '../../../api/notification'; const { Text, Paragraph, Link: AntdLink } = Typography; @@ -65,6 +67,7 @@ const transformNotificationData = (apiData) => { : 'N/A', location: item.plant_sub_section_name || item.device_location || 'Location not specified', details: item.device_name || '-', + errId: item.notification_error_id || 0, link: `/verification-sparepart/${item.notification_error_id}`, // Dummy URL untuk verifikasi spare part subsection: item.plant_sub_section_name || 'N/A', isRead: item.is_read, @@ -195,9 +198,9 @@ const ListNotification = memo(function ListNotification(props) { content: `Are you sure you want to resend the notification for "${notification.title}"?`, okText: 'Resend', cancelText: 'Cancel', - onOk() { + async onOk() { console.log('Resending notification:', notification.id); - + await resendChatAllUser(notification.errId); message.success( `Notification for "${notification.title}" has been resent successfully.` ); @@ -605,10 +608,11 @@ const ListNotification = memo(function ListNotification(props) { type="primary" ghost icon={} - onClick={() => { - message.info( - 'Resend feature is not available yet. This feature is still under development.' - ); + onClick={async () => { + await resendChatByUser(user.id, user.phone); + // message.info( + // 'Resend feature is not available yet. This feature is still under development.' + // ); }} > Resend diff --git a/src/pages/notificationDetail/IndexNotificationDetail.jsx b/src/pages/notificationDetail/IndexNotificationDetail.jsx index a3eb466..0db8009 100644 --- a/src/pages/notificationDetail/IndexNotificationDetail.jsx +++ b/src/pages/notificationDetail/IndexNotificationDetail.jsx @@ -38,6 +38,7 @@ import { getNotificationDetail, createNotificationLog, getNotificationLogByNotificationId, + updateIsRead, resendNotificationToUser, } from '../../api/notification'; @@ -234,6 +235,8 @@ const NotificationDetailTab = (props) => { // Fetch using the actual API const response = await getNotificationDetail(notificationId); + // Fetch using the actual API + const resUpdate = await updateIsRead(notificationId); if (response && response.data) { const transformedData = transformNotificationData(response.data); @@ -497,59 +500,95 @@ const NotificationDetailTab = (props) => { loading={user.loading} onClick={async (e) => { e.stopPropagation(); - const userId = parseInt(user.id); + const userId = parseInt( + user.id + ); try { // Update user status to show loading - const updatedUsers = notification.users.map(u => - u.notification_error_user_id === userId - ? { ...u, loading: true } - : u - ); + const updatedUsers = + notification.users.map( + (u) => + u.notification_error_user_id === + userId + ? { + ...u, + loading: true, + } + : u + ); setNotification({ ...notification, - users: updatedUsers + users: updatedUsers, }); // Call the resend API - const response = await resendNotificationToUser( - notification.notification_error_id, - userId - ); + const response = + await resendNotificationToUser( + notification.notification_error_id, + userId + ); - if (response && response.statusCode === 200) { - message.success(`Notification resent to ${user.name}`); + if ( + response && + response.statusCode === + 200 + ) { + message.success( + `Notification resent to ${user.name}` + ); // Update user status - const updatedUsersAfterSuccess = notification.users.map(u => - u.notification_error_user_id === userId - ? { - ...u, - is_send: true, - status: 'sent', - loading: false - } - : { ...u, loading: false } - ); + const updatedUsersAfterSuccess = + notification.users.map( + (u) => + u.notification_error_user_id === + userId + ? { + ...u, + is_send: true, + status: 'sent', + loading: false, + } + : { + ...u, + loading: false, + } + ); setNotification({ ...notification, - users: updatedUsersAfterSuccess + users: updatedUsersAfterSuccess, }); } else { - throw new Error(response?.message || 'Failed to resend notification'); + throw new Error( + response?.message || + 'Failed to resend notification' + ); } } catch (error) { - console.error('Error resending notification:', error); - message.error(error.message || 'Failed to resend notification'); + console.error( + 'Error resending notification:', + error + ); + message.error( + error.message || + 'Failed to resend notification' + ); // Reset loading state - const resetUsers = notification.users.map(u => - u.notification_error_user_id === userId - ? { ...u, loading: false } - : u - ); + const resetUsers = + notification.users.map( + (u) => + u.notification_error_user_id === + userId + ? { + ...u, + loading: false, + } + : u + ); setNotification({ ...notification, - users: resetUsers + users: resetUsers, }); } }}