| 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";
|
| 4600 |
stevensc |
11 |
import { useDispatch } from 'react-redux';
|
| 4599 |
stevensc |
12 |
|
|
|
13 |
const ImageModal = ({
|
|
|
14 |
isModalOpen,
|
|
|
15 |
handleModalOpen,
|
|
|
16 |
imageProfileCover,
|
|
|
17 |
profileType,
|
|
|
18 |
profileId,
|
|
|
19 |
setProfileImg
|
|
|
20 |
}) => {
|
|
|
21 |
const { register, errors, handleSubmit, setValue, clearErrors, setError, getValues } = useForm();
|
|
|
22 |
const [loading, setLoading] = useState(false);
|
| 4600 |
stevensc |
23 |
const dispatch = useDispatch();
|
| 4599 |
stevensc |
24 |
|
|
|
25 |
const onUploadedHandler = (files) => {
|
|
|
26 |
setValue("image", files);
|
|
|
27 |
clearErrors("image");
|
|
|
28 |
};
|
|
|
29 |
|
|
|
30 |
const dropZoneRender = () => (
|
|
|
31 |
<DropzoneComponent
|
|
|
32 |
modalType="IMAGE"
|
|
|
33 |
onUploaded={onUploadedHandler}
|
|
|
34 |
recomendationText={`Imágenes recomendadas de ${imageProfileCover}`}
|
|
|
35 |
/>
|
|
|
36 |
);
|
|
|
37 |
|
|
|
38 |
const onSubmitHandler = async (data) => {
|
|
|
39 |
let postPath = "";
|
|
|
40 |
switch (profileType) {
|
|
|
41 |
case profileTypes.USER:
|
|
|
42 |
postPath = `/profile/my-profiles/image/${profileId}/operation/upload`;
|
|
|
43 |
break;
|
|
|
44 |
case profileTypes.COMPANY:
|
|
|
45 |
postPath = `/my-company/${profileId}/profile/image/upload`;
|
|
|
46 |
break;
|
|
|
47 |
default:
|
|
|
48 |
break;
|
|
|
49 |
}
|
|
|
50 |
("called");
|
|
|
51 |
setLoading(true);
|
|
|
52 |
const formData = new FormData();
|
|
|
53 |
Object.entries(data).map(([key, value]) => {
|
|
|
54 |
formData.append(key, value);
|
|
|
55 |
});
|
|
|
56 |
await axios.post(postPath, formData).then((response) => {
|
|
|
57 |
const resData = response.data;
|
|
|
58 |
(resData);
|
|
|
59 |
if (resData.success) {
|
|
|
60 |
setLoading(false);
|
|
|
61 |
const newCoverImg = {
|
|
|
62 |
path: resData.data.profile,
|
|
|
63 |
uid: Date.now(),
|
|
|
64 |
};
|
|
|
65 |
if (resData.data.update_navbar) sessionStorage.setItem('user_session_image', resData.data.user)
|
|
|
66 |
setProfileImg(newCoverImg);
|
|
|
67 |
setValue("image", "");
|
| 4600 |
stevensc |
68 |
dispatch(addNotification({ style: "success", msg: 'Registro actualizado' }))
|
| 4599 |
stevensc |
69 |
handleModalOpen();
|
|
|
70 |
} else {
|
|
|
71 |
const resError = resData.data;
|
|
|
72 |
if (resError.constructor.name === "Object") {
|
|
|
73 |
Object.entries(resError).map(([key, value]) => {
|
|
|
74 |
if (key in getValues()) {
|
|
|
75 |
setError(key, {
|
|
|
76 |
type: "manual",
|
|
|
77 |
message: Array.isArray(value) ? value[0] : value,
|
|
|
78 |
});
|
|
|
79 |
}
|
|
|
80 |
});
|
|
|
81 |
} else {
|
| 4600 |
stevensc |
82 |
dispatch(addNotification({ style: "danger", msg: resError }))
|
| 4599 |
stevensc |
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>
|
| 4606 |
stevensc |
98 |
<Modal.Title>Imagen</Modal.Title>
|
| 4599 |
stevensc |
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
|