Skip to content

Commit

Permalink
fix(project): 修复主页主机没有分页的问题
Browse files Browse the repository at this point in the history
Closes: #8
  • Loading branch information
lixin59 committed Aug 5, 2023
1 parent bfdfb6e commit 4046f7a
Show file tree
Hide file tree
Showing 7 changed files with 42 additions and 8 deletions.
30 changes: 26 additions & 4 deletions src/components/HostInfoCard/HostInfoTable.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { Dispatch, SetStateAction, useEffect } from 'react';
import React, { Dispatch, SetStateAction, useEffect, useState } from 'react';
import OmsTable from '../OmsTable/index';
import Card from '@material-ui/core/Card';
import { GroupInfo, HostInfo, IState, PrivateKeyInfo, TagInfo } from '../../store/interface';
Expand Down Expand Up @@ -163,13 +163,15 @@ type tOP = {};
type tSP = tOP & {
hostList: HostInfo[];
groupList: GroupInfo[];
hostTotal: number;
tagList: TagInfo[];
privateKeyList: PrivateKeyInfo[];
};

const mapStateToProps = (state: IState, props: tOP): tSP => ({
...props,
hostList: state.hostList,
hostTotal: state.hostTotal,
groupList: state.groupList,
tagList: state.tagList,
privateKeyList: state.privateKeyList
Expand All @@ -190,14 +192,24 @@ type tProps = tSP &
};

function HostInfoTable(props: tProps) {
const { hostList, upHostList, tipDispatch, formDispatch, setHostId, setHostInfo, setTlc, tagList } = props;
const { hostList, hostTotal, upHostList, tipDispatch, formDispatch, setHostId, setHostInfo, setTlc, tagList } = props;
const navigate = useNavigate();
const { enqueueSnackbar } = useSnackbar();

const [pageSize, setPageSize] = useState(25);

useEffect(() => {
upHostList();
upHostList({ pageSize, pageNo: 1 });
}, []);

const onPageSizeChange = (pageSize: number) => {
setPageSize(pageSize);
upHostList({ pageSize, pageNo: 1 });
};

const onPageChange = (pageNo: number) => {
upHostList({ pageSize, pageNo: pageNo + 1 }); // 组件库是以0开始的所以要加一
};
const getHostList = (hostList: HostInfo[]) => {
return hostList?.map((h) => {
return {
Expand Down Expand Up @@ -232,7 +244,17 @@ function HostInfoTable(props: tProps) {

return (
<Card style={{ width: '98%', height: '80vh', margin: '0 auto', marginTop: '20px' }}>
<OmsTable columns={columns} rows={getHostList(hostList)} />
<OmsTable
columns={columns}
rows={getHostList(hostList)}
rowCount={hostTotal}
pagination
pageSize={pageSize}
rowsPerPageOptions={[25, 50, 100]}
paginationMode={'server'}
onPageSizeChange={onPageSizeChange}
onPageChange={onPageChange}
/>
</Card>
);
}
Expand Down
2 changes: 2 additions & 0 deletions src/components/OmsTable/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ type tProps = DataGridProps & React.RefAttributes<HTMLDivElement>;

export default function OmsTable(props: tProps) {
const { columns, rows } = props;

return (
<Box sx={{ height: '100%', width: '100%' }}>
{rows.length < 1 ? (
Expand All @@ -16,6 +17,7 @@ export default function OmsTable(props: tProps) {
</div>
) : (
<DataGrid
{...props}
rows={rows}
columns={columns}
components={{
Expand Down
3 changes: 2 additions & 1 deletion src/sagas/hostInfoSaga.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,11 @@ export default function* hostInfoSaga() {
init: take(actionType.WILL_INIT_HOST)
});
if (getList) {
const res = yield call(getHostsApi);
const res = yield call(getHostsApi, getList?.value?.pageSize || 1000, getList?.value?.pageNo || 1);
if (res.code !== '200') {
return;
}
yield put({ type: hostActions.UPDATE_HOST_TOTAL, value: res?.data?.total });
yield put({ type: hostActions.INIT, value: res?.data?.data });
}
if (init) {
Expand Down
3 changes: 2 additions & 1 deletion src/store/action-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@ export enum hostActions {
INIT = 'INIT_HOST_INFO',
DELETE_HOST_INFO = 'DELETE_HOST_INFO',
ADD_HOST_INFO = 'ADD_HOST_INFO',
EDIT_HOST_INFO = 'EDIT_HOST_INFO'
EDIT_HOST_INFO = 'EDIT_HOST_INFO',
UPDATE_HOST_TOTAL = 'UPDATE_HOST_TOTAL'
}

export enum groupActions {
Expand Down
4 changes: 2 additions & 2 deletions src/store/action.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ import { GroupInfo, HostInfo, JobInfo, PrivateKeyInfo, TagInfo, TunnelInfo } fro
import { WILL_INIT_APP_VERSION, WILL_INIT_QUICK_COMMAND } from './action-types';

const actions = {
getHostList() {
return { type: actionType.WILL_GET_HOST_LIST };
getHostList(value?: { pageSize: number; pageNo: number }) {
return { type: actionType.WILL_GET_HOST_LIST, value };
},
initHostInfo(value: HostInfo[]) {
return { type: actionType.WILL_INIT_HOST, value };
Expand Down
1 change: 1 addition & 0 deletions src/store/interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ export interface HostAction {
export interface IState {
appVersion: string;
hostList: HostInfo[];
hostTotal: number;
groupList: GroupInfo[];
tagList: TagInfo[];
tunnelList: TunnelInfo[];
Expand Down
7 changes: 7 additions & 0 deletions src/store/reducer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,7 @@ import {
const initialState: IState = {
appVersion: 'v0.0',
hostList: [],
hostTotal: 0,
groupList: [],
tagList: [],
tunnelList: [],
Expand All @@ -179,6 +180,12 @@ const reducer = (state = initialState, action: HostAction) => {
hostList: action.value
};
}
case hostActions.UPDATE_HOST_TOTAL: {
return {
...state,
hostTotal: action.value
};
}
case groupActions.INIT: {
return {
...state,
Expand Down

0 comments on commit 4046f7a

Please sign in to comment.