Proyectos de Subversion LeadersLinked - SPA

Rev

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