[questions][fix] fix index refreshing constantly

pull/346/head
Jeff Sieu 3 years ago committed by wlren
parent 28f9c7da6c
commit 9a6682d3c1

@ -40,7 +40,7 @@ export default function QuestionsHomePage() {
useSearchFilter('locations'); useSearchFilter('locations');
const [hasLanded, setHasLanded] = useState(false); const [hasLanded, setHasLanded] = useState(false);
// Const [loaded, setLoaded] = useState(false); const [loaded, setLoaded] = useState(false);
const companyFilterOptions = useMemo(() => { const companyFilterOptions = useMemo(() => {
return COMPANIES.map((company) => ({ return COMPANIES.map((company) => ({
@ -102,44 +102,14 @@ export default function QuestionsHomePage() {
if (hasFilter) { if (hasFilter) {
setHasLanded(true); setHasLanded(true);
} }
// Console.log('shit'); // Console.log('landed', hasLanded);
// setLoaded(true); setLoaded(true);
} }
}, [areFiltersInitialized, router.query]); }, [areFiltersInitialized, hasLanded, router.query]);
// Update url query params if (!loaded) {
useEffect(() => { return null;
if (router.isReady && areFiltersInitialized) { }
router.replace(
{
query: {
companies: selectedCompanies,
locations: selectedLocations,
questionTypes: selectedQuestionTypes,
...(selectedQuestionAge !== 'all'
? {
questionAge: selectedQuestionAge,
}
: {}),
},
},
undefined,
{ shallow: true },
);
}
}, [
selectedCompanies,
selectedQuestionTypes,
selectedQuestionAge,
selectedLocations,
router,
router.isReady,
areFiltersInitialized,
]);
// If (!loaded) {
// return <div>hi</div>;
// }
return !hasLanded ? ( return !hasLanded ? (
<LandingComponent onLanded={handleLandingQuery}></LandingComponent> <LandingComponent onLanded={handleLandingQuery}></LandingComponent>

@ -21,22 +21,37 @@ export const useSearchFilter = (
// Try to load from local storage // Try to load from local storage
const localStorageValue = localStorage.getItem(name); const localStorageValue = localStorage.getItem(name);
if (localStorageValue !== null) { if (localStorageValue !== null) {
setFilters(JSON.parse(localStorageValue)); const loadedFilters = JSON.parse(localStorageValue);
setFilters(loadedFilters);
router.replace({
pathname: router.pathname,
query: {
...router.query,
[name]: loadedFilters,
},
});
} }
} }
setIsInitialized(true); setIsInitialized(true);
} }
}, [router.isReady, isInitialized, router.query, name]); }, [isInitialized, name, router]);
const setFiltersAndSaveToLocalStorage = useCallback( const setFiltersCallback = useCallback(
(newFilters: Array<string>) => { (newFilters: Array<string>) => {
setFilters(newFilters); setFilters(newFilters);
localStorage.setItem(name, JSON.stringify(newFilters)); localStorage.setItem(name, JSON.stringify(newFilters));
router.replace({
pathname: router.pathname,
query: {
...router.query,
[name]: newFilters,
},
});
}, },
[name], [name, router],
); );
return [filters, setFiltersAndSaveToLocalStorage, isInitialized] as const; return [filters, setFiltersCallback, isInitialized] as const;
}; };
export const useSearchFilterSingle = (name: string, defaultValue: string) => { export const useSearchFilterSingle = (name: string, defaultValue: string) => {

Loading…
Cancel
Save