Proyectos de Subversion LeadersLinked - Backend

Rev

Rev 8372 | Rev 8374 | Ir a la última revisión | | Comparar con el anterior | Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
8110 stevensc 1
import React from "react";
2
import { useState, useEffect } from "react";
3
import { useForm } from "react-hook-form";
4
import TagsInput from "../../../shared/TagsInput";
5
import SubmitModal from "./SubmitModal";
8368 stevensc 6
import { getData } from "../../../helpers/fetchHelpers"
7771 stevensc 7
 
8110 stevensc 8
 
8107 stevensc 9
const LanguageModal = ({
10
    closeModal,
8372 stevensc 11
    dataLink
8107 stevensc 12
}) => {
7771 stevensc 13
 
8110 stevensc 14
    const {
15
        register,
8138 stevensc 16
        errors
8110 stevensc 17
    } = useForm();
18
 
8153 stevensc 19
    const [userLanguagesArray, setUserLanguagesArray] = useState([]);
8110 stevensc 20
    const [languagesOptionsArray, setLanguagesOptionsArray] = useState([]);
8138 stevensc 21
    const [data, setData] = useState({ languages: "" });
7771 stevensc 22
 
23
    useEffect(() => {
8064 stevensc 24
        register("languages", {
25
            required: "Por favor seleccione al menos un lenguaje",
26
        });
27
    }, []);
7771 stevensc 28
 
29
    const handleTagsChange = (tags) => {
30
        if (tags.length) {
31
            let newTags = [];
32
            tags.map((tag) => {
33
                newTags = [...newTags, tag.value];
34
            });
8141 stevensc 35
            setData({ "languages[]": newTags });
7771 stevensc 36
        } else {
8141 stevensc 37
            setData({ "languages[]": "" });
7771 stevensc 38
        }
39
    };
40
 
8064 stevensc 41
    useEffect(() => {
8372 stevensc 42
        getData(dataLink)
43
            .then(({ languages_available, languages_selected }) => {
8147 stevensc 44
 
8372 stevensc 45
                // Set languages options
46
                Object.entries(languages_available).map(([key, value]) => {
8370 stevensc 47
                    setLanguagesOptionsArray(prev => [...prev, { value: key, name: value }])
48
                })
8372 stevensc 49
 
50
                // Set languages selecteds
51
                languages_selected.map(lang => {
8373 stevensc 52
                    console.log(languagesOptionsArray)
8372 stevensc 53
                    const userLang = languagesOptionsArray.find(el => el.name === lang)
8373 stevensc 54
                    console.log(userLang)
55
 
8372 stevensc 56
                    if (userLang) {
57
                        setUserLanguagesArray(prev => [...prev, { value: userLang.value, name: userLang.name }])
58
                    }
59
                })
8370 stevensc 60
            })
61
    }, [dataLink]);
8368 stevensc 62
 
7771 stevensc 63
    return (
7773 stevensc 64
        <SubmitModal
8110 stevensc 65
            title="Idiomas"
8139 stevensc 66
            submitData={data}
8110 stevensc 67
            postLink={dataLink}
8064 stevensc 68
            closeModal={closeModal}
7773 stevensc 69
        >
8110 stevensc 70
            <div className="form-group">
71
                <TagsInput
72
                    suggestions={languagesOptionsArray}
73
                    settedTags={userLanguagesArray}
74
                    onChange={handleTagsChange}
75
                />
8372 stevensc 76
                {errors.languages && <p>{errors.languages}</p>}
8110 stevensc 77
            </div>
7771 stevensc 78
        </SubmitModal>
8110 stevensc 79
    );
80
};
81
 
8372 stevensc 82
export default LanguageModal;