Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion client/app/components/items-list/ItemsList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ export interface Controller<I, P = any> {
orderByField?: string;
orderByReverse: boolean;
toggleSorting: (orderByField: string) => void;
setSorting: (orderByField: string, orderByReverse: boolean) => void;

// pagination
page: number;
Expand Down Expand Up @@ -139,10 +140,11 @@ export function wrap<I, P = any>(
this.props.onError!(error);

const initialState = this.getState({ ...itemsSource.getState(), isLoaded: false });
const { updatePagination, toggleSorting, updateSearch, updateSelectedTags, update, handleError } = itemsSource;
const { updatePagination, toggleSorting, setSorting, updateSearch, updateSelectedTags, update, handleError } = itemsSource;
this.state = {
...initialState,
toggleSorting, // eslint-disable-line react/no-unused-state
setSorting, // eslint-disable-line react/no-unused-state
updateSearch: debounce(updateSearch, 200), // eslint-disable-line react/no-unused-state
updateSelectedTags, // eslint-disable-line react/no-unused-state
updatePagination, // eslint-disable-line react/no-unused-state
Expand Down
25 changes: 15 additions & 10 deletions client/app/components/items-list/classes/ItemsSource.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,14 +39,12 @@ export class ItemsSource {
const customParams = {};
const context = {
...this.getCallbackContext(),
setCustomParams: params => {
setCustomParams: (params) => {
extend(customParams, params);
},
};
return this._beforeUpdate().then(() => {
const fetchToken = Math.random()
.toString(36)
.substr(2);
const fetchToken = Math.random().toString(36).substr(2);
this._currentFetchToken = fetchToken;
return this._fetcher
.fetch(changes, state, context)
Expand All @@ -59,7 +57,7 @@ export class ItemsSource {
return this._afterUpdate();
}
})
.catch(error => this.handleError(error));
.catch((error) => this.handleError(error));
});
}

Expand Down Expand Up @@ -124,13 +122,20 @@ export class ItemsSource {
});
};

