Proyectos de Subversion LeadersLinked - Antes de SPA

Rev

Rev 7239 | | Comparar con el anterior | Ultima modificación | Ver Log |

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