chore: teams filter improvements (#14315)

* Changed translations, many languages were wrong

* Clear localStorage from the useTypedQuery

* Missed one
This commit is contained in:
Alex van Andel authored and GitHub committed 2024-04-04 09:59:58 -07:00
1 parent 9bd3a362a2
commit 1132d9d0dc
12 files changed
+15 -56

No files matched your search

@@ -15,10 +15,9 @@ import { filterQuerySchema } from "../lib/getTeamsFiltersFromQuery";
export type IEventTypesFilters = RouterOutputs["viewer"]["eventTypes"]["listWithTeam"];
export type IEventTypeFilter = IEventTypesFilters[0];
export const TEAMS_FILTER_KEY = "TEAMS_FILTER_KEY";
function useFilterQuery() {
// passthrough allows additional params to not be removed
return useTypedQuery(filterQuerySchema.passthrough(), TEAMS_FILTER_KEY);
return useTypedQuery(filterQuerySchema.passthrough());
}
export const TeamsFilter = ({
@@ -32,11 +31,14 @@ export const TeamsFilter = ({
}) => {
const { t } = useLocale();
const session = useSession();
const { data: query, pushItemToKey, removeItemByKeyAndValue, removeAllQueryParams } = useFilterQuery();
const { data: teams } = trpc.viewer.teams.list.useQuery(undefined, {
// Teams don't change that frequently
refetchOnWindowFocus: false,
});
const getCheckedOptionsNames = () => {
const checkedOptions: string[] = [];
const teamIds = query.teamIds;
+1 -44
View File
@@ -4,7 +4,6 @@ import { usePathname, useRouter } from "next/navigation";
import { useCallback, useMemo, useEffect } from "react";
import { z } from "zod";
import { localStorage } from "../webstorage";
import { useRouterQuery } from "./useRouterQuery";
type OptionalKeys<T> = {
@@ -33,7 +32,7 @@ export const queryStringArray = z
.preprocess((a) => z.string().parse(a).split(","), z.string().array())
.or(z.string().array());
export function useTypedQuery<T extends z.AnyZodObject>(schema: T, localStorageKey: string | null = null) {
export function useTypedQuery<T extends z.AnyZodObject>(schema: T) {
type Output = z.infer<typeof schema>;
type FullOutput = Required<Output>;
type OutputKeys = Required<keyof FullOutput>;
@@ -44,7 +43,6 @@ export function useTypedQuery<T extends z.AnyZodObject>(schema: T, localStorageK
const unparsedQuery = useRouterQuery();
const pathname = usePathname();
const parsedQuerySchema = schema.safeParse(unparsedQuery);
const useLocalStorage = !!localStorageKey;
let parsedQuery: Output = useMemo(() => {
return {} as Output;
}, []);
@@ -74,39 +72,6 @@ export function useTypedQuery<T extends z.AnyZodObject>(schema: T, localStorageK
// eslint-disable-next-line react-hooks/exhaustive-deps
[parsedQuery, router]
);
//If the value for {localStorageKey} exist in localStorage set it to queryParams
useEffect(() => {
if (!useLocalStorage) return;
const storedValueString = localStorage.getItem(localStorageKey);
if (!storedValueString || storedValueString == "{}") return;
const storedValue: Output = JSON.parse(storedValueString);
if (storedValue) {
const searchParams = new URLSearchParams();
for (const [key, value] of Object.entries(storedValue)) {
searchParams.set(String(key), String(value));
}
router.replace(`${pathname}?${searchParams.toString()}`);
}
}, []);
function setLocalStorage<J extends OutputKeys>(key: J, value: Output[J]) {
if (!useLocalStorage) return;
const storedValue: Output = JSON.parse(localStorage.getItem(localStorageKey) || "{}");
const newValue = { ...storedValue, [key]: value };
localStorage.setItem(localStorageKey, JSON.stringify(newValue));
}
function removeByKeyFromLocalStorage(key: OutputOptionalKeys) {
if (!useLocalStorage) return;
const storedValue: Output = JSON.parse(localStorage.getItem(localStorageKey) || "{}");
const newValue = storedValue;
delete newValue[key];
localStorage.setItem(localStorageKey, JSON.stringify(newValue));
}
//Remove {localStorageKey} from the localStorage
function removeAllValuesFromLocalStorage() {
if (!useLocalStorage) return;
localStorage.removeItem(localStorageKey);
}
// Delete a key from the query
function removeByKey(key: OutputOptionalKeys) {
@@ -122,13 +87,9 @@ export function useTypedQuery<T extends z.AnyZodObject>(schema: T, localStorageK
if (existingValue.includes(value)) return; // prevent adding the same value to the array
// @ts-expect-error this is too much for TS it seems
setQuery(key, [...existingValue, value]);
// @ts-expect-error this is too much for TS it seems
setLocalStorage(key, [...existingValue, value]);
} else {
// @ts-expect-error this is too much for TS it seems
setQuery(key, [value]);
// @ts-expect-error this is too much for TS it seems
setLocalStorage(key, [value]);
}
}
@@ -139,12 +100,9 @@ export function useTypedQuery<T extends z.AnyZodObject>(schema: T, localStorageK
// @ts-expect-error this is too much for TS it seems
const newValue = existingValue.filter((item) => item !== value);
setQuery(key, newValue);
setLocalStorage(key, newValue);
} else {
// @ts-expect-error this is too much for TS it seems
removeByKey(key);
//@ts-expect-error this is too much for TS it seems
removeByKeyFromLocalStorage(key);
}
}
@@ -152,7 +110,6 @@ export function useTypedQuery<T extends z.AnyZodObject>(schema: T, localStorageK
function removeAllQueryParams() {
if (pathname !== null) {
router.replace(pathname);
removeAllValuesFromLocalStorage();
}
}