You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

313 lines
12 KiB
React

2 years ago
import React, { useState, useEffect } from 'react'
import { usePrevious } from "react-use";
import { router } from '@inertiajs/react'
import qs from 'qs'
2 years ago
import AuthenticatedLayout from '@/Layouts/AuthenticatedLayout';
import Pagination from "@/Components/Pagination";
import ModalConfirm from "@/Components/ModalConfirm";
import FormModal from './FormModal';
import DetailModal from './DetailModal';
import ImportModal from './ImportModal';
2 years ago
import { DatePickerRangeInput } from "@/Components/DatePickerInput";
import { useModalState } from "@/Hook";
import { Head } from '@inertiajs/react';
2 years ago
import { formatDate } from "@/Utils";
import { toast } from 'react-toastify';
export default function Dashboard(props) {
const { _startDate, _endDate, _limit } = props
const [startDate] = useState(_startDate)
const [endDate] = useState(_endDate)
const [filterDate, setFilterDate] = useState([_startDate, _endDate])
2 years ago
const { data: bookings, links } = props.booking;
const [bookingsChecked, setBookingsChecked] = useState(
bookings.map((booking) => {
return {
...booking,
isChecked: false,
};
})
);
const [search, setSearch] = useState("");
const [limit, setLimit] = useState(_limit)
const preValue = usePrevious(`${search}-${filterDate[0]}-${filterDate[1]}-${limit}`);
2 years ago
const [booking, setBooking] = useState(null);
2 years ago
const formModal = useModalState(false);
const handleToggleForm = (booking = null) => {
2 years ago
setBooking(booking);
formModal.toggle();
};
const confirmModal = useModalState(false);
const handleDelete = (booking) => {
confirmModal.setData(booking);
confirmModal.toggle();
};
const detailModal = useModalState(false);
const handleDetail = (booking = null) => {
setBooking(booking);
detailModal.toggle();
};
const bookingModal = useModalState(false);
const handleImport = () => {
2 years ago
bookingModal.toggle();
};
const onDelete = () => {
const booking = confirmModal.data;
if (booking != null) {
router.delete(
2 years ago
route("monitoring-booking.destroy", booking), {
onSuccess: () => toast.success("item deleted"),
2 years ago
}
);
}
};
const handleCheckedCheckbox = (e) => {
setBookingsChecked(
bookingsChecked.map((booking) => {
if (booking.id === e.target.defaultValue * 1) {
return {
...booking,
isChecked: !booking.isChecked,
};
} else {
return booking;
}
})
);
};
const handleExport = () => {
const params = bookingsChecked
2 years ago
.map((booking) => {
if (booking.isChecked) {
return booking.id;
}
})
.filter((isChecked) => {
return isChecked !== undefined;
});
fetch(route('monitoring-booking.export') +'?'+ qs.stringify({ids: params}, { encodeValuesOnly:true }))
.then( res => res.blob() )
.then( blob => {
var file = window.URL.createObjectURL(blob);
var a = document.createElement('a');
a.href = file;
a.download = "bookings.xlsx";
document.body.appendChild(a);
a.click();
a.remove();
});
2 years ago
};
const handleCheckAll = (e) => {
setBookingsChecked((prevBookingsChecked) => {
return prevBookingsChecked.map((booking) => {
return {
...booking,
isChecked: e.target.checked,
};
});
});
};
const params = {
q: search,
startDate: filterDate[0],
endDate: filterDate[1],
limit,
};
2 years ago
useEffect(() => {
setBookingsChecked(
bookings.map((booking) => {
return {
...booking,
isChecked: false,
};
})
);
}, [bookings]);
useEffect(() => {
if (preValue) {
router.get(
2 years ago
route(route().current()),
{ q: search, startDate: filterDate[0], endDate: filterDate[1], limit },
2 years ago
{
replace: true,
preserveState: true,
}
)
}
}, [search, filterDate, limit])
2 years ago
return (
<AuthenticatedLayout
auth={props.auth}
errors={props.errors}
>
<Head title="Booking" />
<div className="p-4">
2 years ago
<div className="mx-auto max-w-7xl p-4 bg-white overflow-hidden shadow-sm sm:rounded-lg" style={{minHeight: '500px'}}>
<div className='flex justify-between space-x-1 flex-row mb-2'>
2 years ago
<div className='flex space-x-1'>
<div className='btn' onClick={() => handleToggleForm()}>Tambah</div>
<div className='btn' onClick={handleImport}>Import</div>
2 years ago
</div>
<div className='btn' onClick={() => handleExport()}>Export</div>
2 years ago
</div>
<div className='flex justify-between space-y-1 lg:space-y-0 space-x-0 lg:space-x-1 flex-col lg:flex-row'>
<div>
Terakhir diperbarui: {props.last_updated
? formatDate(props.last_updated).format('DD/MM/Y hh:mm')
: 'Belum ada pembaruan'}
</div>
<div className='flex lg:space-x-2 space-x-0 lg:space-y-0 space-y-1 flex-col lg:flex-row'>
<div className='flex space-x-1'>
<DatePickerRangeInput
startDate={new Date(startDate)}
endDate={endDate}
setFilterDate={setFilterDate}
2 years ago
/>
</div>
<div>
<input
className='input input-bordered w-full'
placeholder='search'
value={search}
onChange={e => setSearch(e.target.value)}
/>
2 years ago
</div>
</div>
</div>
<div className="overflow-x-auto mt-2">
<table className="table w-full table-zebra">
<thead>
<tr>
<th>
<input
type="checkbox"
className="checkbox checkbox-xs"
onChange={(e) =>handleCheckAll(e)}
/>
</th>
<th>Master AWB</th>
<th>Flight Number</th>
<th>Departure</th>
<th>Destination</th>
<th>Booked</th>
<th>Packaging</th>
<th>Used</th>
<th>Status</th>
<th>Opsi</th>
</tr>
</thead>
<tbody>
{bookingsChecked.map((booking) => (
<tr key={booking.id}>
<td>
<input
type="checkbox"
className="checkbox checkbox-xs"
onChange={(e) => handleCheckedCheckbox(e)}
checked={booking.isChecked}
value={booking.id}
/>
</td>
<td>{booking.master_awb}</td>
<td>{booking.flight_number}</td>
<td>
{formatDate(booking.departure).format('DD/MM/yyyy')}
</td>
<td>{booking.destination}</td>
<td>{booking.booked}</td>
<td>{booking.kemasan}</td>
<td>{booking.used}</td>
<td>
{+booking.is_available === 0
? 'Available'
: 'Closed'}
</td>
<td className="flex gap-1">
<button
className="btn btn-neutral btn-xs"
onClick={() => handleDetail(booking)}
>
Detail
</button>
<button
className="btn btn-neutral btn-xs"
onClick={() => handleToggleForm(booking)}
2 years ago
>
Edit
</button>
<button
className="btn btn-neutral btn-xs"
onClick={() => handleDelete(booking)}
>
Hapus
</button>
</td>
</tr>
))}
</tbody>
</table>
</div>
<div className='flex w-full'>
<div className="flex mx-auto items-end mt-4">
<Pagination
links={links}
params={params}
/>
<div>
<select
className="select select-bordered w-full max-w-xs"
onChange={(e) => setLimit(e.target.value)}
value={limit}
>
<option value="10">10</option>
<option value="50">50</option>
<option value="100">100</option>
</select>
</div>
2 years ago
</div>
</div>
</div>
</div>
<ModalConfirm
isOpen={confirmModal.isOpen}
toggle={confirmModal.toggle}
onConfirm={onDelete}
/>
<FormModal
isOpen={formModal.isOpen}
toggle={formModal.toggle}
booking={booking}
/>
<DetailModal
isOpen={detailModal.isOpen}
toggle={detailModal.toggle}
booking={booking}
title="Detail Booking"
/>
<ImportModal
isOpen={bookingModal.isOpen}
toggle={bookingModal.toggle}
/>
2 years ago
</AuthenticatedLayout>
);
}