Proyectos de Subversion LeadersLinked - Backend

Rev

Rev 11484 | Ir a la última revisión | | Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
11483 stevensc 1
import React, { useState, useEffect } 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 EditAndAddModal = ({ action_link, closeModal, type }) => {
11
 
12
	//Hooks
13
	const { register, handleSubmit, errors, setValue, clearErrors } = useForm()
14
	const [isActive, setIsActive] = useState(false)
15
	const [year, setYear] = useState(new Date())
16
	const dispatch = useDispatch()
17
 
18
	const onSubmit = (data) => {
19
		const submitData = new FormData()
20
 
21
		Object.entries({
22
			...data,
23
			status: isActive ? 'a' : 'i',
24
			date: year
25
		}).map(([key, value]) => {
26
			submitData.append(key, value)
27
		})
28
 
29
		axios.post(action_link, submitData)
30
			.then(({ data }) => {
31
				if (!data.success) {
32
					return dispatch(addNotification({
33
						style: 'danger',
34
						msg: 'Ha ocurrido un error'
35
					}))
36
				}
37
 
38
				clearErrors()
39
				closeModal()
40
				dispatch(addNotification({
41
					style: 'success',
42
					msg: 'Usuario registrado'
43
				}))
44
			})
45
	}
46
 
47
	useEffect(() => {
48
		if (type === 'edit') {
49
			axios.get(action_link)
50
				.then(({ data }) => {
51
					if (!data.success) {
52
						return dispatch(addNotification({
53
							style: 'danger',
54
							msg: 'Ha ocurrido un error'
55
						}))
56
					}
57
 
58
					Object.entries(data.data).map(([key, value]) => {
59
						if (key === 'status') {
60
							return setValue(key, value ? 'a' : 'i')
61
						}
62
						setValue(key, value)
63
					})
64
				})
65
		}
66
	}, [type])
67
 
68
	return (
69
		<Modal size="md" onHide={closeModal} show={type === 'add' || type === ('edit')}>
70
			<Modal.Header closeButton>
71
				<Modal.Title>
72
					{
73
						type === 'add'
74
							? 'Agregar Objetivo'
75
							: 'Editar Objetivo'
76
					}
77
				</Modal.Title>
78
			</Modal.Header>
79
			<form onSubmit={handleSubmit(onSubmit)}>
80
				<Modal.Body>
81
					<div className="d-flex" style={{ gap: '10px' }}>
82
						<div className='form-group'>
83
							<label className="form-label">Título</label>
84
							<input type="text" name='title' className='form-control' ref={register({ required: true })} />
85
							{errors.title && <p>{errors.title.message}</p>}
86
						</div>
87
						<div className='form-group'>
88
							<label className="form-label">Estatus</label>
89
							<div
90
								className={`toggle d-block btn btn-primary ${!isActive && 'off'}`}
91
								data-toggle="toggle"
92
								role="button"
93
								style={{ width: '130px' }}
94
								onClick={() => setIsActive(!isActive)}
95
							>
96
								<input
97
									type="checkbox"
98
									checked={isActive}
99
								/>
100
								<div className="toggle-group">
101
									<label htmlFor="status" className="btn btn-primary toggle-on">Activo</label>
102
									<label htmlFor="status" className="btn btn-light toggle-off">Inactivo</label>
103
									<span className="toggle-handle btn btn-light"></span>
104
								</div>
105
							</div>
106
						</div>
107
					</div>
108
					<div className='form-group'>
109
						<label className="form-label">Fecha</label>
110
						<Datetime
111
							dateFormat="DD-MM-YYYY"
112
							timeFormat={false}
113
							onChange={(e) =>
114
								setYear(new Intl.DateTimeFormat('en-CA', { year: 'numeric', month: 'numeric', day: 'numeric' }).format(e.toDate()))
115
							}
116
							inputProps={{ className: 'form-control' }}
117
							initialValue={Date.parse(year)}
118
							closeOnSelect
119
							value={year}
120
						/>
121
					</div>
122
					<div className='form-group'>
123
						<label className="form-label">Descripción</label>
124
						<textarea type="text" name='description' className='form-control' rows='3' ref={register({ required: true })} />
125
						{errors.description && <p>{errors.description.message}</p>}
126
					</div>
127
				</Modal.Body>
128
				<Modal.Footer>
129
					<Button variant="primary" type='submit'>
130
						Enviar
131
					</Button>
132
					<Button variant="danger" onClick={closeModal}>
133
						Cancelar
134
					</Button>
135
				</Modal.Footer>
136
			</form>
137
		</Modal >
138
	)
139
}
140
 
141
export default EditAndAddModal