Proyectos de Subversion LeadersLinked - SPA

Rev

Ir a la última revisión | | Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
3059 stevensc 1
import { useEffect, useState } from 'react'
2
import { useDispatch } from 'react-redux'
3
 
4
import {
5
  addExperience,
6
  deleteExperience,
7
  editExperience
8
} from '@services/profile/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
 
22
  const onAdd = async (uuid, experience) => {
23
    try {
24
      const newExperiences = await addExperience(uuid, experience)
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(addNotification({ style: 'danger', msg: error.message }))
50
    }
51
  }
52
 
53
  const onDelete = async () => {
54
    try {
55
      if (!currentExperience?.link_delete) {
56
        throw new Error('Enlace de eliminación no disponible')
57
      }
58
      const newExperiences = await deleteExperience(
59
        currentExperience.link_delete
60
      )
61
      setExperiences(newExperiences)
62
      showModal(null)
63
    } catch (error) {
64
      dispatch(
65
        addNotification({
66
          style: 'danger',
67
          msg: `Error al eliminar la experiencia: ${error.message}`
68
        })
69
      )
70
    }
71
  }
72
 
73
  useEffect(() => {
74
    setExperiences(defaultExperiences)
75
  }, [defaultExperiences])
76
 
77
  return {
78
    showModal,
79
    modalState,
80
    onAdd,
81
    onEdit,
82
    onDelete,
83
    experiences,
84
    currentExperience
85
  }
86
}