Proyectos de Subversion LeadersLinked - Antes de SPA

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
5473 stevensc 1
/* eslint-disable react/prop-types */
2
import React, { useEffect, useState } from 'react'
3
import { Button, Modal } from 'react-bootstrap'
4
import { useForm } from 'react-hook-form'
5
import { useDispatch } from 'react-redux'
6
import { addNotification } from '../../../../redux/notification/notification.actions'
7
import FormErrorFeedback from '../../../../shared/form-error-feedback/FormErrorFeedback'
8
import Spinner from '../../../../shared/loading-spinner/Spinner'
9
import TagsInput from '../../../../shared/tags-input/TagsInput'
10
import { axios } from '../../../../utils'
11
 
12
const SkillsModal = ({ setSkills, userIdEncrypted, closeModal, show, skillsOptions, userSkillsArray }) => {
13
 
14
    const { register, errors, handleSubmit, setValue, getValues, setError } = useForm();
15
    const [settedSkillTags, setSettedSkillTags] = useState([]);
16
    const [modalLoading, setModalLoading] = useState(false);
17
    const dispatch = useDispatch();
18
 
19
    const onSubmitHandler = async () => {
20
        setModalLoading(true);
21
        const formData = new FormData();
22
        getValues("skills").map((language) => formData.append("skills[]", language));
23
 
24
        await axios
25
            .post(`/profile/my-profiles/skill/${userIdEncrypted}`, formData)
26
            .then(({ data: response }) => {
27
 
28
                if (!response.success) {
29
                    const responseError = response.data;
30
 
31
                    if (responseError.constructor.name === "string") {
32
                        dispatch(addNotification({ style: "danger", msg: responseError }))
33
                        return
34
                    }
35
 
36
                    Object.entries(responseError)
37
                    .map(([key, value]) => {
38
                        if (key in getValues()) {
39
                            setError(key, {type: "manual",message: Array.isArray(value) ? value[0] : value});
40
                        }
41
                    });
42
                }
43
 
44
                setSkills(settedSkillTags);
45
                dispatch(addNotification({ style: "success", msg: 'Registro agregado' }))
46
 
47
                handleModalOpen()
48
            })
49
            .finally(() => setModalLoading(false))
50
    };
51
 
52
    const handleTagsChange = (tags) => {
53
        if (tags.length) {
54
            let newTags = [];
55
            tags.map((tag) => {
56
                newTags = [...newTags, tag.value];
57
            });
58
            setValue("skills", newTags);
59
            setSettedSkillTags(tags);
60
        } else {
61
            setValue("skills", "");
62
            setSettedSkillTags("");
63
        }
64
    };
65
 
66
    const handleModalOpen = () => {
67
        Object.keys(getValues()).map(([key]) => setValue(key, ""))
68
        closeModal()
69
    }
70
 
71
    useEffect(() => {
72
        register("skills", {
73
            required: "Por favor seleccione al menos una habilidad",
74
        });
75
    }, []);
76
 
77
    return (
78
        <Modal show={show} onHide={handleModalOpen}>
79
            <Modal.Header closeButton>
80
                <Modal.Title>Habilidades</Modal.Title>
81
            </Modal.Header>
82
            <form onSubmit={handleSubmit(onSubmitHandler)}>
83
                <Modal.Body>
84
                    <div className="form-group">
85
                        <TagsInput
86
                            suggestions={skillsOptions}
87
                            settedTags={userSkillsArray}
88
                            onChange={handleTagsChange}
89
                        />
90
                        {errors.skills && <FormErrorFeedback>{errors.skills.message}</FormErrorFeedback>}
91
                    </div>
92
                </Modal.Body>
93
                <Modal.Footer>
94
                    <Button size="sm" type="submit">Enviar</Button>
95
                    <Button size="sm" variant="danger" onClick={handleModalOpen}>
96
                        Cancelar
97
                    </Button>
98
                </Modal.Footer>
99
            </form>
100
            {modalLoading && <Spinner />}
101
        </Modal>
102
    )
103
}
104
 
105
export default SkillsModal