Proyectos de Subversion LeadersLinked - Antes de SPA

Rev

Rev 3276 | Rev 3278 | 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';
6
import { BsTrash, BsEye } from 'react-icons/bs';
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
 
3277 stevensc 43
  const deleteNotifications = () => {
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">
3277 stevensc 87
              <div className="d-flex align-items-center justify-content-between">
88
                <h2 className="card-title" style={{ fontSize: '1.7rem', fontWeight: '700' }}>
89
                  Notificaciones
90
                </h2>
91
                <div className="cursor-pointer d-flex align-items-center">
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
100
                          className="option-btn"
101
                          onClick={deleteNotifications}
102
                        >
103
                          <BsTrash className="mr-1" />
104
                          Borrar notificaciones
105
                        </button>
106
                      </li>
107
                      <li>
108
                        <button
109
                          className="option-btn"
110
                          onClick={markReadNotifications}
111
                        >
112
                          <BsEye className="mr-1" />
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">
125
                        <a href={element.link}>
126
                          {element.message}
127
                        </a>
128
                        <span>
129
                          {element.time_elapsed}
130
                        </span>
131
                      </div>
132
                    </li>
133
                  )
134
                  :
135
                  <div
136
                    className="section_admin_title_buttons w-100"
137
                    style={{ display: 'grid', placeItems: 'center' }}
138
                  >
139
                    <h1 className="title">No hay notificaciones</h1>
140
                  </div>
2471 stevensc 141
                }
142
              </ul>
1311 steven 143
            </div>
144
          </div>
1309 steven 145
        </div>
146
      </div>
2468 stevensc 147
    </section >
1307 steven 148
  )
149
}
3276 stevensc 150
const mapDispatchToProps = {
151
  addNotification: (notification) => addNotification(notification)
152
};
153
 
154
export default connect(null, mapDispatchToProps)(Notifications)