Proyectos de Subversion LeadersLinked - SPA

Rev

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