Proyectos de Subversion LeadersLinked - Antes de SPA

Rev

Rev 3281 | Rev 3311 | 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
                    </ul>
108
                  </div>
109
                </div>
110
              </div>
2471 stevensc 111
              <ul>
3276 stevensc 112
                {notifications.length
113
                  ? [...notifications].reverse().map((element, i) =>
114
                    <li key={i}>
115
                      <div className="notification-item">
3278 stevensc 116
                        <div className="d-inline-flex flex-column">
3279 stevensc 117
                          <a href={element.link} className='mb-1'>
3278 stevensc 118
                            {element.message}
119
                          </a>
120
                          <span>
121
                            {element.time_elapsed}
122
                          </span>
123
                        </div>
3276 stevensc 124
                      </div>
125
                    </li>
126
                  )
127
                  :
128
                  <div
129
                    className="section_admin_title_buttons w-100"
130
                    style={{ display: 'grid', placeItems: 'center' }}
131
                  >
132
                    <h1 className="title">No hay notificaciones</h1>
133
                  </div>
2471 stevensc 134
                }
135
              </ul>
1311 steven 136
            </div>
137
          </div>
1309 steven 138
        </div>
139
      </div>
2468 stevensc 140
    </section >
1307 steven 141
  )
142
}
3276 stevensc 143
const mapDispatchToProps = {
144
  addNotification: (notification) => addNotification(notification)
145
};
146
 
147
export default connect(null, mapDispatchToProps)(Notifications)