Proyectos de Subversion LeadersLinked - SPA

Rev

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

Rev Autor Línea Nro. Línea
5 stevensc 1
import React, { useState, useLayoutEffect, useEffect } from 'react'
2
import { axios } from '../../utils'
3
import { useDispatch } from 'react-redux'
4
import { getBackendVars } from '../../services/backendVars'
5
import { addNotification } from '../../redux/notification/notification.actions'
6
import { Col, Container, Row } from 'react-bootstrap'
670 stevensc 7
import { Link } from 'react-router-dom'
8
import { IconButton } from '@mui/material'
5 stevensc 9
import DeleteOutline from '@mui/icons-material/DeleteOutline'
10
 
11
import Options from '../../components/UI/Option'
12
import ConfirmModal from '../../components/modals/ConfirmModal'
13
import EmptySection from '../../components/UI/EmptySection'
14
import ProfileInfo from '../../components/widgets/default/ProfileWidget'
15
 
16
const NotificationsPage = () => {
17
  const [userInfo, setuserInfo] = useState({})
18
  const [notifications, setNotifications] = useState([])
19
  const [confirmModalShow, setConfirmModalShow] = useState(false)
20
  const dispatch = useDispatch()
21
 
22
  const { image, fullName, country, visits, connections, description } =
23
    userInfo
24
 
25
  const getNotifications = async () => {
26
    axios
27
      .get('/notifications')
670 stevensc 28
      .then(({ data: responseData }) => {
29
        const { data, success } = responseData
5 stevensc 30
 
31
        if (!success) {
32
          const errorMessage =
33
            typeof data === 'string'
34
              ? data
35
              : 'Ha ocurrido un error, por favor intente más tarde'
670 stevensc 36
          throw new Error(errorMessage)
5 stevensc 37
        }
38
 
39
        setNotifications(data)
40
      })
670 stevensc 41
      .catch((err) => {
42
        dispatch(addNotification({ style: 'danger', msg: err.message }))
5 stevensc 43
      })
44
  }
45
 
46
  const deleteAllNotifications = () => {
47
    axios
48
      .post('/notifications/clear')
670 stevensc 49
      .then(({ data: responseData }) => {
50
        const { data, success } = responseData
51
 
5 stevensc 52
        if (!success) {
670 stevensc 53
          throw new Error(data)
5 stevensc 54
        }
55
 
56
        dispatch(addNotification({ style: 'success', msg: data }))
57
        setNotifications([])
58
      })
59
      .catch((err) => {
670 stevensc 60
        dispatch(addNotification({ style: 'danger', msg: err.message }))
5 stevensc 61
      })
62
  }
63
 
64
  const deleteNotification = (url) => {
65
    axios
66
      .post(url)
670 stevensc 67
      .then(({ data: responseData }) => {
68
        const { data, success } = responseData
5 stevensc 69
 
70
        if (!success) {
670 stevensc 71
          throw new Error(data)
5 stevensc 72
        }
73
 
74
        const newNotifications = notifications.filter(
75
          ({ link_delete }) => link_delete !== url
76
        )
77
 
78
        setNotifications(newNotifications)
79
        dispatch(addNotification({ style: 'success', msg: data }))
80
      })
81
      .catch((err) => {
670 stevensc 82
        dispatch(addNotification({ style: 'danger', msg: err.message }))
5 stevensc 83
      })
84
  }
85
 
86
  const readAllNotification = () => {
87
    axios
88
      .post('notifications/mark-all-read')
670 stevensc 89
      .then(({ data: responseData }) => {
90
        const { data, success } = responseData
5 stevensc 91
 
92
        if (!success) {
670 stevensc 93
          throw new Error(data)
5 stevensc 94
        }
95
      })
96
      .catch((err) => {
670 stevensc 97
        dispatch(addNotification({ style: 'danger', msg: err.message }))
5 stevensc 98
      })
99
  }
100
 
101
  const toggleConfirmModal = () => {
102
    setConfirmModalShow(!confirmModalShow)
103
  }
104
 
105
  useEffect(() => {
106
    getBackendVars('/helpers/menu')
107
      .then(
108
        ({ image, full_name, country, visits, connections, description }) => {
109
          setuserInfo({
110
            image,
111
            full_name,
112
            country,
113
            visits,
114
            connections,
670 stevensc 115
            description
5 stevensc 116
          })
117
        }
118
      )
119
      .catch((err) => {
120
        console.log(err)
121
      })
122
  }, [])
123
 
124
  useLayoutEffect(() => {
125
    getNotifications()
126
    readAllNotification()
127
  }, [])
128
 
129
  return (
130
    <>
670 stevensc 131
      <Container as='main'>
5 stevensc 132
        <Row>
670 stevensc 133
          <Col as='aside' md='4' className='d-none d-md-flex'>
5 stevensc 134
            <ProfileInfo
135
              image={image}
136
              fullName={fullName}
137
              description={description}
138
              visits={visits}
139
              country={country}
140
              connections={connections}
141
            />
142
          </Col>
670 stevensc 143
          <Col as='section' md='8' className='notification-list'>
144
            <div className='notification-header position-relative'>
5 stevensc 145
              <h2>Notificaciones</h2>
146
              <Options
147
                options={[
148
                  {
149
                    label: 'Borrar notificaciones',
670 stevensc 150
                    action: toggleConfirmModal
151
                  }
5 stevensc 152
                ]}
153
              />
154
            </div>
670 stevensc 155
 
156
            {notifications.length ? (
157
              [...notifications].reverse().map((notification, index) => (
158
                <div key={index}>
159
                  <NotificationsPage.Item
160
                    onDelete={deleteNotification}
161
                    {...notification}
162
                  />
163
                </div>
164
              ))
165
            ) : (
166
              <EmptySection message='No hay notificaciones' align='center' />
167
            )}
5 stevensc 168
          </Col>
169
        </Row>
170
      </Container>
171
      <ConfirmModal
172
        show={confirmModalShow}
173
        onClose={toggleConfirmModal}
174
        onAccept={() => {
175
          deleteAllNotifications()
176
          toggleConfirmModal()
177
        }}
178
      />
179
    </>
180
  )
181
}
182
 
183
const Item = ({ link_delete, link, message, time_elapsed, onDelete }) => {
184
  return (
670 stevensc 185
    <div className='notification-item'>
186
      <div className='d-flex align-items-center justify-content-between m-0 p-0'>
187
        <Link to={link}>{message}</Link>
188
        <IconButton onClick={() => onDelete(link_delete)}>
189
          <DeleteOutline />
190
        </IconButton>
644 andreina 191
      </div>
619 andreina 192
      <span>{time_elapsed}</span>
193
      <hr />
5 stevensc 194
    </div>
195
  )
196
}
197
 
198
NotificationsPage.Item = Item
199
 
200
export default NotificationsPage