Proyectos de Subversion LeadersLinked - SPA

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
3031 stevensc 1
import { useEffect, useState } from 'react'
3028 stevensc 2
import { useDispatch } from 'react-redux'
3
 
4
import {
5
  addExperience,
6
  deleteExperience,
7
  editExperience
8
} from '@services/profiles'
9
import { addNotification } from '@store/notification/notification.actions'
10
 
11
export function useExperiences({ defaultExperiences = [] }) {
12
  const [experiences, setExperiences] = useState(defaultExperiences)
13
  const [modalState, setModalState] = useState(null)
14
  const [currentExperience, setCurrentExperience] = useState(null)
15
  const dispatch = useDispatch()
16
 
17
  const showModal = (type, experience = null) => {
18
    setCurrentExperience(experience)
19
    setModalState(modalState === type ? null : type)
20
  }
21
 
3029 stevensc 22
  const onAdd = async (uuid, experience) => {
3028 stevensc 23
    try {
3029 stevensc 24
      const newExperiences = await addExperience(uuid, experience)
3028 stevensc 25
      setExperiences(newExperiences)
26
      showModal(null)
27
    } catch (error) {
28
      dispatch(
29
        addNotification({
30
          style: 'danger',
31
          msg: `Error al agregar la experiencia: ${error.message}`
32
        })
33
      )
34
    }
35
  }
36
 
37
  const onEdit = async (experience) => {
38
    try {
39
      if (!currentExperience?.link_edit) {
40
        throw new Error('Enlace de edición no disponible')
41
      }
42
      const newExperiences = await editExperience(
43
        currentExperience.link_edit,
44
        experience
45
      )
46
      setExperiences(newExperiences)
47
      showModal(null)
48
    } catch (error) {
49
      dispatch(
50
        addNotification({
51
          style: 'danger',
52
          msg: `Error al editar la experiencia: ${error.message}`
53
        })
54
      )
55
    }
56
  }
57
 
58
  const onDelete = async () => {
59
    try {
60
      if (!currentExperience?.link_delete) {
61
        throw new Error('Enlace de eliminación no disponible')
62
      }
63
      const newExperiences = await deleteExperience(
64
        currentExperience.link_delete
65
      )
66
      setExperiences(newExperiences)
67
      showModal(null)
68
    } catch (error) {
69
      dispatch(
70
        addNotification({
71
          style: 'danger',
72
          msg: `Error al eliminar la experiencia: ${error.message}`
73
        })
74
      )
75
    }
76
  }
77
 
3031 stevensc 78
  useEffect(() => {
79
    setExperiences(defaultExperiences)
80
  }, [defaultExperiences])
81
 
3028 stevensc 82
  return {
83
    showModal,
84
    modalState,
85
    onAdd,
86
    onEdit,
87
    onDelete,
88
    experiences,
89
    currentExperience
90
  }
91
}