save, create and view reports in dashboard

This commit is contained in:
Carl-Gerhard Lindesvärd
2023-10-19 11:56:52 +02:00
parent 2cb6bbfdd3
commit 4576453aef
28 changed files with 686 additions and 403 deletions

View File

@@ -0,0 +1,35 @@
import { useMemo } from "react";
import { useRouter } from "next/router";
import { type z } from "zod";
export function useQueryParams<Z extends z.ZodTypeAny = z.ZodNever>(zod: Z) {
const router = useRouter();
const value = zod.safeParse(router.query);
return useMemo(() => {
function setQueryParams(newValue: Partial<z.infer<Z>>) {
return router
.replace({
pathname: router.pathname,
query: {
...router.query,
...newValue,
},
})
.catch(() => {
// ignore
});
}
if (value.success) {
return { ...value.data, setQueryParams } as z.infer<Z> & {
setQueryParams: typeof setQueryParams;
};
}
return { ...router.query, setQueryParams } as z.infer<Z> & {
setQueryParams: typeof setQueryParams;
};
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [router.asPath, value.success]);
}

View File

@@ -0,0 +1,21 @@
import { useRouter } from "next/router";
import { useEffect, useRef } from "react";
export function useRouterBeforeLeave(callback: () => void) {
const router = useRouter();
const prevUrl = useRef(router.asPath);
useEffect(() => {
const handleRouteChange = (url: string) => {
if (prevUrl.current !== url) {
callback()
}
prevUrl.current = url;
};
router.events.on("routeChangeStart", handleRouteChange);
return () => {
router.events.off("routeChangeStart", handleRouteChange);
};
}, [router, callback]);
}