Skip to content
This repository has been archived by the owner on Nov 27, 2020. It is now read-only.

feat: keep search results when coming back from the (new/edit) flag page #73

Merged
merged 1 commit into from
May 2, 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
10 changes: 6 additions & 4 deletions web/src/components/SearchInput/SearchInput.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { useCallback } from 'react';
import React from 'react';
import PropTypes from 'prop-types';
import clsx from 'clsx';
import { debounce } from 'lodash';
Expand Down Expand Up @@ -27,10 +27,10 @@ const useStyles = makeStyles(theme => ({
}));

const SearchInput = props => {
const { className, onChange, style, ...rest } = props;
const { className, search, onChange, style, ...rest } = props;

const classes = useStyles();
const handler = useCallback(debounce(onChange, 300), []);
const handler = debounce(onChange, 300);

return (
<Paper
Expand All @@ -44,14 +44,16 @@ const SearchInput = props => {
autoFocus
className={classes.input}
disableUnderline
onChange={e => handler(e.target.value || undefined)}
defaultValue={search}
onChange={e => handler(e.target.value)}
/>
</Paper>
);
};

SearchInput.propTypes = {
className: PropTypes.string,
search: PropTypes.string,
onChange: PropTypes.func.isRequired,
style: PropTypes.object,
};
Expand Down
15 changes: 10 additions & 5 deletions web/src/views/FlagList/FlagList.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ const useStyles = makeStyles(theme => ({
}));

const rowsPerPageKey = 'rowsPerPage';
const searchKey = 'flagSearch';
const rowsPerPageOptions = [10, 25, 50, { value: -1, label: 'All' }];

function EmptyMessage({ message }) {
Expand All @@ -30,7 +31,7 @@ function EmptyMessage({ message }) {

const FlagList = () => {
const classes = useStyles();
const [search, setSearch] = useState();
const [search, setSearch] = useState(sessionStorage.getItem(searchKey) || '');
const [page, setPage] = useState(0);
const [rowsPerPage, setRowsPerPage] = useState(Number(localStorage.getItem(rowsPerPageKey) || 25));
const { loading, error, data } = useQuery(FLAGS_QUERY, {
Expand Down Expand Up @@ -68,10 +69,14 @@ const FlagList = () => {

return (
<div className={classes.root}>
<FlagsToolbar onSearch={v => {
setSearch(v);
setPage(0)
}}/>
<FlagsToolbar
search={search}
onSearch={v => {
setSearch(v);
sessionStorage.setItem(searchKey, v);
setPage(0);
}}
/>
<div className={classes.content}>
{content}
</div>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ const useStyles = makeStyles(theme => ({
}));

const FlagsToolbar = props => {
const { className, onSearch, ...rest } = props;
const { className, search, onSearch, ...rest } = props;

const classes = useStyles();

Expand All @@ -34,6 +34,7 @@ const FlagsToolbar = props => {
<div className={classes.row}>
<SearchInput
className={classes.searchInput}
search={search}
onChange={onSearch}
placeholder="Search flag"
/>
Expand All @@ -45,6 +46,7 @@ const FlagsToolbar = props => {

FlagsToolbar.propTypes = {
className: PropTypes.string,
search: PropTypes.string,
onSearch: PropTypes.func.isRequired,
};

Expand Down