Proyectos de Subversion LeadersLinked - Antes de SPA

Rev

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