Proyectos de Subversion LeadersLinked - Backend

Rev

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

Rev Autor Línea Nro. Línea
7774 stevensc 1
import React from 'react'
2
import { useState } from 'react'
3
import SearchLocationInput from '../../../shared/SearchLocationInput'
14738 stevensc 4
import { addNotification } from '../../../redux/notification/notification.actions'
5
import axios from 'axios'
6
import { useDispatch } from 'react-redux'
7
import { Button, Modal } from 'react-bootstrap'
7774 stevensc 8
 
9
const LocationModal = ({
11152 stevensc 10
	closeModal,
11
	dataLink,
12
	googleApiKey,
14738 stevensc 13
	defaultData = '',
14
	onComplete = function () { }
7774 stevensc 15
}) => {
16
 
11152 stevensc 17
	const [isActive, setIsActive] = useState((defaultData.is_main === 'y') ? true : false)
18
	const [value, setValue] = useState(defaultData.formatted_address)
19
	const [data, setData] = useState({})
15225 stevensc 20
	const [loading, setLoading] = useState(false)
14738 stevensc 21
	const dispatch = useDispatch()
8042 stevensc 22
 
14738 stevensc 23
	const onSubmit = () => {
15225 stevensc 24
		setLoading(true)
14738 stevensc 25
		const values = Object.values(data)
26
 
27
		if (!values.length) {
28
			return dispatch(addNotification({
29
				style: 'danger',
30
				msg: 'El campo no puede estar vacio.'
31
			}))
32
		}
33
 
34
		const formData = new FormData()
35
		Object.entries({ ...data, is_main: isActive ? 'y' : 'n' }).forEach((entries) => {
36
			formData.append(entries[0], entries[1])
37
		})
38
 
39
		axios.post(dataLink, formData)
40
			.then(({ data }) => {
41
				if (!data.success) {
14843 stevensc 42
					typeof data.data === 'string'
43
						?
44
						dispatch(addNotification({
45
							style: 'danger',
46
							msg: data.data
47
						}))
48
						: Object.entries(data.data).map(([key, value]) =>
49
							value.map(err =>
50
								dispatch(addNotification({
51
									style: 'danger',
52
									msg: `${key}: ${err}`
53
								}))
54
							)
55
						)
56
					return
14738 stevensc 57
				}
58
 
59
				onComplete(data.data)
60
				dispatch(addNotification({
61
					style: 'success',
62
					msg: 'Registro actualizado'
63
				}))
64
				closeModal()
65
			})
66
			.catch((err) => console.log(err))
15225 stevensc 67
			.finally(() => setLoading(false))
14738 stevensc 68
	}
69
 
11152 stevensc 70
	return (
15261 stevensc 71
		<Modal size="md" show onHide={closeModal}>
14738 stevensc 72
			<Modal.Header closeButton>
73
				<Modal.Title>Ubicación</Modal.Title>
74
			</Modal.Header>
75
			<Modal.Body>
76
				<div className='form-group'>
77
					<SearchLocationInput
78
						value={value}
79
						setValue={setValue}
80
						googleApiKey={googleApiKey}
81
						updateData={setData}
82
					/>
83
					<div
84
						className={`toggle btn btn-primary ${!isActive && 'off'}`}
85
						data-toggle="toggle"
86
						role="button"
87
						style={{ width: '130px' }}
88
						onClick={() => setIsActive(!isActive)}
89
					>
90
						<input
91
							type="checkbox"
92
							checked={isActive}
93
						/>
94
						<div className="toggle-group">
95
							<label htmlFor="status" className="btn btn-primary toggle-on">Principal</label>
96
							<label htmlFor="status" className="btn btn-light toggle-off">Secundaria</label>
97
							<span className="toggle-handle btn btn-light"></span>
98
						</div>
99
					</div>
11152 stevensc 100
				</div>
14738 stevensc 101
			</Modal.Body>
102
			<Modal.Footer>
103
				<Button
15225 stevensc 104
					disabled={loading}
14738 stevensc 105
					variant="primary"
106
					onClick={onSubmit}
107
				>
108
					Enviar
109
				</Button>
110
				<Button variant="danger" onClick={closeModal}>
111
					Cancelar
112
				</Button>
113
			</Modal.Footer>
114
		</Modal >
11152 stevensc 115
	)
7774 stevensc 116
}
117
export default LocationModal