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