Proyectos de Subversion LeadersLinked - Antes de SPA

Rev

Rev 5373 | Rev 5380 | Ir a la última revisión | | Comparar con el anterior | Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
3198 stevensc 1
/* eslint-disable react/prop-types */
5373 stevensc 2
import React, { useState, useLayoutEffect, useRef } from 'react'
3
import { BiDotsVerticalRounded } from 'react-icons/bi'
4633 stevensc 4
import DeleteOutline from '@mui/icons-material/DeleteOutline'
5373 stevensc 5
import { axios } from '../utils'
4203 stevensc 6
 
5373 stevensc 7
import SocialNetworks from '../dashboard/components/home-section/SocialNetworks'
8
import ConfirmModal from '../shared/confirm-modal/ConfirmModal'
4203 stevensc 9
 
5373 stevensc 10
import { useDispatch } from 'react-redux'
11
import { addNotification } from '../redux/notification/notification.actions'
12
import ProfileInfo from '../shared/profile/edit/profile-info/ProfileInfo'
13
import { profileTypes } from '../shared/profile/Profile.types'
1307 steven 14
 
2470 stevensc 15
const Notifications = ({ backendVars }) => {
5373 stevensc 16
  const { image, uuid } = backendVars
3277 stevensc 17
  const [notifications, setNotifications] = useState([])
18
  const [displayOption, setDisplayOption] = useState(false)
5373 stevensc 19
  const [confirmModalShow, setConfirmModalShow] = useState(false)
3313 stevensc 20
  const dispatch = useDispatch()
3277 stevensc 21
  const menuOptions = useRef(null)
2468 stevensc 22
 
3311 stevensc 23
  const handleConfirmModalShow = () => setConfirmModalShow(!confirmModalShow)
24
 
3315 stevensc 25
  const handleAccept = () => {
26
    deleteAllNotifications()
27
    handleConfirmModalShow()
28
  }
4201 stevensc 29
 
1309 steven 30
  const handleNotifications = async () => {
1310 steven 31
    try {
3277 stevensc 32
      const _notifications = await axios.get('/notifications')
1310 steven 33
      setNotifications(_notifications.data.data)
34
    } catch (error) {
35
      console.log('>>: error > ', error)
36
    }
1309 steven 37
  }
2468 stevensc 38
 
3197 stevensc 39
  const markReadNotifications = () => {
3274 stevensc 40
    axios.post('/notifications/mark-all-read')
3276 stevensc 41
      .then(({ data }) => {
3277 stevensc 42
        !data.success
3313 stevensc 43
          ? dispatch(addNotification({ style: 'danger', msg: data.data }))
44
          : dispatch(addNotification({ style: 'success', msg: data.data }))
3276 stevensc 45
      })
3197 stevensc 46
      .catch(err => {
3313 stevensc 47
        dispatch(addNotification({
5373 stevensc 48
          style: 'danger',
49
          msg: 'Disculpe, ha ocurrido un error marcando notificaciones como leidas'
3313 stevensc 50
        }))
3197 stevensc 51
        console.log('>>: err > ', err)
52
      })
53
  }
54
 
3278 stevensc 55
  const deleteAllNotifications = () => {
3277 stevensc 56
    axios.post('/notifications/clear')
57
      .then(({ data }) => {
3313 stevensc 58
        if (!data.success) dispatch(addNotification({ style: 'danger', msg: data.data }))
59
        dispatch(addNotification({ style: 'success', msg: data.data }))
3277 stevensc 60
        setNotifications([])
61
      })
5373 stevensc 62
      .catch(err => dispatch(addNotification({ style: 'danger', msg: `Error: ${err}` })))
3277 stevensc 63
  }
64
 
4633 stevensc 65
  const deleteNotification = (url) => {
66
    axios.post(url)
4635 stevensc 67
      .then(({ data: response }) => {
68
        if (!response.success) {
69
          return dispatch(addNotification({ style: 'danger', msg: response.data }))
4633 stevensc 70
        }
4635 stevensc 71
        dispatch(addNotification({ style: 'success', msg: response.data }))
72
        setNotifications((prevNotifications) => prevNotifications.filter((notification) => notification.link_delete !== url))
4633 stevensc 73
      })
5373 stevensc 74
      .catch(err => dispatch(addNotification({ style: 'danger', msg: `Error: ${err}` })))
4633 stevensc 75
  }
76
 
1309 steven 77
  useLayoutEffect(() => {
3277 stevensc 78
    const handleClickOutside = (event) => {
79
      if (menuOptions.current && !menuOptions.current.contains(event.target)) {
80
        setDisplayOption(false)
81
      }
82
    }
5373 stevensc 83
    document.addEventListener('mousedown', handleClickOutside)
3277 stevensc 84
 
85
    return () => {
5373 stevensc 86
      document.removeEventListener('mousedown', handleClickOutside)
87
    }
88
  }, [menuOptions])
3277 stevensc 89
 
90
  useLayoutEffect(() => {
91
    handleNotifications()
5373 stevensc 92
  }, [])
2468 stevensc 93
 
1307 steven 94
  return (
3311 stevensc 95
    <>
96
      <section className="notifications-page">
3316 stevensc 97
        <div className="notifications-grid">
4232 stevensc 98
          <div className='main-left-sidebar d-none d-md-flex'>
3311 stevensc 99
            <ProfileInfo
5373 stevensc 100
              entityId={uuid}
3311 stevensc 101
              image={image}
102
            />
103
            <SocialNetworks />
104
          </div>
4232 stevensc 105
          <div className="notification-list">
106
            <div className="notification-header">
107
              <h2>
108
                Notificaciones
109
              </h2>
110
              <div className="cursor-pointer d-flex align-items-center">
111
                <BiDotsVerticalRounded
112
                  onClick={() => setDisplayOption(!displayOption)}
113
                  style={{ fontSize: '1.5rem' }}
114
                />
115
                <div className={`feed-options ${displayOption ? 'active' : ''}`} ref={menuOptions} >
116
                  <ul>
117
                    <li>
118
                      <button
119
                        className="option-btn"
120
                        onClick={handleConfirmModalShow}
121
                      >
122
                        <i className="fa fa-trash-o mr-1" />
123
                        Borrar notificaciones
124
                      </button>
125
                    </li>
126
                  </ul>
3277 stevensc 127
                </div>
128
              </div>
1311 steven 129
            </div>
4232 stevensc 130
            <ul>
131
              {notifications.length
132
                ? [...notifications].reverse().map((element, index) =>
133
                  <li key={index}>
134
                    <div className="notification-item">
4634 stevensc 135
                      <div className="d-flex align-items-center justify-content-between" style={{ gap: '.5rem' }}>
4633 stevensc 136
                        <a href={element.link}>
137
                          {element.message}
138
                        </a>
4635 stevensc 139
                        <DeleteOutline className='cursor-pointer' onClick={() => deleteNotification(element.link_delete)} />
4633 stevensc 140
                      </div>
4232 stevensc 141
                      <span>
142
                        {element.time_elapsed}
143
                      </span>
144
                    </div>
145
                  </li>
5373 stevensc 146
                  )
147
                : <div className="empty-section">
4232 stevensc 148
                  <h2>No hay notificaciones</h2>
149
                </div>
150
              }
151
            </ul>
1311 steven 152
          </div>
1309 steven 153
        </div>
3311 stevensc 154
      </section >
155
      <ConfirmModal
156
        show={confirmModalShow}
157
        onClose={handleConfirmModalShow}
3315 stevensc 158
        onAccept={handleAccept}
3314 stevensc 159
        acceptLabel='Aceptar'
3311 stevensc 160
      />
161
    </>
1307 steven 162
  )
163
}
3276 stevensc 164
 
5373 stevensc 165
export default Notifications