diff --git a/src/components/LoadingWrapper.tsx b/src/components/LoadingWrapper.tsx new file mode 100644 index 0000000..3aefbcb --- /dev/null +++ b/src/components/LoadingWrapper.tsx @@ -0,0 +1,34 @@ +import { type UseTRPCQueryResult } from "@trpc/react-query/shared"; +import { LoadingBarChart } from "./LoadingBarChart"; +import { ErrorNotice } from "./notice"; + +/** + * Generic query wrapper which handles the states of a query + * (loading, error, etc.). Will hydrate child with query.data + * + */ +export function QueryWaitWrapper({ + query, + Render, +}: { + query: UseTRPCQueryResult; + Render: (data: TData) => JSX.Element; +}) { + if (query.isLoading) { + return ; + } + + if (!query.data || query.isError) { + return ( +
+ +
+ ); + } + + return ; +} diff --git a/src/pages/resources/index.tsx b/src/pages/resources/index.tsx index 31c6863..87a1f4c 100644 --- a/src/pages/resources/index.tsx +++ b/src/pages/resources/index.tsx @@ -5,9 +5,9 @@ import { useRouter } from "next/router"; import ResourceTable from "~/components/ResourceTable"; import { api } from "~/utils/api"; import { parseQueryData } from "~/utils/parseSearchForm"; -import { LoadingBarChart } from "~/components/LoadingBarChart"; -import { ErrorNotice } from "~/components/notice"; import { HeaderFooterLayout } from "~/layouts/HeaderFooterLayout"; +import { type AuditoryResource } from "@prisma/client"; +import { QueryWaitWrapper } from "~/components/LoadingWrapper"; const Resources = () => { const router = useRouter(); @@ -30,29 +30,16 @@ const Resources = () => { : ""; const printLink = `${router.route}/print?${printQueryStr}`; - const ConditionalTable = () => { - if (resourceQuery.isLoading) { - return ; - } - - if (!resourceQuery.data || resourceQuery.isError) { - return ( -
- -
- ); - } - - const totalPages = Math.ceil(resourceQuery.data.count / queryData.perPage); + const ConditionalTable = (data: { + count: number; + resources: AuditoryResource[]; + }) => { + const totalPages = Math.ceil(data.count / queryData.perPage); return ( { - + + );