Skip to content

Commit

Permalink
feat: Limits custom searches to 20 and adds show more button.
Browse files Browse the repository at this point in the history
  • Loading branch information
neomorphic committed Jun 5, 2024
1 parent 40e4398 commit 18ec8b9
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 10 deletions.
13 changes: 10 additions & 3 deletions src/components/CustomSearch/SearchList.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ ErrorMessage.defaultProps = {
error: null
};

export default function SearchList({ searches }) {
export default function SearchList({ searches, showMore, showMoreHandler }) {
const { appState } = useContext(AppContext);

const searchesInProgress = searches
Expand Down Expand Up @@ -97,9 +97,16 @@ export default function SearchList({ searches }) {
);
}

return <div>{searchesInProgress}</div>;
return(
<div>
{searchesInProgress}
{showMore ? <Button onClick={() => showMoreHandler()}>Show more</Button> : ""}
</div>
);
}

SearchList.propTypes = {
searches: PropTypes.arrayOf(PropTypes.object).isRequired
searches: PropTypes.arrayOf(PropTypes.object).isRequired,
showMore: PropTypes.bool.isRequired,
showMoreHandler: PropTypes.func.isRequired,
};
25 changes: 20 additions & 5 deletions src/components/CustomSearchList.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ const { Title } = Typography;
export default function CustomSearchList() {
const [uploadedFile, setUploadedFile] = useState(null);
const [isLoading, setLoading] = useState(true);
const [nextToken, setNextToken] = useState(null);
const [searches, dispatch] = useReducer((searchList, { type, value }) => {
switch (type) {
case "init":
Expand All @@ -39,11 +40,12 @@ export default function CustomSearchList() {
async function fetchSearches() {
const creds = await Auth.currentCredentials();

const items = await fetchItemsNextToken({
const [items, token] = await fetchItemsNextToken({
query: queries.listItemsByOwner,
variables: { limit: 30, identityId: creds.identityId, sortDirection: "DESC"},
limit: 100
variables: { limit: 20, identityId: creds.identityId, sortDirection: "DESC"},
limit: 20
});
setNextToken(token);
items.forEach(search => logSearchInfo(search));
dispatch({ type: "init", value: items });
setLoading(false);
Expand Down Expand Up @@ -145,6 +147,19 @@ export default function CustomSearchList() {
});
}, []);

const showMoreHandler = async () => {
const creds = await Auth.currentCredentials();

const [items, token] = await fetchItemsNextToken({
query: queries.listItemsByOwner,
variables: { limit: 20, identityId: creds.identityId, sortDirection: "DESC", nextToken},
limit: 20
});
setNextToken(token);
items.forEach(search => dispatch({ type: "add", value: search}));
setLoading(false);
}

return (
<div>
<ScrollToTopOnMount />
Expand All @@ -153,8 +168,8 @@ export default function CustomSearchList() {
handleUpload={setUploadedFile}
/>
<Divider dashed />
<Title level={3}>Your Searches ({searches.length})</Title>
{isLoading ? <Spin size="large" /> : <SearchList searches={searches} />}
<Title level={3}>Your Searches ({searches.length}{nextToken ? '+' : null})</Title>
{isLoading ? <Spin size="large" /> : <SearchList searches={searches} showMore={Boolean(nextToken)} showMoreHandler={showMoreHandler}/>}
</div>
);
}
4 changes: 2 additions & 2 deletions src/libs/awsLib.js
Original file line number Diff line number Diff line change
Expand Up @@ -146,10 +146,10 @@ export async function fetchItemsNextToken({
// if there are enough items to fulfill the limit, then trim the items
// to the limit and return.
if (items.length >= limit) {
return items;
return [items, res.nextToken];
}

if (!res.nextToken) return items;
if (!res.nextToken) return [items, null];

// eslint-disable-next-line no-param-reassign
variables.nextToken = res.nextToken;
Expand Down

0 comments on commit 18ec8b9

Please sign in to comment.