Proyectos de Subversion LeadersLinked - Antes de SPA

Rev

Rev 3313 | Rev 3315 | 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
 
1309 steven 23
  const handleNotifications = async () => {
1310 steven 24
    try {
3277 stevensc 25
      const _notifications = await axios.get('/notifications')
1310 steven 26
      setNotifications(_notifications.data.data)
27
    } catch (error) {
28
      console.log('>>: error > ', error)
29
    }
1309 steven 30
  }
2468 stevensc 31
 
3197 stevensc 32
  const markReadNotifications = () => {
3274 stevensc 33
    axios.post('/notifications/mark-all-read')
3276 stevensc 34
      .then(({ data }) => {
3277 stevensc 35
        !data.success
3313 stevensc 36
          ? dispatch(addNotification({ style: 'danger', msg: data.data }))
37
          : dispatch(addNotification({ style: 'success', msg: data.data }))
3276 stevensc 38
      })
3197 stevensc 39
      .catch(err => {
3313 stevensc 40
        dispatch(addNotification({
3276 stevensc 41
          style: "danger",
3197 stevensc 42
          msg: 'Disculpe, ha ocurrido un error marcando notificaciones como leidas',
3313 stevensc 43
        }))
3197 stevensc 44
        console.log('>>: err > ', err)
45
      })
46
  }
47
 
3278 stevensc 48
  const deleteAllNotifications = () => {
3277 stevensc 49
    axios.post('/notifications/clear')
50
      .then(({ data }) => {
3313 stevensc 51
        if (!data.success) dispatch(addNotification({ style: 'danger', msg: data.data }))
52
        dispatch(addNotification({ style: 'success', msg: data.data }))
3277 stevensc 53
        setNotifications([])
54
      })
3313 stevensc 55
      .catch(err => dispatch(addNotification({ style: "danger", msg: `Error: ${err}` })))
3277 stevensc 56
  }
57
 
1309 steven 58
  useLayoutEffect(() => {
3277 stevensc 59
    const handleClickOutside = (event) => {
60
      if (menuOptions.current && !menuOptions.current.contains(event.target)) {
61
        setDisplayOption(false)
62
      }
63
    }
64
    document.addEventListener("mousedown", handleClickOutside);
65
 
66
    return () => {
67
      document.removeEventListener("mousedown", handleClickOutside);
68
    };
69
  }, [menuOptions]);
70
 
71
  useLayoutEffect(() => {
72
    handleNotifications()
1309 steven 73
  }, []);
2468 stevensc 74
 
1307 steven 75
  return (
3311 stevensc 76
    <>
77
      <section className="notifications-page">
78
        <div className="card notifications-grid">
79
          <div className='main-left-sidebar' style={{ marginTop: '1.25rem' }}>
80
            <ProfileInfo
81
              image={image}
82
              fullName={fullName}
83
              description={description}
84
              visits={visits}
85
              country={country}
86
              connections={connections}
87
            />
88
            <SocialNetworks />
89
          </div>
90
          <div className="card-body">
91
            <div className="container">
92
              <div className="messages-sec border-gray px-5 py-4">
93
                <div className="d-flex align-items-center justify-content-between position-relative mb-3">
94
                  <h2 className="card-title" style={{ fontSize: '1.7rem', fontWeight: '700' }}>
95
                    Notificaciones
96
                  </h2>
97
                  <div className="cursor-pointer d-flex align-items-center">
98
                    <BiDotsVerticalRounded
99
                      onClick={() => setDisplayOption(!displayOption)}
100
                      style={{ fontSize: '1.5rem' }}
101
                    />
102
                    <div className={`feed-options ${displayOption ? 'active' : ''}`} ref={menuOptions} >
103
                      <ul>
104
                        <li>
105
                          <button
106
                            className="option-btn mb-2"
3313 stevensc 107
                            onClick={handleConfirmModalShow}
3311 stevensc 108
                          >
109
                            <FaTrash className="mr-1" />
110
                            Borrar notificaciones
111
                          </button>
112
                        </li>
113
                      </ul>
114
                    </div>
3277 stevensc 115
                  </div>
116
                </div>
3311 stevensc 117
                <ul>
118
                  {notifications.length
119
                    ? [...notifications].reverse().map((element, i) =>
120
                      <li key={i}>
121
                        <div className="notification-item">
122
                          <div className="d-inline-flex flex-column">
123
                            <a href={element.link} className='mb-1'>
124
                              {element.message}
125
                            </a>
126
                            <span>
127
                              {element.time_elapsed}
128
                            </span>
129
                          </div>
130
                        </div>
131
                      </li>
132
                    )
133
                    :
134
                    <div
135
                      className="section_admin_title_buttons w-100"
136
                      style={{ display: 'grid', placeItems: 'center' }}
137
                    >
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}
150
        onAccept={deleteAllNotifications}
3314 stevensc 151
        acceptLabel='Aceptar'
3311 stevensc 152
      />
153
    </>
1307 steven 154
  )
155
}
3276 stevensc 156
 
3313 stevensc 157
export default Notifications