Proyectos de Subversion LeadersLinked - Backend

Rev

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

Rev Autor Línea Nro. Línea
6309 stevensc 1
import axios from 'axios'
15271 stevensc 2
import React, { useEffect } from 'react'
6309 stevensc 3
import { Modal, Button } from 'react-bootstrap'
4
import { useForm } from 'react-hook-form'
14843 stevensc 5
import { useDispatch } from 'react-redux'
6309 stevensc 6
import { getData } from '../../../helpers/fetchHelpers'
14843 stevensc 7
import { addNotification } from '../../../redux/notification/notification.actions'
6309 stevensc 8
 
9
const WebPageModal = ({
11152 stevensc 10
	isOpen,
11
	closeModal,
12
	websiteUrl,
13
	action
6309 stevensc 14
}) => {
15
 
11152 stevensc 16
	const { handleSubmit, register, setValue } = useForm()
14843 stevensc 17
	const dispatch = useDispatch()
6309 stevensc 18
 
11152 stevensc 19
	const onSubmit = ({ website }) => {
20
		const data = new FormData()
21
		data.append('website', website)
6309 stevensc 22
 
11152 stevensc 23
		axios.post(websiteUrl, data)
24
			.then(({ data }) => {
25
				if (!data.success) {
14843 stevensc 26
					typeof data.data === 'string'
27
						?
28
						dispatch(addNotification({
29
							style: 'danger',
30
							msg: data.data
31
						}))
32
						: Object.entries(data.data).map(([key, value]) =>
33
							value.map(err =>
34
								dispatch(addNotification({
35
									style: 'danger',
36
									msg: `${key}: ${err}`
37
								}))
38
							)
39
						)
40
					return
11152 stevensc 41
				}
42
				action(data.data.website)
43
				closeModal()
44
			})
45
			.catch((err) => console.log(err))
46
	}
6309 stevensc 47
 
11152 stevensc 48
	useEffect(() => {
49
		getData(websiteUrl)
50
			.then((resp) => setValue('website', resp.website))
51
			.catch(err => console.log(err))
52
	}, [isOpen])
6344 stevensc 53
 
11152 stevensc 54
	return (
55
		<Modal
56
			size="md"
57
			show={isOpen}
58
			onHide={closeModal}
59
			autoFocus={false}
60
		>
61
			<Modal.Header closeButton>
15227 stevensc 62
				<Modal.Title>Página web</Modal.Title>
11152 stevensc 63
			</Modal.Header>
64
			<form onSubmit={handleSubmit(onSubmit)}>
65
				<Modal.Body>
15275 stevensc 66
					<div className="form-group">
11152 stevensc 67
						<input
68
							className="form-control form-control-sm"
69
							type="text"
70
							name="website"
71
							ref={register}
72
						/>
73
					</div>
74
				</Modal.Body>
75
				<Modal.Footer>
76
					<Button
77
						variant="primary"
78
						type="submit"
79
					>
14843 stevensc 80
						Enviar
11152 stevensc 81
					</Button>
82
					<Button
83
						className='btn-tertiary'
84
						onClick={closeModal}
85
					>
14843 stevensc 86
						Cancelar
11152 stevensc 87
					</Button>
88
				</Modal.Footer>
89
			</form>
90
		</Modal >
91
	)
6309 stevensc 92
}
93
 
94
export default WebPageModal