Skip to content

Commit

Permalink
Fixes Seneca-CDOT#1983: Improve SearchHelp logic
Browse files Browse the repository at this point in the history
  • Loading branch information
rjayroso committed Apr 17, 2021
1 parent cc88fe7 commit 708e8d3
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 6 deletions.
3 changes: 1 addition & 2 deletions src/web/src/components/SearchPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,13 @@ const useStyles = makeStyles((theme: Theme) => ({

const SearchPage = () => {
const classes = useStyles();

const { showHelp } = useSearchValue();

return (
<div className={classes.searchPage}>
<SearchBar />
<SearchResults />
{showHelp && <SearchHelp />}
<SearchResults />
</div>
);
};
Expand Down
20 changes: 17 additions & 3 deletions src/web/src/components/SearchProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ export interface SearchContextInterface {
textParam: string;
filter: FilterProp['filter'];
showHelp: boolean;
toggleHelp: (value: boolean) => void;
onTextChange: (value: string) => void;
onFilterChange: (value: FilterProp['filter']) => void;
onSubmitHandler: (value: FormEvent) => void;
Expand All @@ -20,6 +21,9 @@ const SearchContext = createContext<SearchContextInterface>({
textParam: '',
filter: 'post',
showHelp: true,
toggleHelp() {
throw new Error('This context must be wrapped inside SearchProvider');
},
onTextChange() {
throw new Error('This context must be wrapped inside SearchProvider');
},
Expand Down Expand Up @@ -54,9 +58,10 @@ const SearchProvider = ({ children }: Props) => {
const onSubmitHandler = (event: FormEvent) => {
event.preventDefault();
router.push(`/search?text=${text}&filter=${filter}`);
};

// On form submit, hide help list
setShowHelp(false);
const toggleHelp = (value: boolean) => {
setShowHelp(value);
};

const onTextChange = (value: string) => {
Expand All @@ -74,7 +79,16 @@ const SearchProvider = ({ children }: Props) => {

return (
<SearchContext.Provider
value={{ text, textParam, showHelp, filter, onTextChange, onFilterChange, onSubmitHandler }}
value={{
text,
textParam,
showHelp,
filter,
onTextChange,
onFilterChange,
onSubmitHandler,
toggleHelp,
}}
>
{children}
</SearchContext.Provider>
Expand Down
9 changes: 8 additions & 1 deletion src/web/src/components/SearchResults.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ const useStyles = makeStyles(() => ({

const SearchResults = () => {
const classes = useStyles();
const { textParam, filter } = useSearchValue();
const { textParam, filter, toggleHelp } = useSearchValue();

const prepareUrl = (index: number) =>
`${searchServiceUrl}?text=${encodeURIComponent(textParam)}&filter=${filter}&page=${index}`;
Expand All @@ -75,6 +75,13 @@ const SearchResults = () => {
const loadingMoreData =
!isReachingEnd && data && size > 0 && typeof data[size - 1] === 'undefined';

// If there is no posts or if the search bar is empty, then show the search help, otherwise hide it
if (!error && (isEmpty || textParam.length == 0)) {
toggleHelp(true);
} else {
toggleHelp(false);
}

if (error) {
return (
<Container className={classes.searchResults}>
Expand Down

0 comments on commit 708e8d3

Please sign in to comment.