Proyectos de Subversion LeadersLinked - Antes de SPA

Rev

Rev 4205 | Rev 4633 | 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';
3
import { axios } from '../utils';
4
import { BiDotsVerticalRounded } from 'react-icons/bi';
4203 stevensc 5
 
2468 stevensc 6
import ProfileInfo from '../dashboard/components/home-section/ProfileInfo';
7
import SocialNetworks from '../dashboard/components/home-section/SocialNetworks';
3311 stevensc 8
import ConfirmModal from '../shared/confirm-modal/ConfirmModal';
4203 stevensc 9
 
3313 stevensc 10
import { useDispatch } from 'react-redux';
4203 stevensc 11
import { addNotification } from '../redux/notification/notification.actions';
1307 steven 12
 
2470 stevensc 13
const Notifications = ({ backendVars }) => {
2468 stevensc 14
 
2470 stevensc 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
 
1309 steven 64
  useLayoutEffect(() => {
3277 stevensc 65
    const handleClickOutside = (event) => {
66
      if (menuOptions.current && !menuOptions.current.contains(event.target)) {
67
        setDisplayOption(false)
68
      }
69
    }
70
    document.addEventListener("mousedown", handleClickOutside);
71
 
72
    return () => {
73
      document.removeEventListener("mousedown", handleClickOutside);
74
    };
75
  }, [menuOptions]);
76
 
77
  useLayoutEffect(() => {
78
    handleNotifications()
1309 steven 79
  }, []);
2468 stevensc 80
 
1307 steven 81
  return (
3311 stevensc 82
    <>
83
      <section className="notifications-page">
3316 stevensc 84
        <div className="notifications-grid">
4232 stevensc 85
          <div className='main-left-sidebar d-none d-md-flex'>
3311 stevensc 86
            <ProfileInfo
87
              image={image}
88
              fullName={fullName}
89
              description={description}
90
              visits={visits}
91
              country={country}
92
              connections={connections}
93
            />
94
            <SocialNetworks />
95
          </div>
4232 stevensc 96
          <div className="notification-list">
97
            <div className="notification-header">
98
              <h2>
99
                Notificaciones
100
              </h2>
101
              <div className="cursor-pointer d-flex align-items-center">
102
                <BiDotsVerticalRounded
103
                  onClick={() => setDisplayOption(!displayOption)}
104
                  style={{ fontSize: '1.5rem' }}
105
                />
106
                <div className={`feed-options ${displayOption ? 'active' : ''}`} ref={menuOptions} >
107
                  <ul>
108
                    <li>
109
                      <button
110
                        className="option-btn"
111
                        onClick={handleConfirmModalShow}
112
                      >
113
                        <i className="fa fa-trash-o mr-1" />
114
                        Borrar notificaciones
115
                      </button>
116
                    </li>
117
                  </ul>
3277 stevensc 118
                </div>
119
              </div>
1311 steven 120
            </div>
4232 stevensc 121
            <ul>
122
              {notifications.length
123
                ? [...notifications].reverse().map((element, index) =>
124
                  <li key={index}>
125
                    <div className="notification-item">
126
                      <a href={element.link}>
127
                        {element.message}
128
                      </a>
129
                      <span>
130
                        {element.time_elapsed}
131
                      </span>
132
                    </div>
133
                  </li>
134
                )
135
                :
136
                <div className="empty-section">
137
                  <h2>No hay notificaciones</h2>
138
                </div>
139
              }
140
            </ul>
1311 steven 141
          </div>
1309 steven 142
        </div>
3311 stevensc 143
      </section >
144
      <ConfirmModal
145
        show={confirmModalShow}
146
        onClose={handleConfirmModalShow}
3315 stevensc 147
        onAccept={handleAccept}
3314 stevensc 148
        acceptLabel='Aceptar'
3311 stevensc 149
      />
150
    </>
1307 steven 151
  )
152
}
3276 stevensc 153
 
3313 stevensc 154
export default Notifications