Proyectos de Subversion LeadersLinked - Backend

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
12135 stevensc 1
import axios from 'axios'
2
import React, { useEffect, useState } from 'react'
3
import parse from 'html-react-parser'
4
import { useForm } from 'react-hook-form'
5
import { useDispatch } from 'react-redux'
6
import { useHistory } from 'react-router-dom'
7
import { addNotification } from '../../../redux/notification/notification.actions'
8
import DescriptionInput from '../../../shared/DescriptionInput'
12012 stevensc 9
 
12135 stevensc 10
const AddView = ({ actionLink, jobsDescription: jobDescriptionOptions }) => {
11
 
12
	// Hooks
13
	const history = useHistory()
14
	const { register, setValue, watch, reset } = useForm()
15
	const dispatch = useDispatch()
16
 
17
	const [status, setStatus] = useState('a')
18
 
19
	const onSubmit = () => {
20
		const submitData = new FormData()
21
		submitData.append('name', watch('name'))
22
		submitData.append('description', watch('description'))
23
		submitData.append('status', status)
14625 stevensc 24
		submitData.append('job_description_id', watch('job_description_id'))
12135 stevensc 25
		submitData.append('content', '[]')
26
 
27
		axios.post(actionLink, submitData)
28
			.then(({ data }) => {
29
				if (!data.success) {
14625 stevensc 30
					typeof data.data === 'string'
31
						?
32
						dispatch(addNotification({
33
							style: 'danger',
34
							msg: data.data
35
						}))
36
						: Object.entries(data.data).map(([key, value]) =>
37
							value.map(err =>
38
								dispatch(addNotification({
39
									style: 'danger',
40
									msg: `${key}: ${err}`
41
								}))
42
							)
43
						)
44
					return
12135 stevensc 45
				}
46
 
47
				dispatch(addNotification({
48
					style: 'success',
49
					msg: data.data
50
				}))
51
			})
52
	}
53
 
15066 stevensc 54
	useEffect(() => {
55
		axios.get(actionLink)
56
			.then(({ data }) => {
57
				if (!data.success) {
58
					return dispatch(addNotification({
59
						style: 'danger',
60
						msg: 'Ha ocurrido un error'
61
					}))
62
				}
63
 
64
				setValue('name', data.data.name)
65
				setValue('description', data.data.description)
66
				setValue('job_description_id', data.data.job_description_id)
67
				setStatus(data.data.status)
68
			})
69
	}, [actionLink])
70
 
12135 stevensc 71
	const submitAndClose = () => {
72
		onSubmit()
73
		reset()
74
		history.goBack()
75
	}
76
 
77
	useEffect(() => {
78
		register('description')
79
	}, [])
80
 
12012 stevensc 81
	return (
12135 stevensc 82
		<section className="content">
83
			<div className="row" style={{ padding: 16 }}>
84
				<div className="col-xs-12 col-md-12">
85
					<div className="form-group">
86
						<label>Nombre</label>
87
						<input type="text" name="name" className='form-control' ref={register({ required: true, maxLength: 50 })} />
88
					</div>
89
					<div className="form-group">
90
						<label>Cargo a evaluar</label>
14625 stevensc 91
						<select name="job_description_id" className="form-control" ref={register}>
12135 stevensc 92
							{
12138 stevensc 93
								jobDescriptionOptions.map(({ name, uuid }) => (
94
									<option key={uuid} value={uuid}>{name}</option>
12135 stevensc 95
								))
96
							}
97
						</select>
98
					</div>
99
					<div className="form-group">
100
						<label htmlFor="form-description">Descripción</label>
101
						<DescriptionInput
102
							defaultValue={typeof watch('description') === 'string' ? parse(watch('description')) : ''}
103
							name='description'
104
							onChange={setValue}
105
						/>
106
					</div>
107
					<div className="form-group">
108
						<label htmlFor="form-status">Estatus</label>
109
						<select name="form-status" className="form-control" onChange={(e) => setStatus(e.target.value)} value={status}>
110
							<option selected={status === 'i'} value="i">Inactivo</option>
111
							<option selected={status === 'a'} value="a">Activo</option>
112
						</select>
113
					</div>
114
					<br />
115
					<div className="d-flex" style={{ gap: '5px' }}>
116
						<button type="button" className="btn btn-info" onClick={onSubmit}>Guardar & Continuar</button>
117
						<button type="button" className="btn btn-primary" onClick={submitAndClose}>Guardar & Cerrar</button>
118
						<button type="button" className="btn btn-secondary" onClick={() => history.goBack()}>Cancelar</button>
119
					</div>
120
				</div>
121
			</div >
122
		</section >
123
 
12012 stevensc 124
	)
125
}
126
 
127
export default AddView