@@ -4,12 +4,12 @@ import { useForm } from "react-hook-form";
4
4
5
5
// importando la funcion toast Librería nextjs-toast-notify para las alertas
6
6
import { toast } from "nextjs-toast-notify" ;
7
- import VariablesDeEstados from "./VariablesDeEstados" ;
8
- import "../styles/loading.css" ;
7
+
8
+ // importando la Librería loading-request para agregar un efecto Loading mientras se realiza una solicitud HTTP con Javascript
9
+ import { showLoading , hideLoading } from "loading-request" ;
9
10
10
11
import SelectEdad from "./SelectEdad" ;
11
12
import SelectCargoEmpleado from "./SelectCargoEmpleado" ;
12
- import Loading from "./Loading" ;
13
13
14
14
const FormlarioEmpleado = ( {
15
15
URL_API ,
@@ -19,8 +19,6 @@ const FormlarioEmpleado = ({
19
19
dataEditarEmpleado,
20
20
avatarUrl,
21
21
} ) => {
22
- const { loading, setLoading } = VariablesDeEstados ( ) ;
23
-
24
22
const {
25
23
register,
26
24
handleSubmit,
@@ -41,7 +39,13 @@ const FormlarioEmpleado = ({
41
39
: "" ;
42
40
43
41
const customHandleSubmit = async ( data ) => {
44
- setLoading ( true ) ;
42
+ // Agregando función showLoading para inabilitar la pantalla mientras se realiza la petición HTTP
43
+ showLoading ( {
44
+ message : "Enviando información..." ,
45
+ spinnerColor : "#8201ff" ,
46
+ textLoadingColor : "#8201ff" ,
47
+ textLoadingSize : "20px" ,
48
+ } ) ;
45
49
46
50
const formData = new FormData ( ) ;
47
51
formData . append ( "nombre" , data . nombre ) ;
@@ -59,22 +63,35 @@ const FormlarioEmpleado = ({
59
63
} ,
60
64
} ) ;
61
65
62
- // Simulamos un envío de formulario asíncrono
63
- setTimeout ( ( ) => {
64
- setLoading ( false ) ;
65
- } , 1000 ) ;
66
+ //setLoading(false);
67
+ toast . success ( "Empleado agregado correctamente" , {
68
+ duration : 4000 ,
69
+ progress : true ,
70
+ position : "top-right" ,
71
+ transition : "swingInverted" ,
72
+ icon : '<svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-check"><path d="M20 6 9 17l-5-5"/></svg>' ,
73
+ sonido : true ,
74
+ } ) ;
66
75
67
- toast . success ( "Empleado agregado correctamente" ) ;
68
76
// Consulto la API para obtener la lista de empleados actualizada y actualizo la lista de empleados
69
77
const empleadosData = await obtenerEmpleados ( ) ;
70
78
setEmpleados ( empleadosData ) ;
71
79
} catch ( error ) {
72
80
console . error ( "Error al agregar empleado:" , error ) ;
81
+ } finally {
82
+ // Agregando función hideLoading para habilitar la pantalla
83
+ hideLoading ( { timeLoading : 1500 } ) ;
73
84
}
74
85
} ;
75
86
76
87
const handleSubmitUpdateForm = async ( data ) => {
77
- setLoading ( true ) ;
88
+ showLoading ( {
89
+ message : "eliminando el empleado..." ,
90
+ spinnerColor : "#8201ff" ,
91
+ textLoadingColor : "#8201ff" ,
92
+ textLoadingSize : "20px" ,
93
+ } ) ;
94
+
78
95
/**
79
96
* IMPORTANTE: para enviar el formulario por el metodo put, se debe enmascarar(engañar) el tipo de solicitud, diciendo con axios que la solicitud
80
97
* es de tipo POST, pero en realidad es de tipo PUT ya que esta agregando formData.append("_method", "PUT"); y esto define el metodo de envio.
@@ -105,18 +122,19 @@ const FormlarioEmpleado = ({
105
122
"Content-Type" : "multipart/form-data" ,
106
123
} ,
107
124
} ) ;
108
- // Simulamos un envío de formulario asíncrono
109
- setTimeout ( ( ) => {
110
- setLoading ( false ) ;
111
- } , 1000 ) ;
112
125
113
- toast . success ( "Empleado actualizado correctamente" ) ;
126
+ toast . success ( "Empleado actualizado correctamente" , {
127
+ transition : "bounceIn" ,
128
+ sonido : true ,
129
+ } ) ;
114
130
// Consulto la API para obtener la lista de empleados actualizada y actualizo la lista de empleados
115
131
const empleadosData = await obtenerEmpleados ( ) ;
116
132
setEmpleados ( empleadosData ) ;
117
133
volverAlHomeDesdeEditar ( ) ;
118
134
} catch ( error ) {
119
135
console . error ( "Error al actualizar el empleado:" , error ) ;
136
+ } finally {
137
+ hideLoading ( { timeLoading : 1000 } ) ;
120
138
}
121
139
} ;
122
140
@@ -265,8 +283,6 @@ const FormlarioEmpleado = ({
265
283
</ button >
266
284
</ div >
267
285
</ form >
268
-
269
- { loading && < Loading /> }
270
286
</ >
271
287
) ;
272
288
} ;
0 commit comments