Proyectos de Subversion LeadersLinked - Backend

Rev

Rev 12329 | | Comparar con el anterior | Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
12329 stevensc 1
import React, { useState } from 'react'
2
import { Button, Modal } from 'react-bootstrap'
3
import axios from 'axios'
4
import { useForm } from 'react-hook-form'
5
import { useDispatch } from 'react-redux'
6
import Datetime from 'react-datetime'
7
import 'react-datetime/css/react-datetime.css'
8
import { addNotification } from '../../../redux/notification/notification.actions'
9
 
10
const AddModal = ({
11
	action_link,
12
	closeModal,
13
	isOpen,
14
	onComplete,
15
	email_link
16
}) => {
17
 
18
	//Hooks
19
	const { register, handleSubmit, errors, setValue, clearErrors, watch } = useForm()
20
	const [year, setYear] = useState(new Intl.DateTimeFormat('en-CA').format(new Date()))
21
	const dispatch = useDispatch()
22
 
23
	const onSubmit = (data) => {
24
 
25
		const submitData = new FormData()
26
		submitData.append('date', year)
27
 
28
		Object.entries(data).map(([key, value]) => {
29
			submitData.append(key, value)
30
		})
31
 
32
		axios.post(action_link, submitData)
33
			.then(({ data }) => {
34
				if (!data.success) {
14843 stevensc 35
					typeof data.data === 'string'
36
						?
37
						dispatch(addNotification({
38
							style: 'danger',
39
							msg: data.data
40
						}))
41
						: Object.entries(data.data).map(([key, value]) =>
42
							value.map(err =>
43
								dispatch(addNotification({
44
									style: 'danger',
45
									msg: `${key}: ${err}`
46
								}))
47
							)
48
						)
49
					return
12329 stevensc 50
				}
51
 
52
				clearErrors()
53
				closeModal()
54
				onComplete()
55
				dispatch(addNotification({
56
					style: 'success',
57
					msg: 'Resgistro guardadof'
58
				}))
59
			})
60
	}
61
 
62
	const checkEmail = () => {
63
		axios.get(email_link, { params: { email: watch('email') } })
64
			.then(({ data }) => {
65
				if (!data.success) {
66
					dispatch(addNotification({
67
						style: 'danger',
68
						msg: 'Ha ocurrido un error'
69
					}))
70
				}
71
 
72
				setValue('user_id', data.data.user_id)
73
				setValue('first_name', data.data.first_name)
74
				setValue('last_name', data.data.last_name)
75
				clearErrors()
76
			})
77
	}
78
 
79
	return (
80
		<Modal size="md" onHide={closeModal} show={isOpen}>
81
			<Modal.Header closeButton>
82
				<Modal.Title>
83
                    Evaluación
84
				</Modal.Title>
85
			</Modal.Header>
86
			<form onSubmit={handleSubmit(onSubmit)}>
87
				<Modal.Body>
88
					<div className='form-group'>
89
						<label className="form-label">Último día</label>
90
						<Datetime
91
							dateFormat="DD-MM-YYYY"
92
							timeFormat={false}
93
							onChange={(e) =>
94
								setYear(new Intl.DateTimeFormat('en-CA', { year: 'numeric', month: 'numeric', day: 'numeric' }).format(e.toDate()))
95
							}
96
							inputProps={{ className: 'form-control' }}
97
							initialValue={Date.parse(year)}
98
							closeOnSelect
99
							value={year}
100
						/>
101
					</div>
102
					<div className='form-group'>
103
						<label className="form-label">Correo electrónico</label>
104
						<input type="email" name='email' className='form-control' ref={register({ required: true })} />
105
						{errors.email && <p>{errors.email.message}</p>}
106
					</div>
107
					<button
108
						type="button"
109
						className="btn btn-primary mb-1"
110
						onClick={checkEmail}
111
					>
112
                        Verificar Email
113
					</button>
114
					<div className='form-group'>
115
						<label className="form-label">Nombre</label>
116
						<input type="text" name='first_name' className='form-control' ref={register({ required: true })} />
117
						{errors.first_name && <p>{errors.first_name.message}</p>}
118
					</div>
119
					<div className='form-group'>
120
						<label className="form-label">Apellido</label>
121
						<input type="text" name='last_name' className='form-control' ref={register({ required: true })} />
122
						{errors.last_name && <p>{errors.last_name.message}</p>}
123
					</div>
124
				</Modal.Body>
125
				<Modal.Footer>
126
					<Button variant="primary" type='submit'>
127
                        Enviar
128
					</Button>
129
					<Button variant="danger" onClick={closeModal}>
130
                        Cancelar
131
					</Button>
132
				</Modal.Footer>
133
			</form>
134
		</Modal >
135
	)
136
}
137
 
138
export default AddModal