dialogos y funciones eliminar reserva

This commit is contained in:
Mariela 2022-08-28 01:58:57 -06:00
parent dc3977ef44
commit 2ff3db6615
1 changed files with 165 additions and 15 deletions

View File

@ -94,7 +94,7 @@ const Reservations = () => {
) )
data.map((item) => { data.map((item) => {
item.date = formatDateString(item.date) item.date = formatDateString(item.date)
if (item.status == '1') { if (item.status == '1') {
item.status_text = 'Activo'; item.status_text = 'Activo';
@ -222,7 +222,83 @@ const Reservations = () => {
setReservation(emptyReservation); setReservation(emptyReservation);
setReservationDialog(true); setReservationDialog(true);
setSubmitted(false); setSubmitted(false);
};
const hideDeleteReservationDialog = () => {
setDeleteReservationDialog(false);
};
const hideDeleteReservationsDialog = () => {
setDeleteReservationsDialog(false);
};
const deleteReservation = () => {
fetch('http://localhost:4000/reservation/deleteReservation' + reservation._id, {
cache: 'no-cache',
method: 'DELETE',
headers: {
'Content-Type': 'application/json',
},
})
.then(function (response) {
if (response.status != 201)
console.log('Ocurrió un error con el servicio: ' + response.status);
else return response.json();
})
.then(function (response) {
let _reservation = reservations.filter(
(val) => (val._id !== reservation._id || val.status != -1),
);
setReservations(_reservation);
setDeleteReservationDialog(false);
setReservation(emptyReservation);
toast.current.show({
severity: 'success',
summary: 'Éxito',
detail: 'Reservación Eliminada',
life: 3000,
});
})
.catch((err) => {
console.log('Ocurrió un error con el fetch', err);
toast.current.show({
severity: 'danger',
summary: 'Error',
detail: 'Reservación no se pudo Eliminar',
life: 3000,
});
});
};
const deleteSelectedReservations = () => {
let _reservations = reservations.filter(
(val) => (!selectedReservations.includes(val)),
);
selectedReservations.map((item) => {
fetch('http://localhost:4000/reservation/deleteReservation/' + item._id, {
cache: 'no-cache',
method: 'DELETE',
headers: {
'Content-Type': 'application/json',
},
});
});
_reservations = _reservations.filter(
(val) => val.status != -1,
)
setReservations(_reservations);
setDeleteReservationsDialog(false);
setSelectedReservations(null);
toast.current.show({
severity: 'success',
summary: 'Éxito',
detail: 'Reservaciones Eliminadas',
life: 3000,
});
}; };
const leftToolbarTemplate = () => { const leftToolbarTemplate = () => {
@ -272,6 +348,40 @@ const Reservations = () => {
</> </>
); );
const deleteReservationDialogFooter = (
<>
<Button
label="No"
icon="pi pi-times"
className="p-button-text"
onClick={hideDeleteReservationDialog}
/>
<Button
label="Yes"
icon="pi pi-check"
className="p-button-text"
onClick={deleteReservation}
/>
</>
);
const deleteReservationsDialogFooter = (
<>
<Button
label="No"
icon="pi pi-times"
className="p-button-text"
onClick={hideDeleteReservationsDialog}
/>
<Button
label="Yes"
icon="pi pi-check"
className="p-button-text"
onClick={deleteSelectedReservations}
/>
</>
);
const header = ( const header = (
<div className="flex flex-column md:flex-row md:justify-content-between md:align-items-center"> <div className="flex flex-column md:flex-row md:justify-content-between md:align-items-center">
<h5 className="m-0"> <h5 className="m-0">
@ -411,12 +521,12 @@ const Reservations = () => {
} }
function validationIsReservation() { function validationIsReservation() {
if(reservation.date && reservation.time && areaId){ if (reservation.date && reservation.time && areaId) {
let date1 = new Date(reservation.date).toJSON().split('T')[0]; let date1 = new Date(reservation.date).toJSON().split('T')[0];
let date2 = date1.split('-')[2] + '-' + date1.split('-')[1] + '-' +date1.split('-')[0]; let date2 = date1.split('-')[2] + '-' + date1.split('-')[1] + '-' + date1.split('-')[0];
let booked = reservations.filter(item => item.common_area_id == areaId let booked = reservations.filter(item => item.common_area_id == areaId
&& item.date == date2 && item.date == date2
&& item.time == reservation.time); && item.time == reservation.time);
if (booked.length > 0) { if (booked.length > 0) {
@ -429,12 +539,12 @@ const Reservations = () => {
} }
function validationIsSameUser() { function validationIsSameUser() {
if(reservation.date && tenantId && areaId){ if (reservation.date && tenantId && areaId) {
let date1 = new Date(reservation.date).toJSON().split('T')[0]; let date1 = new Date(reservation.date).toJSON().split('T')[0];
let date2 = date1.split('-')[2] + '-' + date1.split('-')[1] + '-' +date1.split('-')[0]; let date2 = date1.split('-')[2] + '-' + date1.split('-')[1] + '-' + date1.split('-')[0];
let booked = reservations.filter(item => item.common_area_id == areaId let booked = reservations.filter(item => item.common_area_id == areaId
&& item.date == date2 && item.date == date2
&& item.user_id == tenantId); && item.user_id == tenantId);
if (booked.length >= 2) { if (booked.length >= 2) {
return true; return true;
@ -698,7 +808,47 @@ const Reservations = () => {
</div> </div>
)} )}
</Dialog> </Dialog>
<Dialog
visible={deleteReservationsDialog}
style={{ width: '450px' }}
header="Confirmar"
modal
footer={deleteReservationDialogFooter}
onHide={hideDeleteReservationDialog}
>
<div className="flex align-items-center justify-content-center">
<i
className="pi pi-exclamation-triangle mr-3"
style={{ fontSize: '2rem' }}
/>
{reservation && (
<span>
¿Estás seguro que desea eliminar la reservación de <b>{reservation.user_name}</b>?
</span>
)}
</div>
</Dialog>
<Dialog
visible={deleteReservationsDialog}
style={{ width: '450px' }}
header="Confirmar"
modal
footer={deleteReservationsDialogFooter}
onHide={hideDeleteReservationsDialog}
>
<div className="flex align-items-center justify-content-center">
<i
className="pi pi-exclamation-triangle mr-3"
style={{ fontSize: '2rem' }}
/>
{selectedReservations && (
<span>
¿Está seguro eliminar las reservaciones
seleccionadas?
</span>
)}
</div>
</Dialog>
</div> </div>
</div> </div>