Proyectos de Subversion LeadersLinked - SPA

Rev

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

Rev 2190 Rev 2191
Línea 1... Línea 1...
1
import { useEffect } 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 { addNotification } from '@app/redux/notification/notification.actions'
5
import { addNotification } from '@app/redux/notification/notification.actions'
Línea 6... Línea 6...
6
import useFetch from './useFetch'
6
import useFetch from './useFetch'
7
 
7
 
-
 
8
export default function usePosts(postUrl) {
Línea 8... Línea 9...
8
export default function usePosts(postUrl) {
9
  const { data: post, isLoading, mutate } = useFetch(postUrl)
Línea 9... Línea 10...
9
  const { data: post, isLoading, mutate } = useFetch(postUrl)
10
  const [comments, setComments] = useState([])
10
 
11
 
Línea 20... Línea 21...
20
 
21
 
21
        dispatch(addNotification({ style: 'danger', msg: errorMessage }))
22
        dispatch(addNotification({ style: 'danger', msg: errorMessage }))
22
        return
23
        return
Línea 23... Línea 24...
23
      }
24
      }
24
 
25
 
25
      mutate((prevPost) => ({ ...prevPost, comments: data }))
26
      setComments(data)
Línea 26... Línea 27...
26
    })
27
    })
27
  }
28
  }
Línea 56... Línea 57...
56
              ? data
57
              ? data
57
              : 'Error interno. Intente más tarde.'
58
              : 'Error interno. Intente más tarde.'
58
          throw new Error(errorMessage)
59
          throw new Error(errorMessage)
59
        }
60
        }
Línea 60... Línea -...
60
 
-
 
61
        mutate((prevPost) => ({
-
 
62
          ...prevPost,
61
 
63
          comments: [...prevPost.comments, data]
-
 
64
        }))
62
        setComments((prevComments) => [...prevComments, data])
65
      })
63
      })
66
      .catch((error) => {
64
      .catch((error) => {
67
        dispatch(addNotification({ style: 'danger', msg: error.message }))
65
        dispatch(addNotification({ style: 'danger', msg: error.message }))
68
      })
66
      })
Línea 71... Línea 69...
71
  useEffect(() => {
69
  useEffect(() => {
72
    if (post.comments_url) getComments(post.comments_url)
70
    if (post.comments_url) getComments(post.comments_url)
73
  }, [post])
71
  }, [post])
Línea 74... Línea 72...
74
 
72
 
75
  return {
73
  return {
76
    post,
74
    post: { ...post, comments },
77
    isLoading,
75
    isLoading,
78
    updateTotalShare,
76
    updateTotalShare,
79
    addComment,
77
    addComment,
80
    updateReactions,
78
    updateReactions,