Proyectos de Subversion LeadersLinked - Antes de SPA

Rev

Rev 4600 | Ir a la última revisión | | Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
4599 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 { axios } from "../../../../../utils";
6
import Spinner from "../../../../loading-spinner/Spinner";
7
import DropzoneComponent from "../../../../dropzone/DropzoneComponent";
8
import FormErrorFeedback from "../../../../form-error-feedback/FormErrorFeedback";
9
import { addNotification } from "../../../../../redux/notification/notification.actions";
10
import { profileTypes } from "../../../Profile.types";
11
 
12
const ImageModal = ({
13
    isModalOpen,
14
    handleModalOpen,
15
    imageProfileCover,
16
    profileType,
17
    profileId,
18
    setProfileImg
19
}) => {
20
    const { register, errors, handleSubmit, setValue, clearErrors, setError, getValues } = useForm();
21
    const [loading, setLoading] = useState(false);
22
 
23
    const onUploadedHandler = (files) => {
24
        setValue("image", files);
25
        clearErrors("image");
26
    };
27
 
28
    const dropZoneRender = () => (
29
        <DropzoneComponent
30
            modalType="IMAGE"
31
            onUploaded={onUploadedHandler}
32
            recomendationText={`Imágenes recomendadas de ${imageProfileCover}`}
33
        />
34
    );
35
 
36
    const onSubmitHandler = async (data) => {
37
        let postPath = "";
38
        switch (profileType) {
39
            case profileTypes.USER:
40
                postPath = `/profile/my-profiles/image/${profileId}/operation/upload`;
41
                break;
42
            case profileTypes.COMPANY:
43
                postPath = `/my-company/${profileId}/profile/image/upload`;
44
                break;
45
            default:
46
                break;
47
        }
48
        ("called");
49
        setLoading(true);
50
        const formData = new FormData();
51
        Object.entries(data).map(([key, value]) => {
52
            formData.append(key, value);
53
        });
54
        await axios.post(postPath, formData).then((response) => {
55
            const resData = response.data;
56
            (resData);
57
            if (resData.success) {
58
                setLoading(false);
59
                const newCoverImg = {
60
                    path: resData.data.profile,
61
                    uid: Date.now(),
62
                };
63
                if (resData.data.update_navbar) sessionStorage.setItem('user_session_image', resData.data.user)
64
                setProfileImg(newCoverImg);
65
                setValue("image", "");
66
                handleModalOpen();
67
            } else {
68
                const resError = resData.data;
69
                if (resError.constructor.name === "Object") {
70
                    Object.entries(resError).map(([key, value]) => {
71
                        if (key in getValues()) {
72
                            setError(key, {
73
                                type: "manual",
74
                                message: Array.isArray(value) ? value[0] : value,
75
                            });
76
                        }
77
                    });
78
                } else {
79
                    addNotification({
80
                        style: "danger",
81
                        msg: resError,
82
                    });
83
                }
84
            }
85
        });
86
        setLoading(false);
87
    };
88
 
89
    useEffect(() => {
90
        register("image", {
91
            required: { value: "true", message: "El campo es requerido" },
92
        })
93
    }, [])
94
 
95
    return (
96
        <Modal show={isModalOpen} onHide={handleModalOpen}>
97
            <Modal.Header closeButton>
98
                <Modal.Title>Portada</Modal.Title>
99
            </Modal.Header>
100
            <form
101
                encType="multipart/form-data"
102
                onSubmit={handleSubmit(onSubmitHandler)}
103
            >
104
                <Modal.Body>
105
                    {dropZoneRender()}
106
                    {errors.image && <FormErrorFeedback>{errors.image.message}</FormErrorFeedback>}
107
                </Modal.Body>
108
                <Modal.Footer>
109
                    <Button type="submit">Enviar</Button>
110
                    <Button variant="danger" onClick={handleModalOpen}>
111
                        Cancelar
112
                    </Button>
113
                </Modal.Footer>
114
            </form>
115
            {loading && <Spinner />}
116
        </Modal>
117
    )
118
}
119
 
120
export default ImageModal