Proyectos de Subversion LeadersLinked - Antes de SPA

Rev

Rev 4232 | Rev 4634 | 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 }) => {
2468 stevensc 15
 
2470 stevensc 16
  const { image, fullName, country, visits, connections, description } = backendVars
3277 stevensc 17
  const [notifications, setNotifications] = useState([])
18
  const [displayOption, setDisplayOption] = useState(false)
3311 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({
3276 stevensc 48
          style: "danger",
3197 stevensc 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
      })
3313 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)
67
      .then(({ data }) => {
68
        if (!data.success) {
69
          return dispatch(addNotification({ style: 'danger', msg: data.data }))
70
        }
71
        dispatch(addNotification({ style: 'success', msg: data.data }))
72
        setNotifications((prevNotifications) => prevNotifications.filter((notification) => notification.link_delete === url))
73
      })
74
      .catch(err => dispatch(addNotification({ style: "danger", msg: `Error: ${err}` })))
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
    }
83
    document.addEventListener("mousedown", handleClickOutside);
84
 
85
    return () => {
86
      document.removeEventListener("mousedown", handleClickOutside);
87
    };
88
  }, [menuOptions]);
89
 
90
  useLayoutEffect(() => {
91
    handleNotifications()
1309 steven 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
100
              image={image}
101
              fullName={fullName}
102
              description={description}
103
              visits={visits}
104
              country={country}
105
              connections={connections}
106
            />
107
            <SocialNetworks />
108
          </div>
4232 stevensc 109
          <div className="notification-list">
110
            <div className="notification-header">
111
              <h2>
112
                Notificaciones
113
              </h2>
114
              <div className="cursor-pointer d-flex align-items-center">
115
                <BiDotsVerticalRounded
116
                  onClick={() => setDisplayOption(!displayOption)}
117
                  style={{ fontSize: '1.5rem' }}
118
                />
119
                <div className={`feed-options ${displayOption ? 'active' : ''}`} ref={menuOptions} >
120
                  <ul>
121
                    <li>
122
                      <button
123
                        className="option-btn"
124
                        onClick={handleConfirmModalShow}
125
                      >
126
                        <i className="fa fa-trash-o mr-1" />
127
                        Borrar notificaciones
128
                      </button>
129
                    </li>
130
                  </ul>
3277 stevensc 131
                </div>
132
              </div>
1311 steven 133
            </div>
4232 stevensc 134
            <ul>
135
              {notifications.length
136
                ? [...notifications].reverse().map((element, index) =>
137
                  <li key={index}>
138
                    <div className="notification-item">
4633 stevensc 139
                      <div className="d-flex align-items-center justify-content-center" style={{ gap: '.5rem' }}>
140
                        <a href={element.link}>
141
                          {element.message}
142
                        </a>
143
                        <DeleteOutline onClick={() => deleteNotification(element.link_delete)} />
144
                      </div>
4232 stevensc 145
                      <span>
146
                        {element.time_elapsed}
147
                      </span>
148
                    </div>
149
                  </li>
150
                )
151
                :
152
                <div className="empty-section">
153
                  <h2>No hay notificaciones</h2>
154
                </div>
155
              }
156
            </ul>
1311 steven 157
          </div>
1309 steven 158
        </div>
3311 stevensc 159
      </section >
160
      <ConfirmModal
161
        show={confirmModalShow}
162
        onClose={handleConfirmModalShow}
3315 stevensc 163
        onAccept={handleAccept}
3314 stevensc 164
        acceptLabel='Aceptar'
3311 stevensc 165
      />
166
    </>
1307 steven 167
  )
168
}
3276 stevensc 169
 
3313 stevensc 170
export default Notifications