balibabu commited on
Commit
d8f8ecb
·
1 Parent(s): 0c9dbca

fix: fetch file list by react-query #1306 (#1640)

Browse files

### What problem does this PR solve?

fix: fetch file list by react-query #1306

### Type of change

- [x] Bug Fix (non-breaking change which fixes an issue)

web/src/hooks/file-manager-hooks.ts CHANGED
@@ -1,10 +1,21 @@
 
1
  import {
2
  IConnectRequestBody,
3
  IFileListRequestBody,
4
  } from '@/interfaces/request/file-manager';
5
- import { UploadFile } from 'antd';
6
- import { useCallback } from 'react';
7
- import { useDispatch, useSelector } from 'umi';
 
 
 
 
 
 
 
 
 
 
8
 
9
  export const useFetchFileList = () => {
10
  const dispatch = useDispatch();
@@ -22,6 +33,56 @@ export const useFetchFileList = () => {
22
  return fetchFileList;
23
  };
24
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
25
  export const useRemoveFile = () => {
26
  const dispatch = useDispatch();
27
 
 
1
+ import { ResponseType } from '@/interfaces/database/base';
2
  import {
3
  IConnectRequestBody,
4
  IFileListRequestBody,
5
  } from '@/interfaces/request/file-manager';
6
+ import fileManagerService from '@/services/file-manager-service';
7
+ import { useQuery } from '@tanstack/react-query';
8
+ import { PaginationProps, UploadFile } from 'antd';
9
+ import React, { useCallback } from 'react';
10
+ import { useDispatch, useSearchParams, useSelector } from 'umi';
11
+ import { useGetNextPagination, useHandleSearchChange } from './logic-hooks';
12
+
13
+ export const useGetFolderId = () => {
14
+ const [searchParams] = useSearchParams();
15
+ const id = searchParams.get('folderId') as string;
16
+
17
+ return id ?? '';
18
+ };
19
 
20
  export const useFetchFileList = () => {
21
  const dispatch = useDispatch();
 
33
  return fetchFileList;
34
  };
35
 
36
+ export interface IListResult {
37
+ searchString: string;
38
+ handleInputChange: React.ChangeEventHandler<HTMLInputElement>;
39
+ pagination: PaginationProps;
40
+ setPagination: (pagination: { page: number; pageSize: number }) => void;
41
+ }
42
+
43
+ export const useFetchNextFileList = (): ResponseType<any> & IListResult => {
44
+ const { searchString, handleInputChange } = useHandleSearchChange();
45
+ const { pagination, setPagination } = useGetNextPagination();
46
+ const id = useGetFolderId();
47
+
48
+ const { data } = useQuery({
49
+ queryKey: [
50
+ 'fetchFileList',
51
+ id,
52
+ pagination.current,
53
+ pagination.pageSize,
54
+ searchString,
55
+ ],
56
+ initialData: {},
57
+ queryFn: async () => {
58
+ const { data } = await fileManagerService.listFile({
59
+ parent_id: id,
60
+ keywords: searchString,
61
+ page_size: pagination.pageSize,
62
+ page: pagination.current,
63
+ });
64
+
65
+ return data;
66
+ },
67
+ });
68
+
69
+ const onInputChange: React.ChangeEventHandler<HTMLInputElement> = useCallback(
70
+ (e) => {
71
+ setPagination({ page: 1 });
72
+ handleInputChange(e);
73
+ },
74
+ [handleInputChange, setPagination],
75
+ );
76
+
77
+ return {
78
+ ...data,
79
+ searchString,
80
+ handleInputChange: onInputChange,
81
+ pagination: { ...pagination, total: data?.data?.total },
82
+ setPagination,
83
+ };
84
+ };
85
+
86
  export const useRemoveFile = () => {
87
  const dispatch = useDispatch();
88
 
web/src/hooks/logic-hooks.ts CHANGED
@@ -71,6 +71,20 @@ export const useSetSelectedRecord = <T = IKnowledgeFile>() => {
71
  return { currentRecord, setRecord };
72
  };
73
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
74
  export const useChangeLanguage = () => {
75
  const { i18n } = useTranslation();
76
  const saveSetting = useSaveSetting();
@@ -85,6 +99,46 @@ export const useChangeLanguage = () => {
85
  return changeLanguage;
86
  };
87
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
88
  export const useGetPagination = (
89
  total: number,
90
  page: number,
 
71
  return { currentRecord, setRecord };
72
  };
73
 
74
+ export const useHandleSearchChange = () => {
75
+ const [searchString, setSearchString] = useState('');
76
+
77
+ const handleInputChange = useCallback(
78
+ (e: React.ChangeEvent<HTMLInputElement | HTMLTextAreaElement>) => {
79
+ const value = e.target.value;
80
+ setSearchString(value);
81
+ },
82
+ [],
83
+ );
84
+
85
+ return { handleInputChange, searchString };
86
+ };
87
+
88
  export const useChangeLanguage = () => {
89
  const { i18n } = useTranslation();
90
  const saveSetting = useSaveSetting();
 
99
  return changeLanguage;
100
  };
101
 
102
+ export const useGetNextPagination = () => {
103
+ const { t } = useTranslate('common');
104
+ const [{ page, pageSize }, setPagination] = useState({
105
+ page: 1,
106
+ pageSize: 10,
107
+ });
108
+
109
+ const onPageChange: PaginationProps['onChange'] = useCallback(
110
+ (pageNumber: number, pageSize: number) => {
111
+ setPagination({ page: pageNumber, pageSize });
112
+ },
113
+ [setPagination],
114
+ );
115
+
116
+ const setCurrentPagination = useCallback(
117
+ (pagination: { page: number; pageSize?: number }) => {
118
+ setPagination((p) => ({ ...p, ...pagination }));
119
+ },
120
+ [setPagination],
121
+ );
122
+
123
+ const pagination: PaginationProps = useMemo(() => {
124
+ return {
125
+ showQuickJumper: true,
126
+ total: 0,
127
+ showSizeChanger: true,
128
+ current: page,
129
+ pageSize: pageSize,
130
+ pageSizeOptions: [1, 2, 10, 20, 50, 100],
131
+ onChange: onPageChange,
132
+ showTotal: (total) => `${t('total')} ${total}`,
133
+ };
134
+ }, [t, onPageChange, page, pageSize]);
135
+
136
+ return {
137
+ pagination,
138
+ setPagination: setCurrentPagination,
139
+ };
140
+ };
141
+
142
  export const useGetPagination = (
143
  total: number,
144
  page: number,
web/src/pages/file-manager/file-toolbar.tsx CHANGED
@@ -19,17 +19,19 @@ import {
19
  } from 'antd';
20
  import { useMemo } from 'react';
21
  import {
22
- useFetchDocumentListOnMount,
23
  useHandleBreadcrumbClick,
24
  useHandleDeleteFile,
25
- useHandleSearchChange,
26
  useSelectBreadcrumbItems,
27
  } from './hooks';
28
 
29
- import { useSelectParentFolderList } from '@/hooks/file-manager-hooks';
 
 
 
30
  import styles from './index.less';
31
 
32
- interface IProps {
 
33
  selectedRowKeys: string[];
34
  showFolderCreateModal: () => void;
35
  showFileUploadModal: () => void;
@@ -41,10 +43,10 @@ const FileToolbar = ({
41
  showFolderCreateModal,
42
  showFileUploadModal,
43
  setSelectedRowKeys,
 
 
44
  }: IProps) => {
45
  const { t } = useTranslate('knowledgeDetails');
46
- useFetchDocumentListOnMount();
47
- const { handleInputChange, searchString } = useHandleSearchChange();
48
  const breadcrumbItems = useSelectBreadcrumbItems();
49
  const { handleBreadcrumbClick } = useHandleBreadcrumbClick();
50
  const parentFolderList = useSelectParentFolderList();
 
19
  } from 'antd';
20
  import { useMemo } from 'react';
21
  import {
 
22
  useHandleBreadcrumbClick,
23
  useHandleDeleteFile,
 
24
  useSelectBreadcrumbItems,
25
  } from './hooks';
26
 
27
+ import {
28
+ IListResult,
29
+ useSelectParentFolderList,
30
+ } from '@/hooks/file-manager-hooks';
31
  import styles from './index.less';
32
 
33
+ interface IProps
34
+ extends Pick<IListResult, 'searchString' | 'handleInputChange'> {
35
  selectedRowKeys: string[];
36
  showFolderCreateModal: () => void;
37
  showFileUploadModal: () => void;
 
43
  showFolderCreateModal,
44
  showFileUploadModal,
45
  setSelectedRowKeys,
46
+ searchString,
47
+ handleInputChange,
48
  }: IProps) => {
49
  const { t } = useTranslate('knowledgeDetails');
 
 
50
  const breadcrumbItems = useSelectBreadcrumbItems();
51
  const { handleBreadcrumbClick } = useHandleBreadcrumbClick();
52
  const parentFolderList = useSelectParentFolderList();
web/src/pages/file-manager/index.tsx CHANGED
@@ -1,4 +1,4 @@
1
- import { useSelectFileList } from '@/hooks/file-manager-hooks';
2
  import { IFile } from '@/interfaces/database/file-manager';
3
  import { formatDate } from '@/utils/date';
4
  import { Button, Flex, Space, Table, Tag, Typography } from 'antd';
@@ -6,7 +6,6 @@ import { ColumnsType } from 'antd/es/table';
6
  import ActionCell from './action-cell';
7
  import FileToolbar from './file-toolbar';
8
  import {
9
- useGetFilesPagination,
10
  useGetRowSelection,
11
  useHandleConnectToKnowledge,
12
  useHandleCreateFolder,
@@ -30,7 +29,7 @@ const { Text } = Typography;
30
 
31
  const FileManager = () => {
32
  const { t } = useTranslate('fileManager');
33
- const fileList = useSelectFileList();
34
  const { rowSelection, setSelectedRowKeys } = useGetRowSelection();
35
  const loading = useSelectFileListLoading();
36
  const navigateToOtherFolder = useNavigateToOtherFolder();
@@ -64,8 +63,9 @@ const FileManager = () => {
64
  initialValue,
65
  connectToKnowledgeLoading,
66
  } = useHandleConnectToKnowledge();
67
- const { pagination } = useGetFilesPagination();
68
-
 
69
  const columns: ColumnsType<IFile> = [
70
  {
71
  title: t('name'),
@@ -151,13 +151,15 @@ const FileManager = () => {
151
  return (
152
  <section className={styles.fileManagerWrapper}>
153
  <FileToolbar
 
 
154
  selectedRowKeys={rowSelection.selectedRowKeys as string[]}
155
  showFolderCreateModal={showFolderCreateModal}
156
  showFileUploadModal={showFileUploadModal}
157
  setSelectedRowKeys={setSelectedRowKeys}
158
  ></FileToolbar>
159
  <Table
160
- dataSource={fileList}
161
  columns={columns}
162
  rowKey={'id'}
163
  rowSelection={rowSelection}
 
1
+ import { useFetchNextFileList } from '@/hooks/file-manager-hooks';
2
  import { IFile } from '@/interfaces/database/file-manager';
3
  import { formatDate } from '@/utils/date';
4
  import { Button, Flex, Space, Table, Tag, Typography } from 'antd';
 
6
  import ActionCell from './action-cell';
7
  import FileToolbar from './file-toolbar';
8
  import {
 
9
  useGetRowSelection,
10
  useHandleConnectToKnowledge,
11
  useHandleCreateFolder,
 
29
 
30
  const FileManager = () => {
31
  const { t } = useTranslate('fileManager');
32
+ // const fileList = useSelectFileList();
33
  const { rowSelection, setSelectedRowKeys } = useGetRowSelection();
34
  const loading = useSelectFileListLoading();
35
  const navigateToOtherFolder = useNavigateToOtherFolder();
 
63
  initialValue,
64
  connectToKnowledgeLoading,
65
  } = useHandleConnectToKnowledge();
66
+ // const { pagination } = useGetFilesPagination();
67
+ const { pagination, data, searchString, handleInputChange } =
68
+ useFetchNextFileList();
69
  const columns: ColumnsType<IFile> = [
70
  {
71
  title: t('name'),
 
151
  return (
152
  <section className={styles.fileManagerWrapper}>
153
  <FileToolbar
154
+ searchString={searchString}
155
+ handleInputChange={handleInputChange}
156
  selectedRowKeys={rowSelection.selectedRowKeys as string[]}
157
  showFolderCreateModal={showFolderCreateModal}
158
  showFileUploadModal={showFileUploadModal}
159
  setSelectedRowKeys={setSelectedRowKeys}
160
  ></FileToolbar>
161
  <Table
162
+ dataSource={data?.files}
163
  columns={columns}
164
  rowKey={'id'}
165
  rowSelection={rowSelection}