feat(platform): paginate realtime locations
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
import { Card, Table, Tabs, Toast } from '@douyinfe/semi-ui';
|
||||
import { Button, Card, Form, Select, Space, Table, Tabs, Toast } from '@douyinfe/semi-ui';
|
||||
import { useEffect, useState } from 'react';
|
||||
import { api } from '../api/client';
|
||||
import type { RealtimeLocationRow } from '../api/types';
|
||||
@@ -8,18 +8,50 @@ import { PageHeader } from '../components/PageHeader';
|
||||
export function Realtime() {
|
||||
const [rows, setRows] = useState<RealtimeLocationRow[]>([]);
|
||||
const [loading, setLoading] = useState(true);
|
||||
const [filters, setFilters] = useState<Record<string, string>>({});
|
||||
const [pagination, setPagination] = useState({ currentPage: 1, pageSize: 50, total: 0 });
|
||||
|
||||
useEffect(() => {
|
||||
api.realtimeLocations(new URLSearchParams({ limit: '50' }))
|
||||
.then((page) => setRows(page.items))
|
||||
const load = (values: Record<string, string> = filters, page = pagination.currentPage, pageSize = pagination.pageSize) => {
|
||||
setLoading(true);
|
||||
const params = new URLSearchParams({ limit: String(pageSize), offset: String((page - 1) * pageSize) });
|
||||
if (values?.vin) params.set('vin', values.vin);
|
||||
if (values?.protocol) params.set('protocol', values.protocol);
|
||||
api.realtimeLocations(params)
|
||||
.then((nextPage) => {
|
||||
setRows(nextPage.items);
|
||||
setPagination({ currentPage: page, pageSize, total: nextPage.total });
|
||||
})
|
||||
.catch((error: Error) => Toast.error(error.message))
|
||||
.finally(() => setLoading(false));
|
||||
};
|
||||
|
||||
useEffect(() => {
|
||||
load({}, 1, pagination.pageSize);
|
||||
}, []);
|
||||
|
||||
return (
|
||||
<div className="vp-page">
|
||||
<PageHeader title="实时状态" description="按协议和车辆查看最新实时位置、在线状态和核心数据" />
|
||||
<Card bordered>
|
||||
<Form layout="horizontal" onSubmit={(values) => {
|
||||
const nextFilters = values as Record<string, string>;
|
||||
setFilters(nextFilters);
|
||||
load(nextFilters, 1, pagination.pageSize);
|
||||
}} style={{ marginBottom: 12 }}>
|
||||
<Form.Input field="vin" label="关键词" placeholder="VIN / 车牌" style={{ width: 260 }} />
|
||||
<Form.Select field="protocol" label="协议" placeholder="全部协议" style={{ width: 180 }}>
|
||||
<Select.Option value="GB32960">GB32960</Select.Option>
|
||||
<Select.Option value="JT808">JT808</Select.Option>
|
||||
<Select.Option value="YUTONG_MQTT">YUTONG_MQTT</Select.Option>
|
||||
</Form.Select>
|
||||
<Space>
|
||||
<Button htmlType="submit" theme="solid" type="primary">查询</Button>
|
||||
<Button onClick={() => {
|
||||
setFilters({});
|
||||
load({}, 1, pagination.pageSize);
|
||||
}}>重置</Button>
|
||||
</Space>
|
||||
</Form>
|
||||
<Tabs type="line">
|
||||
<Tabs.TabPane tab="表格视图" itemKey="table">
|
||||
{rows.length === 0 && !loading ? (
|
||||
@@ -27,9 +59,16 @@ export function Realtime() {
|
||||
) : (
|
||||
<Table
|
||||
loading={loading}
|
||||
rowKey="vin"
|
||||
rowKey={(row?: RealtimeLocationRow) => `${row?.vin ?? ''}-${row?.protocol ?? ''}`}
|
||||
dataSource={rows}
|
||||
pagination={false}
|
||||
pagination={{
|
||||
currentPage: pagination.currentPage,
|
||||
pageSize: pagination.pageSize,
|
||||
total: pagination.total,
|
||||
showSizeChanger: true,
|
||||
onPageChange: (page) => load(filters, page, pagination.pageSize),
|
||||
onPageSizeChange: (pageSize) => load(filters, 1, pageSize)
|
||||
}}
|
||||
columns={[
|
||||
{ title: 'VIN', dataIndex: 'vin' },
|
||||
{ title: '车牌', dataIndex: 'plate' },
|
||||
|
||||
Reference in New Issue
Block a user