Skip to content

Commit

Permalink
[frontend] Implementation of sorting columns in Storage browser table…
Browse files Browse the repository at this point in the history
… (#3588)

* [frontend] Implementation of sorting columns in Storage browser table

* [frontend] Added option to unsort the table and suggested improvements

* [frontend] Replaced empty div with null in ternary operator

* [frontend] Moved enum to types.ts file and necessary changes
  • Loading branch information
nidhibhatg authored Dec 21, 2023
1 parent 651a090 commit 200c9bf
Show file tree
Hide file tree
Showing 5 changed files with 93 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@ import { fetchFiles } from '../../../../reactComponents/FileChooser/api';
import {
PathAndFileData,
StorageBrowserTableData,
PageStats
PageStats,
SortOrder
} from '../../../../reactComponents/FileChooser/types';

import './StorageBrowserTabContent.scss';
Expand All @@ -62,6 +63,10 @@ const StorageBrowserTabContent: React.FC<StorageBrowserTabContentProps> = ({
const [pageStats, setPageStats] = useState<PageStats>();
const [pageSize, setPageSize] = useState<number>();
const [pageNumber, setPageNumber] = useState<number>(1);
const [sortByColumn, setSortByColumn] = useState<string>('');
const [sortOrder, setSortOrder] = useState<SortOrder>(SortOrder.NONE);
//TODO: Add filter functionality
const [filterData, setFilterData] = useState<string>('');

Check warning on line 69 in desktop/core/src/desktop/js/apps/storageBrowser/StorageBrowserPage/StorageBrowserTabContents/StorageBrowserTabContent.tsx

View workflow job for this annotation

GitHub Actions / build

'setFilterData' is assigned a value but never used

const { t } = i18nReact.useTranslation();

Expand Down Expand Up @@ -132,16 +137,16 @@ const StorageBrowserTabContent: React.FC<StorageBrowserTabContentProps> = ({

useEffect(() => {
setloadingFiles(true);
fetchFiles(filePath, pageSize, pageNumber)
fetchFiles(filePath, pageSize, pageNumber, filterData, sortByColumn, sortOrder)
.then(responseFilesData => {
setFilesData(responseFilesData);
const tableData: StorageBrowserTableData[] = responseFilesData.files.map(file => ({
name: file.name,
size: file.humansize,
user: file.stats.user,
groups: file.stats.group,
group: file.stats.group,
permission: file.rwx,
lastUpdated: file.mtime,
mtime: file.mtime,
type: file.type,
path: file.path
}));
Expand All @@ -156,7 +161,7 @@ const StorageBrowserTabContent: React.FC<StorageBrowserTabContentProps> = ({
.finally(() => {
setloadingFiles(false);
});
}, [filePath, pageSize, pageNumber]);
}, [filePath, pageSize, pageNumber, sortByColumn, sortOrder]);

return (
<Spin spinning={loadingFiles}>
Expand Down Expand Up @@ -217,6 +222,10 @@ const StorageBrowserTabContent: React.FC<StorageBrowserTabContentProps> = ({
onFilepathChange={setFilePath}
onPageSizeChange={setPageSize}
onPageNumberChange={setPageNumber}
onSortByColumnChange={setSortByColumn}
onSortOrderChange={setSortOrder}
sortByColumn={sortByColumn}
sortOrder={sortOrder}
></StorageBrowserTable>
</div>
</Spin>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@ $cell-height: 40px;
th.ant-table-cell {
text-align: left;
}

table {
width: 100%;
}
}

.hue-storage-browser__table-row {
Expand All @@ -55,3 +59,11 @@ $cell-height: 40px;
.hue-storage-browser__table-cell-name {
color: vars.$fluidx-blue-700;
}

.hue-storage-browser__table-column-header {
display: flex;
}

.hue-storage-browser__table-column-title {
text-transform: capitalize;
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,16 @@ import { i18nReact } from '../../../../utils/i18nReact';
import Table from 'cuix/dist/components/Table';
import { ColumnProps } from 'antd/lib/table';
import FolderIcon from '@cloudera/cuix-core/icons/react/ProjectIcon';
import SortAscending from '@cloudera/cuix-core/icons/react/SortAscendingIcon';
import SortDescending from '@cloudera/cuix-core/icons/react/SortDescendingIcon';
//TODO: Use cuix icon (Currently fileIcon does not exist in cuix)
import { FileOutlined } from '@ant-design/icons';

import { PageStats, StorageBrowserTableData } from '../../../../reactComponents/FileChooser/types';
import {
PageStats,
StorageBrowserTableData,
SortOrder
} from '../../../../reactComponents/FileChooser/types';
import Pagination from '../../../../reactComponents/Pagination/Pagination';
import './StorageBrowserTable.scss';
import Tooltip from 'antd/es/tooltip';
Expand All @@ -33,8 +39,12 @@ interface StorageBrowserTableProps {
onFilepathChange: (path: string) => void;
onPageNumberChange: (pageNumber: number) => void;
onPageSizeChange: (pageSize: number) => void;
onSortByColumnChange: (sortByColumn: string) => void;
onSortOrderChange: (sortOrder: SortOrder) => void;
pageSize: number;
pageStats: PageStats;
sortByColumn: string;
sortOrder: SortOrder;
rowClassName?: string;
testId?: string;
}
Expand All @@ -51,6 +61,10 @@ const StorageBrowserTable: React.FC<StorageBrowserTableProps> = ({
onFilepathChange,
onPageNumberChange,
onPageSizeChange,
onSortByColumnChange,
onSortOrderChange,
sortByColumn,
sortOrder,
pageSize,
pageStats,
rowClassName,
Expand All @@ -61,12 +75,43 @@ const StorageBrowserTable: React.FC<StorageBrowserTableProps> = ({

const { t } = i18nReact.useTranslation();

const onColumnTitleClicked = (columnClicked: string) => {
if (columnClicked === sortByColumn) {
if (sortOrder === SortOrder.NONE) {
onSortOrderChange(SortOrder.ASC);
} else if (sortOrder === SortOrder.ASC) {
onSortOrderChange(SortOrder.DSC);
} else {
onSortOrderChange(SortOrder.NONE);
}
} else {
onSortByColumnChange(columnClicked);
onSortOrderChange(SortOrder.ASC);
}
};

const getColumns = file => {
const columns: ColumnProps<unknown>[] = [];
for (const [key] of Object.entries(file)) {
const column: ColumnProps<unknown> = {
dataIndex: `${key}`,
title: t(`${key}`.charAt(0).toUpperCase() + `${key}`.slice(1)),
dataIndex: key,
title: (
<div
className="hue-storage-browser__table-column-header"
onClick={() => onColumnTitleClicked(key)}
>
<div className="hue-storage-browser__table-column-title">
{key === 'mtime' ? t('Last Updated') : t(key)}
</div>
{key === sortByColumn ? (
sortOrder === SortOrder.DSC ? (
<SortDescending />
) : sortOrder === SortOrder.ASC ? (
<SortAscending />
) : null
) : null}
</div>
),
key: `${key}`
};
if (key === 'name') {
Expand All @@ -81,7 +126,7 @@ const StorageBrowserTable: React.FC<StorageBrowserTableProps> = ({
</Tooltip>
);
} else {
column.width = key === 'lastUpdated' ? '15%' : '10%';
column.width = key === 'mtime' ? '15%' : '10%';
}
columns.push(column);
}
Expand Down
13 changes: 10 additions & 3 deletions desktop/core/src/desktop/js/reactComponents/FileChooser/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

import { get } from '../../api/utils';
import { CancellablePromise } from '../../api/cancellablePromise';
import { PathAndFileData } from './types';
import { PathAndFileData, SortOrder } from './types';

const FILESYSTEMS_API_URL = '/api/v1/storage/filesystems';
const VIEWFILES_API_URl = '/api/v1/storage/view=';
Expand All @@ -35,14 +35,21 @@ export const fetchFiles = (
pagenum?: number,
filter?: string,
sortby?: string,
descending?: boolean
sortOrder?: SortOrder
): CancellablePromise<PathAndFileData> => {
let descending = false;
if (sortOrder === SortOrder.ASC) {
descending = false;
} else if (sortOrder === SortOrder.DSC) {
descending = true;
} else {
sortby = '';
}
//If value is undefined default value is assigned.
pagesize = pagesize || 10;
pagenum = pagenum || 1;
filter = filter || '';
sortby = sortby || '';
descending = descending || false;
return get(
VIEWFILES_API_URl +
filePath +
Expand Down
10 changes: 8 additions & 2 deletions desktop/core/src/desktop/js/reactComponents/FileChooser/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,9 @@ export interface StorageBrowserTableData {
name: string;
size: string;
user: string;
groups: string;
group: string;
permission: string;
lastUpdated: string;
mtime: string;
type: string;
path: string;
}
Expand All @@ -79,3 +79,9 @@ export interface PathAndFileData {
page: PageStats;
pagesize: number;
}

export enum SortOrder {
ASC = 'ascending',
DSC = 'descending',
NONE = 'none'
}

0 comments on commit 200c9bf

Please sign in to comment.