Proyectos de Subversion LeadersLinked - SPA

Rev

Rev 3059 | Mostrar el archivo completo | | | Autoría | Ultima modificación | Ver Log |

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