Skip to content

Commit

Permalink
Defer searchOptions
Browse files Browse the repository at this point in the history
  • Loading branch information
taneliang committed Dec 19, 2020
1 parent 80102e5 commit 4423a00
Showing 1 changed file with 31 additions and 17 deletions.
48 changes: 31 additions & 17 deletions website/src/views/venues/VenuesContainer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ export const VenuesContainerComponent: FC<Props> = ({ venues }) => {
) as VenueSearchOptions)
: defaultSearchOptions();
});
const [deferredSearchOptions, setDeferredSearchOptions] = useState(searchOptions);
const [pristineSearchOptions, setPristineSearchOptions] = useState(() => !isAvailabilityEnabled);

// TODO: Check if this actually does anything useful
Expand All @@ -90,24 +91,33 @@ export const VenuesContainerComponent: FC<Props> = ({ venues }) => {

const onFindFreeRoomsClicked = useCallback(() => {
const newIsAvailabilityEnabled = !isAvailabilityEnabled;

// Only reset search options if the user has never changed it, and if the
// search box is being opened. By resetting the option when the box is
// opened, the time when the box is opened will be used, instead of the time
// when the page is loaded.
const shouldResetSearchOptions = pristineSearchOptions && newIsAvailabilityEnabled;

setIsAvailabilityEnabled(newIsAvailabilityEnabled);
if (shouldResetSearchOptions) {
setSearchOptions(defaultSearchOptions());
}

startTransition(() => {
setDeferredIsAvailabilityEnabled(newIsAvailabilityEnabled);
if (pristineSearchOptions && newIsAvailabilityEnabled) {
// Only reset search options if the user has never changed it, and if the
// search box is being opened. By resetting the option when the box is opened,
// the time when the box is opened will be used, instead of the time when the
// page is loaded
setSearchOptions(defaultSearchOptions());
if (shouldResetSearchOptions) {
setDeferredSearchOptions(defaultSearchOptions());
}
});
}, [isAvailabilityEnabled, pristineSearchOptions]);

const onAvailabilityUpdate = useCallback(
(newSearchOptions: VenueSearchOptions) => {
if (!isEqual(newSearchOptions, searchOptions)) {
setSearchOptions(clampClassDuration(newSearchOptions));
const clampedSearchOptions = clampClassDuration(newSearchOptions);
setSearchOptions(clampedSearchOptions);
setPristineSearchOptions(false); // user changed searchOptions
startTransition(() => setDeferredSearchOptions(clampedSearchOptions));
}
},
[searchOptions],
Expand All @@ -117,15 +127,17 @@ export const VenuesContainerComponent: FC<Props> = ({ venues }) => {
if (!deferredIsAvailabilityEnabled) return undefined;

return {
day: searchOptions.day,
startTime: convertIndexToTime(searchOptions.time * 2),
endTime: convertIndexToTime(2 * (searchOptions.time + searchOptions.duration)),
day: deferredSearchOptions.day,
startTime: convertIndexToTime(deferredSearchOptions.time * 2),
endTime: convertIndexToTime(
2 * (deferredSearchOptions.time + deferredSearchOptions.duration),
),
};
}, [
deferredIsAvailabilityEnabled,
searchOptions.day,
searchOptions.duration,
searchOptions.time,
deferredSearchOptions.day,
deferredSearchOptions.duration,
deferredSearchOptions.time,
]);

const selectedVenue = useMemo<Venue | undefined>(
Expand All @@ -137,7 +149,7 @@ export const VenuesContainerComponent: FC<Props> = ({ venues }) => {
useEffect(() => {
let query: Partial<Params> = {};
if (deferredSearchQuery) query.q = deferredSearchQuery;
if (deferredIsAvailabilityEnabled) query = { ...query, ...searchOptions };
if (deferredIsAvailabilityEnabled) query = { ...query, ...deferredSearchOptions };
const search = qs.stringify(query);

const pathname = venuePage(selectedVenue);
Expand All @@ -156,16 +168,18 @@ export const VenuesContainerComponent: FC<Props> = ({ venues }) => {
}, [
debouncedHistory,
deferredIsAvailabilityEnabled,
deferredSearchOptions,
deferredSearchQuery,
history,
searchOptions,
selectedVenue,
]);

const matchedVenues = useMemo(() => {
const matched = searchVenue(venues, deferredSearchQuery);
return deferredIsAvailabilityEnabled ? filterAvailability(matched, searchOptions) : matched;
}, [deferredIsAvailabilityEnabled, searchOptions, deferredSearchQuery, venues]);
return deferredIsAvailabilityEnabled
? filterAvailability(matched, deferredSearchOptions)
: matched;
}, [deferredIsAvailabilityEnabled, deferredSearchOptions, deferredSearchQuery, venues]);
const matchedVenueNames = useMemo(() => matchedVenues.map(([venue]) => venue), [matchedVenues]);

function renderSearch() {
Expand Down

0 comments on commit 4423a00

Please sign in to comment.