Proyectos de Subversion LeadersLinked - Backend

Rev

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

Rev Autor Línea Nro. Línea
6539 stevensc 1
import axios from 'axios'
15274 stevensc 2
import React, { useEffect } from 'react'
6539 stevensc 3
import { Modal, Button } from 'react-bootstrap'
4
import { getData } from '../../../helpers/fetchHelpers'
5
import { useForm } from 'react-hook-form'
14843 stevensc 6
import { addNotification } from '../../../redux/notification/notification.actions'
7
import { useDispatch } from 'react-redux'
6539 stevensc 8
 
9
const SocialNetworksModal = ({
11152 stevensc 10
	isOpen,
11
	closeModal,
12
	socialNetworksUrl,
13
	action
6539 stevensc 14
}) => {
15
 
11152 stevensc 16
	const { handleSubmit, setValue, register } = useForm()
14843 stevensc 17
	const dispatch = useDispatch()
6539 stevensc 18
 
11152 stevensc 19
	const onSubmit = ({ facebook, twitter, instagram }) => {
6539 stevensc 20
 
11152 stevensc 21
		const data = new FormData()
22
		data.append('facebook', facebook)
23
		data.append('twitter', twitter)
24
		data.append('instagram', instagram)
6539 stevensc 25
 
11152 stevensc 26
		axios.post(socialNetworksUrl, data)
27
			.then(({ data }) => {
28
				if (!data.success) {
14843 stevensc 29
					typeof data.data === 'string'
30
						?
31
						dispatch(addNotification({
32
							style: 'danger',
33
							msg: data.data
34
						}))
35
						: Object.entries(data.data).map(([key, value]) =>
36
							value.map(err =>
37
								dispatch(addNotification({
38
									style: 'danger',
39
									msg: `${key}: ${err}`
40
								}))
41
							)
42
						)
43
					return
11152 stevensc 44
				}
45
				action(data.data)
46
				closeModal()
47
			})
15274 stevensc 48
			.catch((err) => console.log(err))
11152 stevensc 49
	}
6539 stevensc 50
 
11152 stevensc 51
	useEffect(() => {
52
		getData(socialNetworksUrl)
53
			.then(({ facebook, twitter, instagram }) => {
54
				setValue('facebook', facebook)
55
				setValue('twitter', twitter)
56
				setValue('instagram', instagram)
57
			})
15274 stevensc 58
			.catch(err => console.log(err))
11152 stevensc 59
	}, [isOpen])
6539 stevensc 60
 
11152 stevensc 61
	return (
62
		<Modal
63
			size="md"
64
			show={isOpen}
65
			onHide={closeModal}
66
			autoFocus={false}
67
		>
68
			<Modal.Header closeButton>
15274 stevensc 69
				<Modal.Title>Redes Sociales</Modal.Title>
11152 stevensc 70
			</Modal.Header>
71
			<form onSubmit={handleSubmit(onSubmit)}>
72
				<Modal.Body>
15274 stevensc 73
					<div className="form-group">
11152 stevensc 74
						<label className="form-label">Facebook</label>
75
						<input
76
							type="text"
77
							className="form-control"
78
							name='facebook'
79
							ref={register}
80
						/>
81
					</div>
15274 stevensc 82
					<div className="form-group">
11152 stevensc 83
						<label className="form-label">Twitter</label>
84
						<input
85
							type="text"
86
							className="form-control"
87
							name='twitter'
88
							ref={register}
89
						/>
90
					</div>
15274 stevensc 91
					<div className="form-group">
11152 stevensc 92
						<label className="form-label">Instagram</label>
93
						<input
94
							type="text"
95
							className="form-control"
96
							name='instagram'
97
							ref={register}
98
						/>
99
					</div>
100
				</Modal.Body>
101
				<Modal.Footer>
102
					<Button
103
						variant="primary"
104
						type="submit"
105
					>
14843 stevensc 106
						Enviar
11152 stevensc 107
					</Button>
108
					<Button
109
						className='btn-tertiary'
110
						onClick={closeModal}
111
					>
14843 stevensc 112
						Cancelar
11152 stevensc 113
					</Button>
114
				</Modal.Footer>
115
			</form>
116
		</Modal >
117
	)
6539 stevensc 118
}
119
 
120
export default SocialNetworksModal