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