Proyectos de Subversion LeadersLinked - SPA

Rev

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