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 SkillsModal = ({ show = false, skills: userSkills = [], onConfirm, onClose }) => {
|
|
|
10 |
const { data: skills = [] } = useFetchHelper('skills');
|
|
|
11 |
|
|
|
12 |
const { register, handleSubmit, setValue } = useForm();
|
|
|
13 |
|
|
|
14 |
const currentValues = useMemo(() => {
|
|
|
15 |
return userSkills?.map(({ value }) => value) || [];
|
|
|
16 |
}, [userSkills]);
|
|
|
17 |
|
|
|
18 |
const handleConfirm = handleSubmit((data) => onConfirm?.(data));
|
|
|
19 |
|
|
|
20 |
useEffect(() => {
|
|
|
21 |
register('skills');
|
|
|
22 |
}, [register]);
|
|
|
23 |
|
|
|
24 |
useEffect(() => {
|
|
|
25 |
show ? setValue('skills', currentValues) : setValue('skills', ['']);
|
|
|
26 |
}, [show, currentValues]);
|
|
|
27 |
|
|
|
28 |
return (
|
|
|
29 |
<Modal title='Habilidades' show={show} onClose={onClose} onAccept={handleConfirm}>
|
|
|
30 |
<TagsInput
|
|
|
31 |
label='Seleccionar habilidades'
|
|
|
32 |
name='skills'
|
|
|
33 |
options={skills}
|
|
|
34 |
defaultValues={currentValues}
|
|
|
35 |
onChange={(tags) => setValue('skills', tags)}
|
|
|
36 |
/>
|
|
|
37 |
</Modal>
|
|
|
38 |
);
|
|
|
39 |
};
|
|
|
40 |
|
|
|
41 |
export default SkillsModal;
|