Proyectos de Subversion LeadersLinked - SPA

Rev

Rev 2993 | Rev 2995 | Ir a la última revisión | | Comparar con el anterior | Ultima modificación | Ver Log |

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