Commit 9d2d20ed authored by Merekeyev Dias's avatar Merekeyev Dias

added filter in RequestPage

parent ed69d9bb
...@@ -42,7 +42,7 @@ export const columns: TableProps<DataType>['columns'] = [ ...@@ -42,7 +42,7 @@ export const columns: TableProps<DataType>['columns'] = [
key: "sendDate", key: "sendDate",
width: '11%', width: '11%',
render: (_, record) => ( render: (_, record) => (
<span>{dayjs(record.sendDate).format('YYYY-MM-DD HH:mm:ss')}</span> <span>{dayjs(record.sendDate).format('DD-MM-YYYY HH:mm:ss')}</span>
) )
}, },
{ {
......
import React, { useEffect, useState, createRef } from 'react'; import React, { useEffect, useState, createRef } from 'react';
import dayjs from 'dayjs'; import dayjs, { Dayjs } from 'dayjs';
// import isBetween from 'dayjs/plugin/isBetween'; import isBetween from 'dayjs/plugin/isBetween';
// import customParseFormat from "dayjs/plugin/customParseFormat";
import { DatePicker, Select, Button, Pagination, Spin, Table } from 'antd'; import { DatePicker, Select, Button, Pagination, Spin, Table } from 'antd';
import { useDispatch, useSelector } from 'react-redux'; import { useDispatch, useSelector } from 'react-redux';
import { columns, DataType } from './Columns'; import { columns, DataType } from './Columns';
...@@ -10,7 +9,8 @@ import { setCurrentPage, setTotalReqNumber } from '../../features/pagination/req ...@@ -10,7 +9,8 @@ import { setCurrentPage, setTotalReqNumber } from '../../features/pagination/req
import { TableRef } from 'antd/es/table'; import { TableRef } from 'antd/es/table';
import './RequestPage.css'; import './RequestPage.css';
const dateFormat = 'YYYY/MM/DD'; dayjs.extend(isBetween);
const dateFormat = 'DD/MM/YYYY';
const RequestPage: React.FC = () => { const RequestPage: React.FC = () => {
const dispatch = useDispatch(); const dispatch = useDispatch();
...@@ -19,53 +19,54 @@ const RequestPage: React.FC = () => { ...@@ -19,53 +19,54 @@ const RequestPage: React.FC = () => {
const currentPage = useSelector((state: any) => state.reqPagination.currentPage); const currentPage = useSelector((state: any) => state.reqPagination.currentPage);
const REQ_NUMBER_PER_PAGE = useSelector((state: any) => state.reqPagination.REQ_NUMBER_PER_PAGE); const REQ_NUMBER_PER_PAGE = useSelector((state: any) => state.reqPagination.REQ_NUMBER_PER_PAGE);
const [startDate, setStartDate] = useState<Date>(); const [startDate, setStartDate] = useState<Dayjs | null>(null);
const [endDate, setEndDate] = useState(); const [endDate, setEndDate] = useState<Dayjs | null>(null);
const [data, setData] = useState<DataType[]>([]);
const [method, setMethod] = useState(''); const [method, setMethod] = useState('');
const [filteredData, setFilteredData] = useState<Request[]>([]);
const handlePaginationChange = (page: number) => { const handlePaginationChange = (page: number) => {
// tableRef.current?.nativeElement.scrollIntoView({ behavior: 'smooth' }); /*
scrollIntoView resets to the top of the table after changing the page with Pagination. But the bug was found - the add button scrolls to top of the page too, under the Header component. It happens because of overflow
*/
// tableRef.current?.nativeElement.scrollIntoView({ behavior: 'smooth' });
dispatch(setCurrentPage(page)); dispatch(setCurrentPage(page));
}; };
const paginatedData = (dataSource: DataType[]) => { const paginateData = (dataSource: DataType[]) => {
const startIndex = (currentPage - 1) * REQ_NUMBER_PER_PAGE; const startIndex = (currentPage - 1) * REQ_NUMBER_PER_PAGE;
const endIndex = Math.min(startIndex + REQ_NUMBER_PER_PAGE, totalReqNumber); const endIndex = Math.min(startIndex + REQ_NUMBER_PER_PAGE, totalReqNumber);
return dataSource.slice(startIndex, endIndex); return dataSource.slice(startIndex, endIndex);
}; };
const filterByDate = () => { const applyFilters = () => {
if (!startDate || !endDate) return; let filtered = reqs.list;
const filtered = reqs.list.filter((req) => { if (startDate && endDate) {
filtered = filtered.filter((req) => {
const sendDate = dayjs(req.sendDate, dateFormat); const sendDate = dayjs(req.sendDate, dateFormat);
}); return sendDate.isBetween(startDate, endDate, 'day', '[]');
});
console.log(filtered); }
// setFilteredData(filtered);
if (method && method !== 'all') {
filtered = filtered.filter(req => req.requestType === method);
}
setData(filtered);
dispatch(setCurrentPage(1));
dispatch(setTotalReqNumber(filtered.length)); dispatch(setTotalReqNumber(filtered.length));
}; }
const setDefaultDate = () => { const onSelectStartDate = (date: Dayjs | null) => {
let min = Infinity; setStartDate(date);
reqs.list.map((req) => {
const dateValue = new Date(req.sendDate).valueOf();
if (min >= dateValue) {
min = dateValue;
}
})
let minDateType = new Date(min);
setStartDate(minDateType);
} }
const onSelectStartDate = (dateString: any) => { const onSelectEndDate = (date: Dayjs | null) => {
setStartDate(dateString); setEndDate(date);
} }
const onSelectEndDate = (dateString: any) => { const onSelectMethod = (method: string) => {
setEndDate(dateString); setMethod(method);
} }
const { const {
...@@ -77,55 +78,91 @@ const RequestPage: React.FC = () => { ...@@ -77,55 +78,91 @@ const RequestPage: React.FC = () => {
} = useGetRequestsQuery(); } = useGetRequestsQuery();
let table; let table;
let min: number = Infinity;
let max: number = -Infinity;
if (isLoading) { if (isLoading) {
table = <Spin />; table = <Spin />;
} else if (isSuccess) { } else if (isSuccess) {
reqs.list.forEach((req) => {
const dateValue = new Date(req.sendDate).valueOf();
if (min > dateValue) {
min = dateValue;
}
if (max < dateValue) {
max = dateValue;
}
})
table = <Table table = <Table
dataSource={paginatedData(reqs.list)} dataSource={paginateData(data)}
columns={columns} columns={columns}
pagination={false} pagination={false}
rowKey="id" rowKey="id"
ref={tableRef} ref={tableRef}
/>; />;
} else if (isError) { } else if (isError) {
console.log(error); console.log(error);
table = <div>{error.toString()}</div>; table = <div>{error.toString()}</div>;
} }
useEffect(() => { useEffect(() => {
if (isSuccess) { if (isSuccess) {
setData(reqs.list);
dispatch(setTotalReqNumber(reqs.list.length)); dispatch(setTotalReqNumber(reqs.list.length));
setStartDate(dayjs(min));
setEndDate(dayjs(max));
} }
}, [isSuccess, reqs, dispatch]); }, [isSuccess, reqs, dispatch]);
useEffect(() => {
setDefaultDate();
}, [setStartDate])
return ( return (
<div className="container"> <div className="container">
<div className="filter"> <div className="filter">
<div className="fields"> <div className="fields">
<div className="dates"> <div className="dates">
<DatePicker onChange={onSelectStartDate} defaultValue={dayjs('2024/07/11', dateFormat)} format={dateFormat} style={{ marginRight: 14 }} /> <DatePicker
<DatePicker onChange={onSelectEndDate} defaultValue={dayjs('2024/07/11', dateFormat)} format={dateFormat} /> onChange={onSelectStartDate}
format={dateFormat}
defaultValue={startDate}
value={startDate}
style={{ marginRight: 14 }}
/>
<DatePicker
onChange={onSelectEndDate}
format={dateFormat}
defaultValue={endDate}
value={endDate}
/>
</div> </div>
<div className="category"> <div className="category">
<Select placeholder="Не выбрано" style={{ width: 270 }} options={[ <Select
{ placeholder="Не выбрано"
value: "contract", style={{ width: 270 }}
label: "contract" onChange={onSelectMethod}
}, defaultValue={'all'}
{ options={[
value: "dictionary", {
label: "dictionary" value: 'all',
} label: 'Все методы'
]} /> },
{
value: "contract",
label: "contract"
},
{
value: "dictionary",
label: "dictionary"
}
]}
/>
</div> </div>
</div> </div>
<Button onClick={filterByDate} type='primary'>Применить</Button> <Button
type='primary'
onClick={applyFilters}
>
Применить
</Button>
</div> </div>
<div className="data"> <div className="data">
{table} {table}
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment