Proyectos de Subversion LeadersLinked - Backend

Rev

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

Rev Autor Línea Nro. Línea
11049 stevensc 1
import { Button, Modal } from 'react-bootstrap'
11046 stevensc 2
import axios from 'axios'
3
import React from 'react'
4
import { useState } from 'react'
5
import { useForm } from 'react-hook-form'
6
import { useDispatch } from 'react-redux'
7
import { addNotification } from '../../../redux/notification/notification.actions'
8
import DescriptionInput from '../../../shared/DescriptionInput'
9
 
10
const AddModal = ({ closeModal, email_link, isOpen, add_link }) => {
11
 
12
	//Hooks
11051 stevensc 13
	const { register, handleSubmit, watch, setError, errors, setValue, clearErrors } = useForm()
11046 stevensc 14
	const dispatch = useDispatch()
15
 
16
	//States
17
	const [isShowSecondPage, setIsShowSecondPage] = useState(false)
18
	const [pointsOptions] = useState([
19
		{ label: 'Evaluación', value: null },
20
		{ label: 'Sugerir otro cargo', value: 0 },
21
		{ label: '25%', value: 1 },
22
		{ label: '50%', value: 2 },
23
		{ label: '75%', value: 3 },
24
		{ label: '100%', value: 4 }
25
	])
26
	const [statusOptions] = useState([
27
		{ label: 'Estatus', value: '' },
28
		{ label: 'Aceptado', value: 'a' },
29
		{ label: 'Rechazado', value: 'r' }
30
	])
31
 
32
	const onSubmit = (data) => {
33
 
34
		const submitData = new FormData()
35
		Object.entries(data).map(([key, value]) => {
36
			submitData.append(key, value)
37
		})
38
 
39
		axios.post(add_link, submitData)
40
			.then(({ data }) => {
41
				if (!data.success) {
42
					dispatch(addNotification({
11051 stevensc 43
						style: 'danger',
11046 stevensc 44
						msg: 'Ha ocurrido un error'
45
					}))
46
				}
47
 
11051 stevensc 48
				clearErrors()
49
				closeModal()
11046 stevensc 50
				dispatch(addNotification({
51
					style: 'success',
52
					msg: 'Usuario registrado'
53
				}))
54
			})
55
	}
56
 
57
	const showSecondPage = () => {
58
		if (watch('first_name') && watch('last_name') && watch('email')) {
59
			return setIsShowSecondPage(true)
60
		}
61
 
62
		setError('first_name', { message: 'Este campo es requerido' })
63
		setError('last_name', { message: 'Este campo es requerido' })
64
		setError('email', { message: 'Este campo es requerido' })
65
	}
66
 
67
	const checkEmail = () => {
68
		axios.get(email_link, { params: { email: watch('email') } })
69
			.then(({ data }) => {
70
				if (!data.success) {
71
					dispatch(addNotification({
11051 stevensc 72
						style: 'danger',
11046 stevensc 73
						msg: 'Ha ocurrido un error'
74
					}))
75
				}
76
 
77
				setValue('user_id', data.data.user_id)
11051 stevensc 78
				setValue('first_name', data.data.first_name)
79
				setValue('last_name', data.data.last_name)
80
				clearErrors()
11046 stevensc 81
			})
82
	}
83
 
84
	return (
11053 stevensc 85
		<Modal size="sm" onHide={closeModal} show={isOpen}>
11046 stevensc 86
			<Modal.Header closeButton>
87
				<Modal.Title>Nuevo candidato</Modal.Title>
88
			</Modal.Header>
89
			<form onSubmit={handleSubmit(onSubmit)}>
11053 stevensc 90
				<Modal.Body>
11055 stevensc 91
					<div style={isShowSecondPage ? { display: 'none' } : { display: 'inline-flex' }}>
92
						<div className='form-group'>
93
							<label className="form-label">Comentario</label>
94
							<DescriptionInput
95
								name='comment'
96
								setValue={setValue}
97
							/>
98
							{errors.comment && <p>{errors.comment.message}</p>}
99
						</div>
100
						<div className='form-group'>
101
							<label className="form-label">Evaluación</label>
102
							<select className='form-control' name='evaluation' ref={register}>
103
								{
104
									pointsOptions.map(({ label, value }) => {
105
										return <option key={value} value={value}>{label}</option>
106
									})
107
								}
108
							</select>
109
						</div>
110
						<div className='form-group'>
111
							<label className="form-label">Resumen Curricular</label>
112
							<input
113
								className="form-control p-0"
114
								type="file"
115
								name="file"
116
								accept='pdf/docx'
117
								ref={register}
118
								style={{ height: '2rem' }}
119
							/>
120
						</div>
121
						<div className='form-group'>
122
							<label className="form-label">Estatus</label>
123
							<select className='form-control' name='evaluation' ref={register}>
124
								{
125
									statusOptions.map(({ label, value }) => {
126
										return <option key={value} value={value}>{label}</option>
127
									})
128
								}
129
							</select>
130
						</div>
131
					</div>
132
					<div style={!isShowSecondPage ? { display: 'none' } : { display: 'inline-flex' }}>
133
						<div className='form-group'>
134
							<label className="form-label">Correo electrónico</label>
135
							<input type="email" name='email' className='form-control' ref={register({ required: true })} />
136
							{errors.email && <p>{errors.email.message}</p>}
137
						</div>
138
						<button
139
							type="button"
140
							className="btn btn-primary"
141
							onClick={checkEmail}
142
						>
143
							Verificar Email
144
						</button>
145
						<div className='form-group'>
146
							<label className="form-label">Nombre</label>
147
							<input type="text" name='first_name' className='form-control' ref={register({ required: true })} />
148
							{errors.first_name && <p>{errors.first_name.message}</p>}
149
						</div>
150
						<div className='form-group'>
151
							<label className="form-label">Apellido</label>
152
							<input type="text" name='last_name' className='form-control' ref={register({ required: true })} />
153
							{errors.last_name && <p>{errors.last_name.message}</p>}
154
						</div>
155
					</div>
11053 stevensc 156
				</Modal.Body>
157
				<Modal.Footer>
158
					{
159
						isShowSecondPage
160
							?
161
							<>
162
								<Button variant="danger" onClick={() => setIsShowSecondPage(false)}>
163
									Cancelar
164
								</Button>
165
								<Button
166
									variant="primary"
167
									type='submit'
168
								>
169
									Enviar
170
								</Button>
171
							</>
172
							:
173
							<Button
174
								variant="primary"
175
								onClick={showSecondPage}
176
							>
177
								Siguiente
178
							</Button>
179
					}
180
				</Modal.Footer>
11046 stevensc 181
			</form>
182
		</Modal >
183
	)
184
}
185
 
186
export default AddModal