Proyectos de Subversion LeadersLinked - Antes de SPA

Rev

Rev 3278 | Rev 3281 | 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
 
3278 stevensc 53
  const deleteNotification = (link_delete) => {
54
    axios.post(link_delete)
55
      .then(({ data }) => {
56
        !data.success && addNotification({ style: 'danger', msg: data.data })
57
        addNotification({ style: 'success', msg: data.data })
58
        setNotifications(notifications.filter(notification => notification.link_delete !== link_delete))
59
      })
60
      .catch(err => addNotification({ style: "danger", msg: `Error: ${err}` }))
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 (
1309 steven 81
    <section className="notifications-page">
2468 stevensc 82
      <div className="card notifications-grid">
2478 stevensc 83
        <div className='main-left-sidebar' style={{ marginTop: '1.25rem' }}>
2468 stevensc 84
          <ProfileInfo
85
            image={image}
86
            fullName={fullName}
87
            description={description}
88
            visits={visits}
89
            country={country}
90
            connections={connections}
91
          />
92
          <SocialNetworks />
93
        </div>
1311 steven 94
        <div className="card-body">
95
          <div className="container">
2476 stevensc 96
            <div className="messages-sec border-gray px-5 py-4">
3279 stevensc 97
              <div className="d-flex align-items-center justify-content-between position-relative mb-3">
3277 stevensc 98
                <h2 className="card-title" style={{ fontSize: '1.7rem', fontWeight: '700' }}>
99
                  Notificaciones
100
                </h2>
3279 stevensc 101
                <div className="cursor-pointer d-flex align-items-center">
3277 stevensc 102
                  <BiDotsVerticalRounded
103
                    onClick={() => setDisplayOption(!displayOption)}
104
                    style={{ fontSize: '1.5rem' }}
105
                  />
106
                  <div className={`feed-options ${displayOption ? 'active' : ''}`} ref={menuOptions} >
107
                    <ul>
108
                      <li>
109
                        <button
3278 stevensc 110
                          className="option-btn mb-2"
111
                          onClick={deleteAllNotifications}
3277 stevensc 112
                        >
3279 stevensc 113
                          <FaTrash className="mr-1" />
3277 stevensc 114
                          Borrar notificaciones
115
                        </button>
116
                      </li>
117
                      <li>
118
                        <button
119
                          className="option-btn"
120
                          onClick={markReadNotifications}
121
                        >
3279 stevensc 122
                          <FaEye className="mr-1" />
3277 stevensc 123
                          Marcar notificaciones como leídas
124
                        </button>
125
                      </li>
126
                    </ul>
127
                  </div>
128
                </div>
129
              </div>
2471 stevensc 130
              <ul>
3276 stevensc 131
                {notifications.length
132
                  ? [...notifications].reverse().map((element, i) =>
133
                    <li key={i}>
134
                      <div className="notification-item">
3278 stevensc 135
                        <div className="d-inline-flex flex-column">
3279 stevensc 136
                          <a href={element.link} className='mb-1'>
3278 stevensc 137
                            {element.message}
138
                          </a>
139
                          <span>
140
                            {element.time_elapsed}
141
                          </span>
142
                        </div>
3279 stevensc 143
                        <FaTrash className='cursor-pointer' onClick={() => deleteNotification(element.link_delete)} />
3276 stevensc 144
                      </div>
145
                    </li>
146
                  )
147
                  :
148
                  <div
149
                    className="section_admin_title_buttons w-100"
150
                    style={{ display: 'grid', placeItems: 'center' }}
151
                  >
152
                    <h1 className="title">No hay notificaciones</h1>
153
                  </div>
2471 stevensc 154
                }
155
              </ul>
1311 steven 156
            </div>
157
          </div>
1309 steven 158
        </div>
159
      </div>
2468 stevensc 160
    </section >
1307 steven 161
  )
162
}
3276 stevensc 163
const mapDispatchToProps = {
164
  addNotification: (notification) => addNotification(notification)
165
};
166
 
167
export default connect(null, mapDispatchToProps)(Notifications)