Proyectos de Subversion LeadersLinked - SPA

Rev

Rev 2798 | Rev 3432 | Ir a la última revisión | | Comparar con el anterior | Ultima modificación | Ver Log |

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