Rev 675 | Rev 1469 | Ir a la última revisión | Autoría | Comparar con el anterior | Ultima modificación | Ver Log |
import React, { useState, useLayoutEffect, useEffect } from 'react'import { axios } from '../../utils'import { useDispatch } from 'react-redux'import { getBackendVars } from '../../services/backendVars'import { addNotification } from '../../redux/notification/notification.actions'import { Col, Container, Row } from 'react-bootstrap'import { Link } from 'react-router-dom'import { IconButton } from '@mui/material'import DeleteOutline from '@mui/icons-material/DeleteOutline'import Options from '../../components/UI/Option'import ConfirmModal from '../../components/modals/ConfirmModal'import EmptySection from '../../components/UI/EmptySection'import ProfileInfo from '../../components/widgets/default/ProfileWidget'import styled from 'styled-components'import StyledContainer from '../../components/widgets/WidgetLayout'import Paraphrase from '../../components/UI/Paraphrase'const StyledNotificationList = styled(StyledContainer.Body)`max-height: 60vh;overflow: auto;`const StyledNotificationItem = styled.div`position: relative;display: flex;flex-direction: column;margin-bottom: 0.5rem;p {margin-bottom: 0;}`const StyledActionButton = styled(IconButton)`position: absolute !important;right: 0.5rem;top: 50%;transform: translateY(-50%);`const NotificationsPage = () => {const [userInfo, setuserInfo] = useState({})const [notifications, setNotifications] = useState([])const [confirmModalShow, setConfirmModalShow] = useState(false)const dispatch = useDispatch()const { image, fullName, country, visits, connections } = userInfoconst getNotifications = async () => {axios.get('/notifications').then(({ data: responseData }) => {const { data, success } = responseDataif (!success) {const errorMessage =typeof data === 'string'? data: 'Ha ocurrido un error, por favor intente más tarde'throw new Error(errorMessage)}setNotifications(data)}).catch((err) => {dispatch(addNotification({ style: 'danger', msg: err.message }))})}const deleteAllNotifications = () => {axios.post('/notifications/clear').then(({ data: responseData }) => {const { data, success } = responseDataif (!success) {throw new Error(data)}dispatch(addNotification({ style: 'success', msg: data }))setNotifications([])}).catch((err) => {dispatch(addNotification({ style: 'danger', msg: err.message }))})}const deleteNotification = (url) => {axios.post(url).then(({ data: responseData }) => {const { data, success } = responseDataif (!success) {throw new Error(data)}const newNotifications = notifications.filter(({ link_delete }) => link_delete !== url)setNotifications(newNotifications)dispatch(addNotification({ style: 'success', msg: data }))}).catch((err) => {dispatch(addNotification({ style: 'danger', msg: err.message }))})}const readAllNotification = () => {axios.post('notifications/mark-all-read').then(({ data: responseData }) => {const { data, success } = responseDataif (!success) {throw new Error(data)}}).catch((err) => {dispatch(addNotification({ style: 'danger', msg: err.message }))})}const toggleConfirmModal = () => {setConfirmModalShow(!confirmModalShow)}useEffect(() => {getBackendVars('/helpers/menu').then(({ image, fullName, country, visits, connections, description }) => {setuserInfo({image,fullName,country,visits,connections})}).catch((err) => {console.log(err)})}, [])useLayoutEffect(() => {getNotifications()readAllNotification()}, [])return (<><Container as='main'><Row><Col as='aside' md='4' className='d-none d-md-flex'><ProfileInfoimage={image}name={fullName}visits={visits}country={country}connections={connections}/></Col><Col as='section' md='8'><StyledContainer><StyledContainer.Header title='Notificaciones'><Optionsoptions={[{label: 'Borrar notificaciones',action: toggleConfirmModal}]}/></StyledContainer.Header><StyledNotificationList>{notifications.length ? ([...notifications].reverse().map((notification, index) => (<div key={index}><NotificationsPage.ItemonDelete={deleteNotification}{...notification}/></div>))) : (<EmptySectionmessage='No hay notificaciones'align='center'/>)}</StyledNotificationList></StyledContainer></Col></Row></Container><ConfirmModalshow={confirmModalShow}onClose={toggleConfirmModal}onAccept={() => {deleteAllNotifications()toggleConfirmModal()}}/></>)}const Item = ({ link_delete, link, message, time_elapsed, onDelete }) => {return (<StyledNotificationItem><Link to={link}><Paraphrase>{message}</Paraphrase></Link><span>{time_elapsed}</span><StyledActionButton onClick={() => onDelete(link_delete)}><DeleteOutline /></StyledActionButton></StyledNotificationItem>)}NotificationsPage.Item = Itemexport default NotificationsPage