Proyectos de Subversion LeadersLinked - Antes de SPA

Rev

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