Proyectos de Subversion LeadersLinked - SPA

Rev

Rev 2137 | Rev 2806 | 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'
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
    <>
1469 stevensc 135
      <Container>
136
        <Grid container spacing={2}>
137
          <Grid item xs={12} md={4}>
5 stevensc 138
            <ProfileInfo
139
              image={image}
675 stevensc 140
              name={fullName}
5 stevensc 141
              visits={visits}
142
              country={country}
143
              connections={connections}
144
            />
1469 stevensc 145
          </Grid>
146
          <Grid item xs={12} md={8}>
1507 stevensc 147
            <WidgetWrapper>
148
              <WidgetWrapper.Header title='Notificaciones'>
671 stevensc 149
                <Options
150
                  options={[
151
                    {
152
                      label: 'Borrar notificaciones',
153
                      action: toggleConfirmModal
154
                    }
155
                  ]}
156
                />
1507 stevensc 157
              </WidgetWrapper.Header>
670 stevensc 158
 
677 stevensc 159
              <StyledNotificationList>
672 stevensc 160
                {notifications.length ? (
161
                  [...notifications].reverse().map((notification, index) => (
162
                    <div key={index}>
2139 stevensc 163
                      <NotificationOption
164
                        {...notification}
672 stevensc 165
                        onDelete={deleteNotification}
166
                      />
167
                    </div>
168
                  ))
169
                ) : (
170
                  <EmptySection
171
                    message='No hay notificaciones'
172
                    align='center'
173
                  />
174
                )}
677 stevensc 175
              </StyledNotificationList>
1507 stevensc 176
            </WidgetWrapper>
1469 stevensc 177
          </Grid>
178
        </Grid>
5 stevensc 179
      </Container>
1469 stevensc 180
 
5 stevensc 181
      <ConfirmModal
182
        show={confirmModalShow}
183
        onClose={toggleConfirmModal}
184
        onAccept={() => {
185
          deleteAllNotifications()
186
          toggleConfirmModal()
187
        }}
188
      />
189
    </>
190
  )
191
}
192
 
193
export default NotificationsPage