Proyectos de Subversion LeadersLinked - SPA

Rev

Rev 3059 | | Comparar con el anterior | Ultima modificación | Ver Log |

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