Skip to content

Commit

Permalink
remove popular filter
Browse files Browse the repository at this point in the history
  • Loading branch information
berekuk committed Mar 6, 2024
1 parent 490f9c4 commit d951b1e
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 82 deletions.
116 changes: 45 additions & 71 deletions src/components/spaces/SpacesIndex/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,95 +9,67 @@ import { DropDown } from "~/components/utility/DropDown";
import { Input } from "~/components/utility/forms";
import { useAppDispatch, useAppSelector } from "~/modules/hooks";
import * as search from "~/modules/search_spaces/actions";
import { SearchSortBy, SearchTimeframe } from "~/modules/search_spaces/actions";
import { SearchSortBy } from "~/modules/search_spaces/actions";

const sortNames: { [k in SearchSortBy]: string } = {
RECOMMENDED: "Recommended",
RECENT: "Recent",
POPULAR: "Popular",
};

const timeframeNames: { [k in SearchTimeframe]: string } = {
MONTHLY: "Monthly",
ALL_TIME: "All Time",
};

const Filters: FC<{
sortBy: SearchSortBy;
timeframe: SearchTimeframe;
onChangeSortBy(sortBy: SearchSortBy): void;
onChangeTimeFrame(timeframe: SearchTimeframe): void;
}> = ({ sortBy, timeframe, onChangeSortBy, onChangeTimeFrame }) => {
// Previously we had more than just filter, and we might have more again, so
// this code is more generic than necessary.
function Filter<T extends string>({
selected,
names,
onChange,
}: {
selected: T;
names: { [k in T]: string };
onChange(value: T): void;
}) {
return (
<div className="flex gap-4">
<Filter selected={sortBy} names={sortNames} onChange={onChangeSortBy} />
{sortBy === "POPULAR" && (
<Filter
selected={timeframe}
names={timeframeNames}
onChange={onChangeTimeFrame}
/>
)}
</div>
<DropDown
openLink={
<div className="flex items-center gap-1 rounded-sm px-2 py-1 text-grey-666 hover:bg-[#e3e8ec]">
<div>{names[selected]}</div>
<Icon name="chevron-down" className="text-sm" />
</div>
}
position="right"
>
{Object.keys(names).map((key: T) => {
return (
<CardListElement
key={key}
header={names[key]}
isSelected={selected === key}
onClick={() => onChange(key)}
closeOnClick={true}
/>
);
})}
</DropDown>
);
};

const Filter: FC<{
// TODO - generic over SearchSortBy/SearchTimeframe?
selected: string;
names: { [k: string]: string };
onChange(value: string): void;
}> = ({ selected, names, onChange }) => (
<DropDown
openLink={
<div className="flex items-center gap-1 rounded-sm px-2 py-1 text-grey-666 hover:bg-[#e3e8ec]">
<div>{names[selected]}</div>
<Icon name="chevron-down" className="text-sm" />
</div>
}
position="right"
>
{Object.keys(names).map((key) => {
return (
<CardListElement
key={key}
header={names[key]}
isSelected={selected === key}
onClick={() => {
onChange(key);
}}
closeOnClick={true}
/>
);
})}
</DropDown>
);
}

export const SpacesIndex: FC = () => {
const dispatch = useAppDispatch();
const searchSpaces = useAppSelector((state) => state.searchSpaces);

const [sortBy, setSortBy] = useState<SearchSortBy>("RECOMMENDED");
const [timeframe, setTimeframe] = useState<SearchTimeframe>("ALL_TIME");
const [searchValue, setSearchValue] = useState("");

const inputRef = useRef<HTMLInputElement>(null);
useEffect(() => {
inputRef.current?.focus();
}, [inputRef.current]);
}, []);

const loadNextPage = () => {
dispatch(search.fetchNextPage());
};

const changeSearchValue = (e: React.ChangeEvent<HTMLInputElement>) => {
setSearchValue(e.target.value);
setTimeframe("ALL_TIME");
};

useEffect(() => {
dispatch(search.fetch(searchValue, { sortBy, timeframe }));
}, [searchValue, sortBy, timeframe]);
dispatch(search.fetch(searchValue, { sortBy }));
}, [searchValue, sortBy]);

const spaces = searchSpaces.hits || [];
const hasMorePages =
Expand All @@ -120,12 +92,14 @@ export const SpacesIndex: FC = () => {
onChange={changeSearchValue}
/>
</div>
<div>
<Filters
sortBy={sortBy}
timeframe={timeframe}
onChangeSortBy={setSortBy}
onChangeTimeFrame={setTimeframe}
<div className="flex">
<Filter<SearchSortBy>
selected={sortBy}
onChange={setSortBy}
names={{
RECOMMENDED: "Recommended",
RECENT: "Recent",
}}
/>
</div>
</div>
Expand Down
14 changes: 3 additions & 11 deletions src/modules/search_spaces/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,23 +7,15 @@ import { searchSpaceIndex } from "~/server/algolia/index";

import { SearchFilters } from "./reducer";

export type SearchTimeframe = "ALL_TIME" | "MONTHLY";
export type SearchSortBy = "POPULAR" | "RECENT" | "RECOMMENDED";
export type SearchSortBy = "RECENT" | "RECOMMENDED";

export function fetch(
query = "",
options: { sortBy: SearchSortBy; timeframe: SearchTimeframe; page?: number }
options: { sortBy: SearchSortBy; page?: number }
): AppThunk {
const filters: SearchFilters = { hitsPerPage: 21, page: options.page || 0 };
const { sortBy } = options;

const secondsAtNow = new Date().getTime() / 1000;
const secondsInMonth = 60 * 60 * 24 * 30;

if (sortBy === "POPULAR" && _.get(options, "timeframe") === "MONTHLY") {
filters.numericFilters = `created_at_i>${secondsAtNow - secondsInMonth}`;
}

if (sortBy === "RECOMMENDED") {
filters.facetFilters = ["is_recommended: true"];
}
Expand Down Expand Up @@ -54,7 +46,7 @@ export function fetchNextPage(): AppThunk {
try {
const results = await searchSpaceIndex().search(searchSpaces.query, {
...searchSpaces.filters,
page: searchSpaces.page + 1,
page: (searchSpaces.page ?? 0) + 1,
});
dispatch({ type: "SEARCH_SPACES_NEXT_PAGE", response: results });
dispatch(spaceActions.fromSearch(results.hits));
Expand Down

0 comments on commit d951b1e

Please sign in to comment.