3719 |
stevensc |
1 |
import React, { useEffect, useState } from 'react';
|
|
|
2 |
import { Link } from 'react-router-dom';
|
|
|
3 |
import { useDispatch } from 'react-redux';
|
|
|
4 |
import { Grid, IconButton, List, ListItem, ListItemButton, ListItemText } from '@mui/material';
|
|
|
5 |
import Delete from '@mui/icons-material/Delete';
|
|
|
6 |
|
|
|
7 |
import { axios } from '@utils';
|
|
|
8 |
import { useFetch } from '@hooks';
|
|
|
9 |
import { addNotification } from '@store/notification/notification.actions';
|
|
|
10 |
|
|
|
11 |
import Widget from '@components/UI/Widget';
|
|
|
12 |
import Options from '@components/UI/Option';
|
|
|
13 |
import Spinner from '@components/UI/Spinner';
|
|
|
14 |
import EmptySection from '@components/UI/EmptySection';
|
|
|
15 |
import ProfileInfo from '@components/widgets/default/ProfileWidget';
|
|
|
16 |
import ConfirmModal from '@components/modals/ConfirmModal';
|
|
|
17 |
|
|
|
18 |
const NotificationsPage = () => {
|
|
|
19 |
const { data: user } = useFetch('/helpers/menu');
|
|
|
20 |
const { data: fetchedNotifications, isLoading } = useFetch('/notifications', []);
|
|
|
21 |
const [notifications, setNotifications] = useState([]);
|
|
|
22 |
const [deleteModalState, setDeleteModalState] = useState({
|
|
|
23 |
show: false,
|
|
|
24 |
type: 'multiple',
|
|
|
25 |
url: null
|
|
|
26 |
});
|
|
|
27 |
const dispatch = useDispatch();
|
|
|
28 |
|
|
|
29 |
// useFetch('/notifications/mark-all-read') POST request needed
|
|
|
30 |
|
|
|
31 |
const deleteAllNotifications = () => {
|
|
|
32 |
axios
|
|
|
33 |
.post('/notifications/clear')
|
|
|
34 |
.then(({ data: { data, success } }) => {
|
|
|
35 |
if (!success) throw new Error('Error al borrar todas las notificaciones');
|
|
|
36 |
setNotifications([]);
|
|
|
37 |
dispatch(addNotification({ style: 'success', msg: data }));
|
|
|
38 |
})
|
|
|
39 |
.catch((err) => {
|
|
|
40 |
dispatch(addNotification({ style: 'danger', msg: err.message }));
|
|
|
41 |
});
|
|
|
42 |
};
|
|
|
43 |
|
|
|
44 |
const deleteNotification = (url) => {
|
|
|
45 |
axios
|
|
|
46 |
.post(url)
|
|
|
47 |
.then(({ data: { data, success } }) => {
|
|
|
48 |
if (!success) throw new Error('Error al borrar la notificacion');
|
|
|
49 |
const newNotifications = notifications.filter(({ link_delete }) => link_delete !== url);
|
|
|
50 |
setNotifications(newNotifications);
|
|
|
51 |
dispatch(addNotification({ style: 'success', msg: data }));
|
|
|
52 |
})
|
|
|
53 |
.catch((err) => {
|
|
|
54 |
dispatch(addNotification({ style: 'danger', msg: err.message }));
|
|
|
55 |
});
|
|
|
56 |
};
|
|
|
57 |
|
|
|
58 |
const handleDelete = (url) => {
|
|
|
59 |
setDeleteModalState({
|
|
|
60 |
show: true,
|
|
|
61 |
type: url ? 'single' : 'multiple',
|
|
|
62 |
url
|
|
|
63 |
});
|
|
|
64 |
};
|
|
|
65 |
|
|
|
66 |
const handleDeleteAccept = () => {
|
|
|
67 |
if (deleteModalState.type === 'multiple') {
|
|
|
68 |
deleteAllNotifications();
|
|
|
69 |
} else {
|
|
|
70 |
deleteNotification(deleteModalState.url);
|
|
|
71 |
}
|
|
|
72 |
setDeleteModalState({
|
|
|
73 |
show: false,
|
|
|
74 |
type: 'multiple'
|
|
|
75 |
});
|
|
|
76 |
};
|
|
|
77 |
|
|
|
78 |
const handleDeleteCancel = () => {
|
|
|
79 |
setDeleteModalState({
|
|
|
80 |
show: false,
|
|
|
81 |
type: 'multiple'
|
|
|
82 |
});
|
|
|
83 |
};
|
|
|
84 |
|
|
|
85 |
useEffect(() => {
|
|
|
86 |
if (fetchedNotifications) {
|
|
|
87 |
setNotifications(fetchedNotifications);
|
|
|
88 |
}
|
|
|
89 |
}, [fetchedNotifications]);
|
|
|
90 |
|
|
|
91 |
return (
|
|
|
92 |
<>
|
|
|
93 |
<Grid container spacing={1}>
|
|
|
94 |
<Grid size={{ xs: 12, md: 4 }}>
|
|
|
95 |
<ProfileInfo {...user} />
|
|
|
96 |
</Grid>
|
|
|
97 |
<Grid size={{ xs: 12, md: 8 }}>
|
|
|
98 |
<Widget>
|
|
|
99 |
<Widget.Header
|
|
|
100 |
title='Notificaciones'
|
|
|
101 |
renderAction={() => (
|
|
|
102 |
<Options>
|
|
|
103 |
<Options.Item onClick={handleDelete}>Borrar notificaciones</Options.Item>
|
|
|
104 |
</Options>
|
|
|
105 |
)}
|
|
|
106 |
/>
|
|
|
107 |
<Widget.Body>
|
|
|
108 |
{isLoading && <Spinner />}
|
|
|
109 |
{notifications.length === 0 && !isLoading && (
|
|
|
110 |
<EmptySection message='No hay notificaciones' align='center' />
|
|
|
111 |
)}
|
|
|
112 |
|
|
|
113 |
<List sx={{ maxHeight: '60vh', overflow: 'auto' }}>
|
|
|
114 |
{notifications.map(({ link_delete, link, message, time_elapsed }) => (
|
|
|
115 |
<ListItem
|
|
|
116 |
key={link}
|
|
|
117 |
secondaryAction={
|
|
|
118 |
<IconButton onClick={() => handleDelete(link_delete)}>
|
|
|
119 |
<Delete />
|
|
|
120 |
</IconButton>
|
|
|
121 |
}
|
|
|
122 |
>
|
|
|
123 |
<ListItemButton LinkComponent={Link} to={link}>
|
|
|
124 |
<ListItemText primary={message} secondary={time_elapsed} />
|
|
|
125 |
</ListItemButton>
|
|
|
126 |
</ListItem>
|
|
|
127 |
))}
|
|
|
128 |
</List>
|
|
|
129 |
</Widget.Body>
|
|
|
130 |
</Widget>
|
|
|
131 |
</Grid>
|
|
|
132 |
</Grid>
|
|
|
133 |
<ConfirmModal
|
|
|
134 |
show={deleteModalState.show}
|
|
|
135 |
onClose={handleDeleteCancel}
|
|
|
136 |
onAccept={handleDeleteAccept}
|
|
|
137 |
/>
|
|
|
138 |
</>
|
|
|
139 |
);
|
|
|
140 |
};
|
|
|
141 |
|
|
|
142 |
export default NotificationsPage;
|