Proyectos de Subversion LeadersLinked - SPA

Rev

Rev 3416 | Ir a la última revisión | Mostrar el archivo completo | | | Autoría | Ultima modificación | Ver Log |

Rev 3416 Rev 3432
Línea 1... Línea 1...
1
import { useEffect, useState } from "react";
1
import { useEffect, useState } from 'react'
2
import { useDispatch } from "react-redux";
2
import { useDispatch } from 'react-redux'
Línea 3... Línea 3...
3
 
3
 
4
import { axios } from "@app/utils";
4
import { axios } from '@app/utils'
5
import { useFetch } from "@hooks";
5
import { useFetch } from '@hooks'
Línea 6... Línea 6...
6
import { addNotification } from "@app/redux/notification/notification.actions";
6
import { addNotification } from '@app/redux/notification/notification.actions'
7
 
7
 
8
export function usePosts(postUrl) {
8
export function usePosts(postUrl) {
Línea 9... Línea 9...
9
  const { data: post, loading, mutate } = useFetch(postUrl);
9
  const { data: post, isLoading, mutate } = useFetch(postUrl)
Línea 10... Línea 10...
10
  const [comments, setComments] = useState([]);
10
  const [comments, setComments] = useState([])
11
 
11
 
12
  const dispatch = useDispatch();
12
  const dispatch = useDispatch()
Línea 13... Línea 13...
13
 
13
 
14
  const getComments = (url) => {
14
  const getComments = (url) => {
15
    axios.get(url).then((response) => {
15
    axios.get(url).then((response) => {
Línea 16... Línea 16...
16
      const { data, success } = response.data;
16
      const { data, success } = response.data
17
 
17
 
18
      if (!success) {
18
      if (!success) {
Línea 19... Línea 19...
19
        const errorMessage =
19
        const errorMessage =
20
          typeof data === "string" ? data : "Error interno. Intente más tarde.";
20
          typeof data === 'string' ? data : 'Error interno. Intente más tarde.'
21
 
21
 
Línea 22... Línea 22...
22
        dispatch(addNotification({ style: "danger", msg: errorMessage }));
22
        dispatch(addNotification({ style: 'danger', msg: errorMessage }))
23
        return;
23
        return
24
      }
24
      }
25
 
25
 
26
      setComments(data);
26
      setComments(data)
27
    });
27
    })
Línea 28... Línea 28...
28
  };
28
  }
29
 
29
 
30
  const updateTotalShare = (value) => {
30
  const updateTotalShare = (value) => {
Línea 31... Línea 31...
31
    mutate((prevPost) => ({
31
    mutate((prevPost) => ({
32
      ...prevPost,
32
      ...prevPost,
33
      total_share_external: value,
33
      total_share_external: value
Línea 34... Línea 34...
34
    }));
34
    }))
35
  };
35
  }
36
 
36
 
Línea 37... Línea 37...
37
  const updateReactions = (reactions) => {
37
  const updateReactions = (reactions) => {
38
    mutate((prevPost) => ({ ...prevPost, reactions }));
38
    mutate((prevPost) => ({ ...prevPost, reactions }))
39
  };
39
  }
40
 
40
 
Línea 41... Línea 41...
41
  const updateMyReaction = (reaction) => {
41
  const updateMyReaction = (reaction) => {
42
    mutate((prevPost) => ({ ...prevPost, my_reaction: reaction }));
42
    mutate((prevPost) => ({ ...prevPost, my_reaction: reaction }))
43
  };
43
  }
44
 
44
 
45
  const addComment = (comment) => {
45
  const addComment = (comment) => {
46
    const formData = new FormData();
46
    const formData = new FormData()
47
    formData.append("comment", comment);
47
    formData.append('comment', comment)
Línea 48... Línea 48...
48
 
48
 
49
    axios
49
    axios
50
      .post(post.comments_add_url, formData)
50
      .post(post.comments_add_url, formData)
51
      .then((response) => {
51
      .then((response) => {
52
        const { success, data } = response.data;
52
        const { success, data } = response.data
53
 
53
 
Línea 54... Línea 54...
54
        if (!success) {
54
        if (!success) {
55
          const errorMessage =
55
          const errorMessage =
56
            typeof data === "string"
56
            typeof data === 'string'
Línea 57... Línea 57...
57
              ? data
57
              ? data
58
              : "Error interno. Intente más tarde.";
58
              : 'Error interno. Intente más tarde.'
59
          throw new Error(errorMessage);
59
          throw new Error(errorMessage)
60
        }
60
        }
61
 
61
 
62
        setComments((prevComments) => [...prevComments, data]);
62
        setComments((prevComments) => [...prevComments, data])
63
      })
63
      })
64
      .catch((error) => {
64
      .catch((error) => {
65
        dispatch(addNotification({ style: "danger", msg: error.message }));
65
        dispatch(addNotification({ style: 'danger', msg: error.message }))