Proyectos de Subversion LeadersLinked - SPA

Rev

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

Rev Autor Línea Nro. Línea
3719 stevensc 1
import React, { useEffect, useMemo } from 'react';
2
import { useForm } from 'react-hook-form';
3
 
4
import { useFetchHelper } from '@hooks';
5
 
6
import Modal from '@app/components/UI/modal/Modal';
7
import TagsInput from '@app/components/UI/TagsInput';
8
 
9
const HobbiesModal = ({
10
  show = false,
11
  hobbies: userHobbies = [],
12
  onClose = () => {},
13
  onConfirm = () => {}
14
}) => {
15
  const { data: hobbies = [] } = useFetchHelper('hobbies');
16
 
17
  const { register, handleSubmit, setValue } = useForm();
18
 
19
  const currentValues = useMemo(() => {
20
    return userHobbies?.map(({ value }) => value) || [];
21
  }, [userHobbies]);
22
 
23
  const handleConfirm = handleSubmit((data) => onConfirm?.(data));
24
 
25
  useEffect(() => {
26
    register('hobbies');
27
  }, [register]);
28
 
29
  useEffect(() => {
30
    show ? setValue('hobbies', currentValues) : setValue('hobbies', ['']);
31
  }, [show, currentValues]);
32
 
33
  return (
34
    <Modal title='Hobbies' show={show} onClose={onClose} onAccept={handleConfirm}>
35
      <TagsInput
36
        label='Seleccionar hobbies'
37
        name='hobbies'
38
        options={hobbies}
39
        defaultValues={currentValues}
40
        onChange={(tags) => setValue('hobbies', tags)}
41
      />
42
    </Modal>
43
  );
44
};
45
 
46
export default HobbiesModal;