Proyectos de Subversion LeadersLinked - Antes de SPA

Rev

Rev 5374 | Rev 5933 | 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'
5380 stevensc 12
import ProfileInfo from '../dashboard/components/home-section/ProfileInfo'
1307 steven 13
 
2470 stevensc 14
const Notifications = ({ backendVars }) => {
5380 stevensc 15
  const { image, fullName, country, visits, connections, description } = backendVars
3277 stevensc 16
  const [notifications, setNotifications] = useState([])
17
  const [displayOption, setDisplayOption] = useState(false)
5373 stevensc 18
  const [confirmModalShow, setConfirmModalShow] = useState(false)
3313 stevensc 19
  const dispatch = useDispatch()
3277 stevensc 20
  const menuOptions = useRef(null)
2468 stevensc 21
 
3311 stevensc 22
  const handleConfirmModalShow = () => setConfirmModalShow(!confirmModalShow)
23
 
3315 stevensc 24
  const handleAccept = () => {
25
    deleteAllNotifications()
26
    handleConfirmModalShow()
27
  }
4201 stevensc 28
 
1309 steven 29
  const handleNotifications = async () => {
1310 steven 30
    try {
3277 stevensc 31
      const _notifications = await axios.get('/notifications')
1310 steven 32
      setNotifications(_notifications.data.data)
33
    } catch (error) {
34
      console.log('>>: error > ', error)
35
    }
1309 steven 36
  }
2468 stevensc 37
 
3278 stevensc 38
  const deleteAllNotifications = () => {
3277 stevensc 39
    axios.post('/notifications/clear')
40
      .then(({ data }) => {
3313 stevensc 41
        if (!data.success) dispatch(addNotification({ style: 'danger', msg: data.data }))
42
        dispatch(addNotification({ style: 'success', msg: data.data }))
3277 stevensc 43
        setNotifications([])
44
      })
5373 stevensc 45
      .catch(err => dispatch(addNotification({ style: 'danger', msg: `Error: ${err}` })))
3277 stevensc 46
  }
47
 
4633 stevensc 48
  const deleteNotification = (url) => {
49
    axios.post(url)
4635 stevensc 50
      .then(({ data: response }) => {
51
        if (!response.success) {
52
          return dispatch(addNotification({ style: 'danger', msg: response.data }))
4633 stevensc 53
        }
4635 stevensc 54
        dispatch(addNotification({ style: 'success', msg: response.data }))
55
        setNotifications((prevNotifications) => prevNotifications.filter((notification) => notification.link_delete !== url))
4633 stevensc 56
      })
5373 stevensc 57
      .catch(err => dispatch(addNotification({ style: 'danger', msg: `Error: ${err}` })))
4633 stevensc 58
  }
59
 
1309 steven 60
  useLayoutEffect(() => {
3277 stevensc 61
    const handleClickOutside = (event) => {
62
      if (menuOptions.current && !menuOptions.current.contains(event.target)) {
63
        setDisplayOption(false)
64
      }
65
    }
5373 stevensc 66
    document.addEventListener('mousedown', handleClickOutside)
3277 stevensc 67
 
68
    return () => {
5373 stevensc 69
      document.removeEventListener('mousedown', handleClickOutside)
70
    }
71
  }, [menuOptions])
3277 stevensc 72
 
73
  useLayoutEffect(() => {
74
    handleNotifications()
5373 stevensc 75
  }, [])
2468 stevensc 76
 
1307 steven 77
  return (
3311 stevensc 78
    <>
5380 stevensc 79
      <main className="notifications-grid container">
80
        <aside className='main-left-sidebar d-none d-md-flex'>
81
          <ProfileInfo
82
            image={image}
83
            fullName={fullName}
84
            description={description}
85
            visits={visits}
86
            country={country}
87
            connections={connections}
88
          />
89
          <SocialNetworks />
90
        </aside>
91
        <div className="notification-list">
92
          <div className="notification-header">
93
            <h2>Notificaciones</h2>
94
            <div className="cursor-pointer d-flex align-items-center">
95
              <BiDotsVerticalRounded
96
                onClick={() => setDisplayOption(!displayOption)}
97
                style={{ fontSize: '1.5rem' }}
98
              />
99
              <div className={`feed-options ${displayOption ? 'active' : ''}`} ref={menuOptions} >
100
                <ul>
101
                  <li>
102
                    <button
103
                      className="option-btn"
104
                      onClick={handleConfirmModalShow}
105
                    >
106
                      <i className="fa fa-trash-o mr-1" />
107
                      Borrar notificaciones
108
                    </button>
109
                  </li>
110
                </ul>
3277 stevensc 111
              </div>
1311 steven 112
            </div>
5380 stevensc 113
          </div>
114
          <ul>
115
            {notifications.length
116
              ? [...notifications].reverse().map((element, index) =>
117
                <li key={index}>
118
                  <div className="notification-item">
119
                    <div className="d-flex align-items-center justify-content-between" style={{ gap: '.5rem' }}>
120
                      <a href={element.link}>
121
                        {element.message}
122
                      </a>
123
                      <DeleteOutline className='cursor-pointer' onClick={() => deleteNotification(element.link_delete)} />
4232 stevensc 124
                    </div>
5380 stevensc 125
                    <span>
126
                      {element.time_elapsed}
127
                    </span>
128
                  </div>
129
                </li>
130
                )
131
              : <div className="empty-section">
132
                <h2>No hay notificaciones</h2>
133
              </div>
134
            }
135
          </ul>
1309 steven 136
        </div>
5380 stevensc 137
      </main>
3311 stevensc 138
      <ConfirmModal
139
        show={confirmModalShow}
140
        onClose={handleConfirmModalShow}
3315 stevensc 141
        onAccept={handleAccept}
3314 stevensc 142
        acceptLabel='Aceptar'
3311 stevensc 143
      />
144
    </>
1307 steven 145
  )
146
}
3276 stevensc 147
 
5373 stevensc 148
export default Notifications