| 6770 |
stevensc |
1 |
import React, { useState, useEffect } from 'react'
|
|
|
2 |
import { axios } from '../../utils'
|
|
|
3 |
import { useForm } from 'react-hook-form'
|
| 6817 |
stevensc |
4 |
import { useParams } from 'react-router-dom'
|
| 6770 |
stevensc |
5 |
import { useDispatch, useSelector } from 'react-redux'
|
|
|
6 |
import { Button, Modal } from 'react-bootstrap'
|
|
|
7 |
import { addNotification } from '../../redux/notification/notification.actions'
|
|
|
8 |
|
|
|
9 |
import Spinner from '../UI/Spinner'
|
|
|
10 |
import DropzoneComponent from '../dropzone/DropzoneComponent'
|
|
|
11 |
import FormErrorFeedback from '../UI/FormErrorFeedback'
|
|
|
12 |
|
|
|
13 |
const CoverModal = ({ isOpen, sizes, onClose, onComplete }) => {
|
|
|
14 |
const [loading, setLoading] = useState(false)
|
|
|
15 |
const { uuid } = useParams()
|
| 6778 |
stevensc |
16 |
const labels = useSelector(({ intl }) => intl.labels)
|
| 6770 |
stevensc |
17 |
const dispatch = useDispatch()
|
|
|
18 |
|
|
|
19 |
const typesUrl = {
|
|
|
20 |
user: `/profile/my-profiles/cover/${uuid}/operation/upload`,
|
|
|
21 |
company: `/my-company/${uuid}/profile/cover/upload`,
|
|
|
22 |
group: `/group/my-groups/cover/${uuid}/operation/upload`,
|
|
|
23 |
}
|
|
|
24 |
|
|
|
25 |
const { register, errors, handleSubmit, setValue, clearErrors, setError } =
|
|
|
26 |
useForm()
|
|
|
27 |
|
|
|
28 |
const onUploadedHandler = (files) => {
|
|
|
29 |
setValue('cover', files)
|
|
|
30 |
clearErrors('cover')
|
|
|
31 |
}
|
|
|
32 |
|
|
|
33 |
const onSubmitHandler = ({ cover }) => {
|
|
|
34 |
setLoading(true)
|
|
|
35 |
|
|
|
36 |
const formData = new FormData()
|
|
|
37 |
formData.append('cover', cover)
|
| 6817 |
stevensc |
38 |
const type = window.location.pathname.split('/')[1]
|
| 6770 |
stevensc |
39 |
|
| 6828 |
stevensc |
40 |
console.log(type)
|
|
|
41 |
|
| 6770 |
stevensc |
42 |
axios
|
| 6828 |
stevensc |
43 |
.post(typesUrl.user, formData)
|
| 6770 |
stevensc |
44 |
.then(({ data: response }) => {
|
|
|
45 |
const { data, success } = response
|
|
|
46 |
|
|
|
47 |
if (!success) {
|
|
|
48 |
typeof data === 'string'
|
|
|
49 |
? dispatch(addNotification({ style: 'danger', msg: data }))
|
|
|
50 |
: Object.entries(data).map(([key, value]) =>
|
|
|
51 |
setError(key, value[0])
|
|
|
52 |
)
|
|
|
53 |
return
|
|
|
54 |
}
|
|
|
55 |
|
|
|
56 |
onComplete(data)
|
|
|
57 |
setValue('cover', '')
|
|
|
58 |
onClose()
|
|
|
59 |
})
|
|
|
60 |
.finally(() => setLoading(false))
|
|
|
61 |
}
|
|
|
62 |
|
|
|
63 |
useEffect(() => {
|
|
|
64 |
register('cover', {
|
|
|
65 |
required: 'Este campo es requerido',
|
|
|
66 |
})
|
|
|
67 |
}, [])
|
|
|
68 |
|
|
|
69 |
return (
|
|
|
70 |
<Modal show={isOpen} onHide={onClose}>
|
|
|
71 |
<form onSubmit={handleSubmit(onSubmitHandler)}>
|
|
|
72 |
<Modal.Header closeButton>
|
|
|
73 |
<Modal.Title>{labels.cover}</Modal.Title>
|
|
|
74 |
</Modal.Header>
|
|
|
75 |
<Modal.Body>
|
|
|
76 |
{loading && <Spinner />}
|
|
|
77 |
<DropzoneComponent
|
|
|
78 |
modalType="IMAGE"
|
|
|
79 |
onUploaded={onUploadedHandler}
|
|
|
80 |
recomendationText={`Imágenes recomendadas de ${sizes}`}
|
|
|
81 |
/>
|
|
|
82 |
{errors.cover && (
|
|
|
83 |
<FormErrorFeedback>{errors.cover.message}</FormErrorFeedback>
|
|
|
84 |
)}
|
|
|
85 |
</Modal.Body>
|
|
|
86 |
<Modal.Footer>
|
|
|
87 |
<Button size="sm" type="submit">
|
|
|
88 |
{labels.accept}
|
|
|
89 |
</Button>
|
|
|
90 |
<Button size="sm" variant="danger" onClick={onClose}>
|
|
|
91 |
{labels.cancel}
|
|
|
92 |
</Button>
|
|
|
93 |
</Modal.Footer>
|
|
|
94 |
</form>
|
|
|
95 |
</Modal>
|
|
|
96 |
)
|
|
|
97 |
}
|
|
|
98 |
|
|
|
99 |
export default CoverModal
|