Update table component

This commit is contained in:
2023-12-08 17:16:13 +07:00
parent d878d3b0f6
commit d543558a01
7 changed files with 103 additions and 20 deletions
@@ -7,6 +7,7 @@ import {
StyledTable, StyledTable,
} from './styled'; } from './styled';
import Pagination from '../Pagination'; import Pagination from '../Pagination';
import { TbArrowNarrowRight } from 'react-icons/tb';
const parseWidthString = (columnsWidth: number[]): string => { const parseWidthString = (columnsWidth: number[]): string => {
let result: string = ''; let result: string = '';
@@ -46,6 +47,18 @@ const Table = <T,>({ rows, columns, count }: Props<T>) => {
{columns.map((column, columnIndex) => { {columns.map((column, columnIndex) => {
const value = row[column.key as keyof typeof row] as string; const value = row[column.key as keyof typeof row] as string;
if (column.isDateValue) {
return (
<div
style={{ display: 'flex', alignItems: 'center' }}
key={`cell-${columnIndex}`}
>
{value[0]} &nbsp; <TbArrowNarrowRight />
&nbsp; {value[1]}
</div>
);
}
return <div key={`cell-${columnIndex}`}>{value}</div>; return <div key={`cell-${columnIndex}`}>{value}</div>;
})} })}
</StyledRow> </StyledRow>
@@ -26,6 +26,10 @@ const StyledBody = styled.div`
const StyledRow = styled(CommonRow)` const StyledRow = styled(CommonRow)`
padding: 20px; padding: 20px;
&.active {
background-color: red;
}
&:not(:last-child) { &:not(:last-child) {
border-bottom: 1px solid var(--border-color); border-bottom: 1px solid var(--border-color);
} }
@@ -0,0 +1,35 @@
import { Dispatch, createContext } from 'react';
export type TItemSelectAction = 'setData' | 'removeData';
interface IAction<T> {
type: TItemSelectAction;
payload: T;
}
interface IItemSelect {
data?: any;
dispatch?: Dispatch<IAction<unknown>>;
}
const ItemSelectContext = createContext<IItemSelect>({});
const itemSelectReducer =
<T>() =>
(state: T, action: IAction<T>) => {
switch (action.type) {
// Init data for the first load
case 'setData': {
const tempData = { ...state };
return {
data: tempData,
};
}
default:
throw new Error('Action unknown');
}
};
export { itemSelectReducer, ItemSelectContext };
@@ -0,0 +1,22 @@
import { useContext } from 'react';
// Contexts
import { ItemSelectContext } from '@src/contexts/ItemSelected';
/**
* The hooks check the place call is in UserRoomAvailableProvider or not
* @returns The context of UserRoomAvailableContext
*/
const useItemSelect = () => {
const context = useContext(ItemSelectContext);
if (context === undefined) {
throw new Error(
'ItemSelectContext was used outside of ItemSelectContextProvider'
);
}
return context;
};
export { useItemSelect };
@@ -14,10 +14,11 @@ import { useBookings } from '@src/hooks/bookings/useBookings';
// Types // Types
import { ColumnProps } from '@src/types/common'; import { ColumnProps } from '@src/types/common';
import { formatCurrency } from '@src/helpers/helper'; import { formatCurrency } from '@src/helpers/helper';
import { useItemSelect } from '@src/hooks/useItemSelected';
interface IBookingTable { interface IBookingTable {
user: string; user: string;
date: string; date: string[];
room: string; room: string;
amount: string; amount: string;
status: string; status: string;
@@ -25,16 +26,19 @@ interface IBookingTable {
} }
const BookingTable = () => { const BookingTable = () => {
const { dispatch } = useItemSelect();
const columns: ColumnProps[] = [ const columns: ColumnProps[] = [
{ {
key: 'user', key: 'user',
title: 'User', title: 'User',
width: 15, width: 10,
}, },
{ {
key: 'date', key: 'date',
title: 'Date', title: 'Date',
width: 25, width: 25,
isDateValue: true,
}, },
{ {
key: 'room', key: 'room',
@@ -49,19 +53,21 @@ const BookingTable = () => {
{ {
key: 'status', key: 'status',
title: 'Status', title: 'Status',
width: 15, width: 20,
}, },
]; ];
const { isLoading, bookings, count } = useBookings(); const { isLoading, bookings, count } = useBookings();
const tempBookings = bookings?.map((booking) => ({ const tempBookings = bookings?.map((booking) => ({
date: 'Error', date: [booking.startDate, booking.endDate],
amount: formatCurrency(booking.amount), amount: formatCurrency(booking.amount),
room: booking.rooms!.name, room: booking.rooms!.name,
user: booking.users!.name, user: booking.users!.name,
status: booking.status ? 'Check in' : 'Check out', status: booking.status ? 'Check in' : 'Check out',
onClick: () => console.log(booking), onClick: () => {
dispatch!({type: 'setData', payload: booking})
},
})); }));
return ( return (
@@ -17,6 +17,7 @@ const Booking = () => {
<Direction type="horizontal"> <Direction type="horizontal">
<Title>List Booking</Title> <Title>List Booking</Title>
<div style={{ display: 'flex', gap: '15px' }}>
<Modal> <Modal>
<Modal.Open <Modal.Open
modalName={FORM.BOOKING} modalName={FORM.BOOKING}
@@ -32,6 +33,7 @@ const Booking = () => {
)} )}
/> />
</Modal> </Modal>
</div>
</Direction> </Direction>
<BookingTable /> <BookingTable />
+1
View File
@@ -16,6 +16,7 @@ interface ColumnProps {
key: string; key: string;
title: string; title: string;
width: number; width: number;
isDateValue?: boolean;
} }
type TDirection = 'vertical' | 'horizontal'; type TDirection = 'vertical' | 'horizontal';