Proyectos de Subversion LeadersLinked - Backend

Rev

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

Rev Autor Línea Nro. Línea
11046 stevensc 1
import { Button, Modal } from '@material-ui/core'
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
13
	const { register, handleSubmit, watch, setError, errors, setValue } = useForm()
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({
43
						style: 'error',
44
						msg: 'Ha ocurrido un error'
45
					}))
46
				}
47
 
48
				setValue('user_id', data.data.user_id)
49
				dispatch(addNotification({
50
					style: 'success',
51
					msg: 'Usuario registrado'
52
				}))
53
			})
54
	}
55
 
56
	const showSecondPage = () => {
57
		if (watch('first_name') && watch('last_name') && watch('email')) {
58
			return setIsShowSecondPage(true)
59
		}
60
 
61
		setError('first_name', { message: 'Este campo es requerido' })
62
		setError('last_name', { message: 'Este campo es requerido' })
63
		setError('email', { message: 'Este campo es requerido' })
64
	}
65
 
66
	const checkEmail = () => {
67
		axios.get(email_link, { params: { email: watch('email') } })
68
			.then(({ data }) => {
69
				if (!data.success) {
70
					dispatch(addNotification({
71
						style: 'error',
72
						msg: 'Ha ocurrido un error'
73
					}))
74
				}
75
 
76
				setValue('user_id', data.data.user_id)
77
			})
78
	}
79
 
80
	return (
81
		<Modal size="sm" onHide={closeModal} show={isOpen}>
82
			<Modal.Header closeButton>
83
				<Modal.Title>Nuevo candidato</Modal.Title>
84
			</Modal.Header>
85
			<form onSubmit={handleSubmit(onSubmit)}>
86
				{
87
					isShowSecondPage
88
						?
89
						<>
90
							<Modal.Body>
91
								<div className='form-group'>
92
									<label className="form-label">Comentario</label>
93
									<DescriptionInput
94
										name='comment'
95
										setValue={setValue}
96
									/>
97
									{errors.comment && <p>{errors.comment.message}</p>}
98
								</div>
99
								<div className='form-group'>
100
									<label className="form-label">Evaluación</label>
101
									<select className='form-control' name='evaluation' ref={register}>
102
										{
103
											pointsOptions.map(({ label, value }) => {
104
												return <option key={value} value={value}>{label}</option>
105
											})
106
										}
107
									</select>
108
								</div>
109
								<div className='form-group'>
110
									<label className="form-label">Resumen Curricular</label>
111
									<input
112
										className="form-control"
113
										type="file"
114
										name="file"
115
										accept='pdf/docx'
116
										ref={register}
117
										style={{ padding: '1px' }}
118
									/>
119
								</div>
120
								<div className='form-group'>
121
									<label className="form-label">Estatus</label>
122
									<select className='form-control' name='evaluation' ref={register}>
123
										{
124
											statusOptions.map(({ label, value }) => {
125
												return <option key={value} value={value}>{label}</option>
126
											})
127
										}
128
									</select>
129
								</div>
130
							</Modal.Body>
131
							<Modal.Footer>
132
								<Button
133
									variant="primary"
134
									type='submit'
135
								>
136
									Enviar
137
								</Button>
11048 stevensc 138
								<Button variant="danger" onClick={() => setIsShowSecondPage(false)}>
11046 stevensc 139
									Cancelar
140
								</Button>
141
							</Modal.Footer>
142
						</>
143
						:
144
						<>
145
							<Modal.Body>
146
								<div className='form-group'>
147
									<label className="form-label">Correo electrónico</label>
148
									<input type="email" name='email' className='form-control' ref={register({ required: true })} />
149
									{errors.email && <p>{errors.email.message}</p>}
150
								</div>
151
								<button
152
									type="button"
153
									className="btn btn-primary"
154
									onClick={checkEmail}
155
								>
156
									Verificar Email
157
								</button>
158
								<div className='form-group'>
159
									<label className="form-label">Nombre</label>
160
									<input type="text" name='first_name' className='form-control' ref={register({ required: true })} />
161
									{errors.first_name && <p>{errors.first_name.message}</p>}
162
								</div>
163
								<div className='form-group'>
164
									<label className="form-label">Apellido</label>
165
									<input type="text" name='last_name' className='form-control' ref={register({ required: true })} />
166
									{errors.last_name && <p>{errors.last_name.message}</p>}
167
								</div>
168
 
169
							</Modal.Body>
170
							<Modal.Footer>
171
								<Button
172
									variant="primary"
173
									onClick={showSecondPage}
174
								>
175
									Enviar
176
								</Button>
177
								<Button variant="danger" onClick={closeModal}>
178
									Cancelar
179
								</Button>
180
							</Modal.Footer>
181
						</>
182
				}
183
			</form>
184
		</Modal >
185
	)
186
}
187
 
188
export default AddModal