Proyectos de Subversion LeadersLinked - Backend

Rev

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

Rev Autor Línea Nro. Línea
5949 stevensc 1
import axios from 'axios'
6199 stevensc 2
import React, { useState } from 'react'
6287 stevensc 3
import { Modal, Button } from 'react-bootstrap'
5949 stevensc 4
import { useForm } from 'react-hook-form'
14843 stevensc 5
import { useDispatch } from 'react-redux'
6
import { addNotification } from '../../../redux/notification/notification.actions'
5949 stevensc 7
 
8
const CoverModal = ({
11152 stevensc 9
	isOpen,
10
	closeModal,
11
	coverUploadUrl,
12
	action
5949 stevensc 13
}) => {
14
 
11152 stevensc 15
	const { handleSubmit, register } = useForm()
16
	const [error, setError] = useState(null)
14843 stevensc 17
	const dispatch = useDispatch()
5949 stevensc 18
 
11152 stevensc 19
	const onSubmit = ({ cover }) => {
20
		const data = new FormData()
21
		data.append('cover', cover[0])
22
		axios.post(coverUploadUrl, 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
	}
5949 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>
15266 stevensc 56
				<Modal.Title>Portada - Imagen recomendada de (2480 x 268px)</Modal.Title>
11152 stevensc 57
			</Modal.Header>
58
			<form onSubmit={handleSubmit(onSubmit)}>
59
				<Modal.Body>
15266 stevensc 60
					<div className="form-group">
11152 stevensc 61
						<input
62
							className="form-control form-control-sm"
63
							id="formFileSm"
64
							type="file"
65
							name="cover"
66
							accept='.jpg, .jpeg, .png, .gif'
67
							ref={register}
68
							style={{ padding: '1px' }}
69
						/>
70
					</div>
71
					{error && <p>{error}</p>}
72
				</Modal.Body>
73
				<Modal.Footer>
74
					<Button
75
						variant="primary"
76
						type="submit"
77
					>
14843 stevensc 78
						Enviar
11152 stevensc 79
					</Button>
80
					<Button
81
						className='btn-tertiary'
82
						onClick={closeModal}
83
					>
14843 stevensc 84
						Cancelar
11152 stevensc 85
					</Button>
86
				</Modal.Footer>
87
			</form>
88
		</Modal >
89
	)
5949 stevensc 90
}
91
 
92
export default CoverModal