Proyectos de Subversion LeadersLinked - Antes de SPA

Rev

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