Proyectos de Subversion LeadersLinked - Backend

Rev

Rev 14843 | Rev 15274 | Ir a la última revisión | | Comparar con el anterior | Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
6547 stevensc 1
import axios from 'axios'
2
import React, { useState } from 'react'
3
import { Modal, Button } from 'react-bootstrap'
4
import { useForm } from 'react-hook-form'
14843 stevensc 5
import { useDispatch } from 'react-redux'
6
import { addNotification } from '../../../redux/notification/notification.actions'
6547 stevensc 7
 
8
const CompanyImgModal = ({
11152 stevensc 9
	isOpen,
10
	closeModal,
11
	imageUploadUrl,
12
	action
6547 stevensc 13
}) => {
14
 
11152 stevensc 15
	const { handleSubmit, register } = useForm()
16
	const [error, setError] = useState(null)
14843 stevensc 17
	const dispatch = useDispatch()
6547 stevensc 18
 
11152 stevensc 19
	const onSubmit = ({ image }) => {
20
		const data = new FormData()
21
		data.append('image', image[0])
22
		axios.post(imageUploadUrl, data)
23
			.then(({ data }) => {
24
				if (!data.success) {
14843 stevensc 25
					typeof data.data === 'string'
26
						?
27
						dispatch(addNotification({
28
							style: 'danger',
29
							msg: data.data
30
						}))
31
						: Object.entries(data.data).map(([key, value]) =>
32
							value.map(err =>
33
								dispatch(addNotification({
34
									style: 'danger',
35
									msg: `${key}: ${err}`
36
								}))
37
							)
38
						)
39
					return
11152 stevensc 40
				}
41
				action(data.data)
42
				setError(null)
43
				closeModal()
44
			})
45
			.catch((err) => console.log(err))
46
	}
6547 stevensc 47
 
11152 stevensc 48
	return (
49
		<Modal
50
			size="md"
51
			show={isOpen}
52
			onHide={closeModal}
53
			autoFocus={false}
54
		>
55
			<Modal.Header closeButton>
15230 stevensc 56
				<Modal.Title>Foto de perfil</Modal.Title>
11152 stevensc 57
			</Modal.Header>
58
			<form onSubmit={handleSubmit(onSubmit)}>
59
				<Modal.Body>
60
					<div className="mb-3">
61
						<label className="form-label">Imagen recomendada de (180 x 180px)</label>
62
						<input
63
							className="form-control form-control-sm"
64
							id="formFileSm"
65
							type="file"
66
							name="image"
67
							accept='.jpg, .jpeg, .png, .gif'
68
							ref={register}
69
							style={{ padding: '1px' }}
70
						/>
71
					</div>
72
					{error && <p>{error}</p>}
73
				</Modal.Body>
74
				<Modal.Footer>
75
					<Button
76
						variant="primary"
77
						type="submit"
78
					>
14843 stevensc 79
						Enviar
11152 stevensc 80
					</Button>
81
					<Button
82
						className='btn-tertiary'
83
						onClick={closeModal}
84
					>
14843 stevensc 85
						Cancelar
11152 stevensc 86
					</Button>
87
				</Modal.Footer>
88
			</form>
89
		</Modal >
90
	)
6547 stevensc 91
}
92
 
93
export default CompanyImgModal