toggleSorting = orderByField => {
toggleSorting = (orderByField) => {
this._sorter.toggleField(orderByField);
this._savedOrderByField = this._sorter.field;
this._changed({ sorting: true });
};

updateSearch = searchTerm => {
setSorting = (orderByField, orderByReverse) => {
this._sorter.setField(orderByField);
this._sorter.setReverse(orderByReverse);
this._savedOrderByField = this._sorter.field;
this._changed({ sorting: true });
};

updateSearch = (searchTerm) => {
// here we update state directly, but later `fetchData` will update it properly
this._searchTerm = searchTerm;
// in search mode ignore the ordering and use the ranking order
Expand All @@ -145,15 +150,15 @@ export class ItemsSource {
this._changed({ search: true, pagination: { page: true } });
};

updateSelectedTags = selectedTags => {
updateSelectedTags = (selectedTags) => {
this._selectedTags = selectedTags;
this._paginator.setPage(1);
this._changed({ tags: true, pagination: { page: true } });
};

update = () => this._changed();

handleError = error => {
handleError = (error) => {
if (isFunction(this.onError)) {
this.onError(error);
}
Expand All @@ -172,7 +177,7 @@ export class ResourceItemsSource extends ItemsSource {
processResults: (results, context) => {
let processItem = getItemProcessor(context);
processItem = isFunction(processItem) ? processItem : identity;
return map(results, item => processItem(item, context));
return map(results, (item) => processItem(item, context));
},
});
}
Expand Down
51 changes: 35 additions & 16 deletions client/app/components/items-list/components/ItemsTable.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -44,15 +44,15 @@ export const Columns = {
date(overrides) {
return extend(
{
render: text => formatDate(text),
render: (text) => formatDate(text),
},
overrides
);
},
dateTime(overrides) {
return extend(
{
render: text => formatDateTime(text),
render: (text) => formatDateTime(text),
},
overrides
);
Expand All @@ -62,15 +62,15 @@ export const Columns = {
{
width: "1%",
className: "text-nowrap",
render: text => durationHumanize(text),
render: (text) => durationHumanize(text),
},
overrides
);
},
timeAgo(overrides, timeAgoCustomProps = undefined) {
return extend(
{
render: value => <TimeAgo date={value} {...timeAgoCustomProps} />,
render: (value) => <TimeAgo date={value} {...timeAgoCustomProps} />,
},
overrides
);
Expand Down Expand Up @@ -110,6 +110,7 @@ export default class ItemsTable extends React.Component {
orderByField: PropTypes.string,
orderByReverse: PropTypes.bool,
toggleSorting: PropTypes.func,
setSorting: PropTypes.func,
"data-test": PropTypes.string,
rowKey: PropTypes.oneOfType([PropTypes.string, PropTypes.func]),
};
Expand All @@ -127,33 +128,29 @@ export default class ItemsTable extends React.Component {
};

prepareColumns() {
const { orderByField, orderByReverse, toggleSorting } = this.props;
const { orderByField, orderByReverse } = this.props;
const orderByDirection = orderByReverse ? "descend" : "ascend";

return map(
map(
filter(this.props.columns, column => (isFunction(column.isAvailable) ? column.isAvailable() : true)),
column => extend(column, { orderByField: column.orderByField || column.field })
filter(this.props.columns, (column) => (isFunction(column.isAvailable) ? column.isAvailable() : true)),
(column) => extend(column, { orderByField: column.orderByField || column.field })
),
(column, index) => {
// Bind click events only to sortable columns
const onHeaderCell = column.sorter ? () => ({ onClick: () => toggleSorting(column.orderByField) }) : null;

// Wrap render function to pass correct arguments
const render = isFunction(column.render) ? (text, row) => column.render(text, row.item) : identity;

return extend(omit(column, ["field", "orderByField", "render"]), {
key: "column" + index,
dataIndex: ["item", column.field],
defaultSortOrder: column.orderByField === orderByField ? orderByDirection : null,
onHeaderCell,
render,
});
}
);
}

getRowKey = record => {
getRowKey = (record) => {
const { rowKey } = this.props;
if (rowKey) {
if (isFunction(rowKey)) {
Expand All @@ -172,22 +169,43 @@ export default class ItemsTable extends React.Component {

// Bind events only if `onRowClick` specified
const onTableRow = isFunction(this.props.onRowClick)
? row => ({
onClick: event => {
? (row) => ({
onClick: (event) => {
this.props.onRowClick(event, row.item);
},
})
: null;

const onChange = (pagination, filters, sorter, extra) => {
const action = extra?.action;
if (action === "sort") {
const propsColumn = this.props.columns.find((column) => column.field === sorter.field[1]);
if (!propsColumn.sorter) {
return;
}
let orderByField = propsColumn.orderByField;
const orderByReverse = sorter.order === "descend";

if (orderByReverse === undefined) {
orderByField = null;
}
if (this.props.setSorting) {
this.props.setSorting(orderByField, orderByReverse);
} else {
this.props.toggleSorting(orderByField);
}
}
};

const { showHeader } = this.props;
if (this.props.loading) {
if (isEmpty(tableDataProps.dataSource)) {
tableDataProps.columns = tableDataProps.columns.map(column => ({
tableDataProps.columns = tableDataProps.columns.map((column) => ({
...column,
sorter: false,
render: () => <Skeleton active paragraph={false} />,
}));
tableDataProps.dataSource = range(10).map(key => ({ key: `${key}` }));
tableDataProps.dataSource = range(10).map((key) => ({ key: `${key}` }));
} else {
tableDataProps.loading = { indicator: null };
}
Expand All @@ -200,6 +218,7 @@ export default class ItemsTable extends React.Component {
rowKey={this.getRowKey}
pagination={false}
onRow={onTableRow}
onChange={onChange}
data-test={this.props["data-test"]}
{...tableDataProps}
/>
Expand Down
15 changes: 8 additions & 7 deletions client/app/pages/queries-list/QueriesList.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -160,14 +160,15 @@ function QueriesList({ controller }) {
orderByField={controller.orderByField}
orderByReverse={controller.orderByReverse}
toggleSorting={controller.toggleSorting}
setSorting={controller.setSorting}
/>
<Paginator
showPageSizeSelect
totalCount={controller.totalItemsCount}
pageSize={controller.itemsPerPage}
onPageSizeChange={itemsPerPage => controller.updatePagination({ itemsPerPage })}
onPageSizeChange={(itemsPerPage) => controller.updatePagination({ itemsPerPage })}
page={controller.page}
onChange={page => controller.updatePagination({ page })}
onChange={(page) => controller.updatePagination({ page })}
/>
</div>
</React.Fragment>
Expand Down Expand Up @@ -196,7 +197,7 @@ const QueriesListPage = itemsList(
}[currentPage];
},
getItemProcessor() {
return item => new Query(item);
return (item) => new Query(item);
},
}),
() => new UrlStateStorage({ orderByField: "created_at", orderByReverse: true })
Expand All @@ -207,30 +208,30 @@ routes.register(
routeWithUserSession({
path: "/queries",
title: "Queries",
render: pageProps => <QueriesListPage {...pageProps} currentPage="all" />,
render: (pageProps) => <QueriesListPage {...pageProps} currentPage="all" />,
})
);
routes.register(
"Queries.Favorites",
routeWithUserSession({
path: "/queries/favorites",
title: "Favorite Queries",
render: pageProps => <QueriesListPage {...pageProps} currentPage="favorites" />,
render: (pageProps) => <QueriesListPage {...pageProps} currentPage="favorites" />,
})
);
routes.register(
"Queries.Archived",
routeWithUserSession({
path: "/queries/archive",
title: "Archived Queries",
render: pageProps => <QueriesListPage {...pageProps} currentPage="archive" />,
render: (pageProps) => <QueriesListPage {...pageProps} currentPage="archive" />,
})
);
routes.register(
"Queries.My",
routeWithUserSession({
path: "/queries/my",
title: "My Queries",
render: pageProps => <QueriesListPage {...pageProps} currentPage="my" />,
render: (pageProps) => <QueriesListPage {...pageProps} currentPage="my" />,
})
);
Loading