Proyectos de Subversion LeadersLinked - SPA

Rev

Rev 3396 | | Comparar con el anterior | Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
3719 stevensc 1
import React, { useEffect, useState } from 'react';
2
import { useNavigate, useParams } from 'react-router-dom';
3
import { useDispatch, useSelector } from 'react-redux';
4
import { Box, Grid } from '@mui/material';
5
import parse from 'html-react-parser';
6
 
7
import { axios } from '@app/utils';
8
import { addNotification } from '@app/redux/notification/notification.actions';
9
import { useFetch } from '@hooks';
10
 
11
import Widget from '@app/components/UI/Widget';
12
import AuthNavbar from '@app/components/UI/auth-navbar';
13
import Button from '@app/components/UI/buttons/Buttons';
14
 
15
function ShorterPostPage() {
16
  const { id } = useParams();
17
  const navigate = useNavigate();
18
 
19
  const [post, setPost] = useState({});
20
  const dispatch = useDispatch();
21
  const { is_logged_in } = useSelector((state) => state.auth);
22
 
23
  const { data } = useFetch(`/shorter/${id}`, null);
24
 
25
  const getPost = async (url) => {
26
    return await axios.get(url).then((res) => res.data.data);
27
  };
28
 
29
  const handleClick = () => navigate('/signin');
30
 
31
  useEffect(() => {
32
    if (!data) return;
33
    if (data?.redirect) {
34
      const redirectUrl = data?.url.split('.com')[1];
35
      navigate(redirectUrl);
36
      return;
37
    }
38
 
39
    getPost(data?.url)
40
      .then((post) => setPost(post))
41
      .catch((error) => {
42
        console.log(error);
43
        dispatch(
44
          addNotification({
45
            style: 'danger',
46
            msg: 'Ha ocurrido un error al obtener la información del post. Por favor, intentelo nuevamente.'
47
          })
48
        );
49
      });
50
  }, [data]);
51
 
52
  return (
53
    <>
54
      {!is_logged_in ? <AuthNavbar /> : null}
55
 
56
      <Grid
57
        container
58
        sx={{
59
          display: 'flex',
60
          justifyContent: 'center',
61
          alignItems: 'center'
62
        }}
63
      >
64
        <Grid size={{ xs: 12, md: 8 }}>
65
          <Widget>
66
            <Widget.Media alt={post.title} src={post.image} height={450} />
67
 
68
            <Widget.Body>
69
              {parse(post.description ?? '')}
70
 
71
              <Box
72
                sx={{
73
                  display: 'flex',
74
                  flexDirection: 'column',
75
                  justifyContent: 'center',
76
                  alignItems: 'center',
77
                  mt: 2
78
                }}
79
              >
80
                <p>Inicie sesion para visualizar los detalles de la publicación.</p>
81
                <Button onClick={handleClick} color='primary'>
82
                  Iniciar Sesión
83
                </Button>
84
              </Box>
85
            </Widget.Body>
86
          </Widget>
87
        </Grid>
88
      </Grid>
89
    </>
90
  );
91
}
92
 
93
export default ShorterPostPage;