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