Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

support direct table filter link #146

Merged
merged 3 commits into from
Sep 1, 2020
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
28 changes: 28 additions & 0 deletions ui/src/Hooks/TableHooks.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { useState, useEffect } from "react";

export const useTableFilters = () => {
const [state, setState] = useState({});

// filter table was changed when the state (table properties) was changed
useEffect(() => {
const searchParams = new window.URLSearchParams(window.location.search);
Object.keys(state).map((key) => {
searchParams.set(key, state[key]);
});

// We don't want keep table action in browser back/forward history
window.history.replaceState(
null,
null,
decodeURIComponent(`?${searchParams.toString()}`)
);
}, [state]);

const handleChange = (filters = []) => {
const newFilters = {};
filters.forEach((filter) => (newFilters[filter.key] = filter.value));
Object.assign(state, newFilters);
setState({ ...state });
};
return [handleChange];
};
2 changes: 1 addition & 1 deletion ui/src/components/Dashboard/FilterBar.js
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ const FilterBar = ({
}
});

updateFilters(filters);
setFilters(filters);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

update filters updates the history as well incase of bad filter passed at the url.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what is a bad filter? this code removes the table filters.
If we have a "bad" filter the code will not use them

if (resource) {
setResource(resource);
}
Expand Down
4 changes: 1 addition & 3 deletions ui/src/components/Dashboard/Index.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ const DashboardIndex = ({
filters,
}) => {
const classes = useStyles();

/**
* Will clear selected filter and show main page
*/
Expand Down Expand Up @@ -73,8 +72,7 @@ const DashboardIndex = ({
<FilterBar />
<StatisticsBar />
<ResourcesList />
{!currentResource && <ResourcesChart />}
{currentResource && <ResourceTable />}
{currentResource ? <ResourceTable /> : <ResourcesChart />}
</Fragment>
);
};
Expand Down
42 changes: 37 additions & 5 deletions ui/src/components/Dashboard/ResourceTable.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import MUIDataTable from "mui-datatables";
import TextUtils from "utils/Text";
import TagsDialog from "../Dialog/Tags";
import ReportProblemIcon from "@material-ui/icons/ReportProblem";
import { getHistory } from "../../utils/History";
import { useTableFilters } from "../../Hooks/TableHooks";

import {
makeStyles,
Expand Down Expand Up @@ -54,13 +56,23 @@ const ResourceTable = ({
const [headers, setHeaders] = useState([]);
const [errorMessage, setErrorMessage] = useState(false);
const [hasError, setHasError] = useState(false);

const classes = useStyles();
const [setTableFilters] = useTableFilters({});
const [tableOptions, setTableOptions] = useState({});

const tableOptions = {
selectableRows: "none",
responsive: "standard",
};
// setting table configuration on first load
useEffect(() => {
setTableOptions({
page: parseInt(getHistory("page", 0)),
searchText: getHistory("search", ""),
sortOrder: {
name: getHistory("sortColumn", ""),
direction: getHistory("direction", "desc"),
},
selectableRows: "none",
responsive: "standard",
});
}, []);

/**
* format table cell by type
Expand Down Expand Up @@ -191,6 +203,26 @@ const ResourceTable = ({
data={currentResourceData}
columns={headers}
options={Object.assign(tableOptions, {
onSearchChange: (searchText) => {
setTableFilters([
{
key: "search",
value: searchText ? searchText : "",
},
]);
},
onColumnSortChange: (changedColumn, direction) => {
setTableFilters([
{ key: "sortColumn", value: changedColumn },
{ key: "direction", value: direction },
]);
},
onChangePage: (currentPage) => {
setTableFilters([{ key: "page", value: currentPage }]);
},
onChangeRowsPerPage: (numberOfRows) => {
setTableFilters([{ key: "rows", value: numberOfRows }]);
},
downloadOptions: {
filename: `${currentResource}.csv`,
},
Expand Down
9 changes: 7 additions & 2 deletions ui/src/utils/History.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,10 +62,15 @@ export const setHistory = (historyParams = {}) => {
/**
*
* @param {string} query params name from url
* @param {any} defaultValue return default value in case the query not exists
* @returns {string} Param value from url
*/
export const getHistory = (query) => {
export const getHistory = (query, defaultValue = null) => {
const searchParams = new window.URLSearchParams(window.location.search);
const searchQuery = searchParams.get(query);
return searchQuery;
if (searchQuery) {
return searchQuery;
} else {
return defaultValue;
}
};