Proyectos de Subversion LeadersLinked - Antes de SPA

Rev

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