feat: integration notification functionality and user history fetching

This commit is contained in:
2025-12-23 22:10:11 +07:00
parent 797f6c2383
commit beb8ccbaee
3 changed files with 199 additions and 93 deletions

View File

@@ -38,6 +38,7 @@ import {
getNotificationDetail,
createNotificationLog,
getNotificationLogByNotificationId,
resendNotificationToUser,
} from '../../api/notification';
const { Content } = Layout;
@@ -94,38 +95,21 @@ const transformNotificationData = (apiData) => {
device_location: apiData.device_location,
brand_name: apiData.brand_name,
},
users: apiData.users || [],
};
};
// Dummy data baru untuk user history
const getDummyUsers = (notification) => {
if (!notification) return [];
return [
{
id: '1',
name: 'John Doe',
phone: '081234567890',
status: 'delivered',
},
{
id: '2',
name: 'Jane Smith',
phone: '082345678901',
status: 'sent',
},
{
id: '3',
name: 'Bob Johnson',
phone: '083456789012',
status: 'failed',
},
{
id: '4',
name: 'Alice Brown',
phone: '084567890123',
status: 'delivered',
},
];
// Function to get actual users from notification data
const getUsersFromNotification = (notification) => {
if (!notification || !notification.users) return [];
return notification.users.map((user) => ({
id: user.notification_error_user_id.toString(),
name: user.contact_name,
phone: user.contact_phone,
status: user.is_send ? 'sent' : 'pending',
loading: user.loading || false,
}));
};
const getStatusTag = (status) => {
@@ -469,7 +453,7 @@ const NotificationDetailTab = (props) => {
size={2}
style={{ width: '100%' }}
>
{getDummyUsers(notification).map((user) => (
{getUsersFromNotification(notification).map((user) => (
<Card
key={user.id}
size="small"
@@ -510,11 +494,64 @@ const NotificationDetailTab = (props) => {
type="primary"
icon={<SendOutlined />}
size="small"
onClick={(e) => {
loading={user.loading}
onClick={async (e) => {
e.stopPropagation();
console.log(
`Resend to ${user.name}`
);
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
);
setNotification({
...notification,
users: updatedUsers
});
// Call the resend API
const response = await resendNotificationToUser(
notification.notification_error_id,
userId
);
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 }
);
setNotification({
...notification,
users: updatedUsersAfterSuccess
});
} else {
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');
// Reset loading state
const resetUsers = notification.users.map(u =>
u.notification_error_user_id === userId
? { ...u, loading: false }
: u
);
setNotification({
...notification,
users: resetUsers
});
}
}}
>
Resend