Proyectos de Subversion LeadersLinked - SPA

Rev

Rev 2806 | Rev 2993 | 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 = () => {
2992 stevensc 25
  const { data: notifications } = useFetch('/notifications')
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
  const { image, fullName, country, visits, connections } = user
5 stevensc 32
 
2992 stevensc 33
  useFetch('/notifications/mark-all-read')
5 stevensc 34
 
35
  const deleteAllNotifications = () => {
36
    axios
37
      .post('/notifications/clear')
670 stevensc 38
      .then(({ data: responseData }) => {
39
        const { data, success } = responseData
40
 
5 stevensc 41
        if (!success) {
670 stevensc 42
          throw new Error(data)
5 stevensc 43
        }
44
 
45
        dispatch(addNotification({ style: 'success', msg: data }))
46
        setNotifications([])
47
      })
48
      .catch((err) => {
670 stevensc 49
        dispatch(addNotification({ style: 'danger', msg: err.message }))
5 stevensc 50
      })
51
  }
52
 
53
  const deleteNotification = (url) => {
54
    axios
55
      .post(url)
670 stevensc 56
      .then(({ data: responseData }) => {
57
        const { data, success } = responseData
5 stevensc 58
 
59
        if (!success) {
670 stevensc 60
          throw new Error(data)
5 stevensc 61
        }
62
 
63
        const newNotifications = notifications.filter(
64
          ({ link_delete }) => link_delete !== url
65
        )
66
 
67
        setNotifications(newNotifications)
68
        dispatch(addNotification({ style: 'success', msg: data }))
69
      })
70
      .catch((err) => {
670 stevensc 71
        dispatch(addNotification({ style: 'danger', msg: err.message }))
5 stevensc 72
      })
73
  }
74
 
75
  const toggleConfirmModal = () => {
76
    setConfirmModalShow(!confirmModalShow)
77
  }
78
 
79
  return (
80
    <>
2806 stevensc 81
      <Grid container spacing={2}>
82
        <Grid item xs={12} md={4}>
83
          <ProfileInfo
84
            image={image}
85
            name={fullName}
86
            visits={visits}
87
            country={country}
88
            connections={connections}
89
          />
90
        </Grid>
91
        <Grid item xs={12} md={8}>
2992 stevensc 92
          <Widget>
93
            <Widget.Header
94
              title='Notificaciones'
95
              renderAction={() => (
96
                <Options>
97
                  <Options.Item onClick={toggleConfirmModal}>
98
                    Borrar notificaciones
99
                  </Options.Item>
100
                </Options>
101
              )}
102
            />
103
            <Widget.Body>
104
              {notifications.length === 0 && (
2806 stevensc 105
                <EmptySection message='No hay notificaciones' align='center' />
106
              )}
2992 stevensc 107
 
108
              <List>
109
                {notifications.map(({ link, message, time_elapsed }) => (
110
                  <ListItem
111
                    key={link}
112
                    secondaryAction={
113
                      <IconButton onClick={deleteNotification}>
114
                        <Delete />
115
                      </IconButton>
116
                    }
117
                  >
118
                    <ListItemButton LinkComponent={Link} to={link}>
119
                      <ListItemText
120
                        primary={message}
121
                        secondary={time_elapsed}
122
                      />
123
                    </ListItemButton>
124
                  </ListItem>
125
                ))}
126
              </List>
127
            </Widget.Body>
128
          </Widget>
1469 stevensc 129
        </Grid>
2806 stevensc 130
      </Grid>
1469 stevensc 131
 
5 stevensc 132
      <ConfirmModal
133
        show={confirmModalShow}
134
        onClose={toggleConfirmModal}
135
        onAccept={() => {
136
          deleteAllNotifications()
137
          toggleConfirmModal()
138
        }}
139
      />
140
    </>
141
  )
142
}
143
 
144
export default NotificationsPage