Proyectos de Subversion LeadersLinked - Antes de SPA

Rev

Rev 4204 | Rev 4232 | 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">
3311 stevensc 85
          <div className='main-left-sidebar' style={{ marginTop: '1.25rem' }}>
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>
96
          <div className="card-body">
97
            <div className="container">
4201 stevensc 98
              <div className="notification-list">
4202 stevensc 99
                <div className="notification-header">
100
                  <h2>
3311 stevensc 101
                    Notificaciones
102
                  </h2>
103
                  <div className="cursor-pointer d-flex align-items-center">
104
                    <BiDotsVerticalRounded
105
                      onClick={() => setDisplayOption(!displayOption)}
106
                      style={{ fontSize: '1.5rem' }}
107
                    />
108
                    <div className={`feed-options ${displayOption ? 'active' : ''}`} ref={menuOptions} >
109
                      <ul>
110
                        <li>
111
                          <button
4204 stevensc 112
                            className="option-btn"
3313 stevensc 113
                            onClick={handleConfirmModalShow}
3311 stevensc 114
                          >
4203 stevensc 115
                            <i className="fa fa-trash-o mr-1" />
3311 stevensc 116
                            Borrar notificaciones
117
                          </button>
118
                        </li>
119
                      </ul>
120
                    </div>
3277 stevensc 121
                  </div>
122
                </div>
3311 stevensc 123
                <ul>
124
                  {notifications.length
4201 stevensc 125
                    ? [...notifications].reverse().map((element, index) =>
126
                      <li key={index}>
3311 stevensc 127
                        <div className="notification-item">
4201 stevensc 128
                          <a href={element.link}>
129
                            {element.message}
130
                          </a>
131
                          <span>
132
                            {element.time_elapsed}
133
                          </span>
3311 stevensc 134
                        </div>
135
                      </li>
136
                    )
137
                    :
4201 stevensc 138
                    <div className="empty-section">
4205 stevensc 139
                      <h2>No hay notificaciones</h2>
3311 stevensc 140
                    </div>
141
                  }
142
                </ul>
3277 stevensc 143
              </div>
1311 steven 144
            </div>
145
          </div>
1309 steven 146
        </div>
3311 stevensc 147
      </section >
148
      <ConfirmModal
149
        show={confirmModalShow}
150
        onClose={handleConfirmModalShow}
3315 stevensc 151
        onAccept={handleAccept}
3314 stevensc 152
        acceptLabel='Aceptar'
3311 stevensc 153
      />
154
    </>
1307 steven 155
  )
156
}
3276 stevensc 157
 
3313 stevensc 158
export default Notifications