Proyectos de Subversion LeadersLinked - SPA

Rev

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