Proyectos de Subversion LeadersLinked - SPA

Rev

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