Proyectos de Subversion LeadersLinked - SPA

Rev

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