Skip to content

Commit

Permalink
chore: fix build step
Browse files Browse the repository at this point in the history
  • Loading branch information
PupoSDC committed Jan 17, 2024
1 parent d2b1e24 commit eb08aa7
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 23 deletions.
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
import * as fs from "node:fs/promises";
import { getTrpcHelper } from "libs/trpc/server/src/next/trpc-helper";
import { AppHead } from "@chair-flight/react/components";
import { FlashcardList, LayoutModule } from "@chair-flight/react/containers";
import { staticHandler } from "@chair-flight/trpc/server";
import { staticHandler, staticPathsHandler } from "@chair-flight/trpc/server";
import type { QuestionBankName } from "@chair-flight/base/types";
import type { Breadcrumbs } from "@chair-flight/react/containers";
import type { GetStaticPaths, NextPage } from "next";
import type { NextPage } from "next";

type PageProps = {
questionBank: QuestionBankName;
Expand Down Expand Up @@ -41,23 +40,26 @@ export const getStaticProps = staticHandler<PageProps, PageParams>(
fs,
);

export const getStaticPaths: GetStaticPaths<PageParams> = async () => {
const helper = await getTrpcHelper();
const qb = helper.questionBank;
const banks: QuestionBankName[] = ["prep"];
const paths = await Promise.all(
banks.map(async (questionBank) => {
const params = { questionBank };
const data = await qb.getFlashcardsCollections.fetch(params);
return data.collections.map(({ id: collectionId }) => ({
params: {
questionBank,
collectionId,
},
}));
}),
).then((c) => c.flat());
return { fallback: false, paths };
};
export const getStaticPaths = staticPathsHandler<PageParams>(
async ({ helper }) => {
const qb = helper.questionBank;
const banks: QuestionBankName[] = ["prep"];
const paths = await Promise.all(
banks.map(async (questionBank) => {
const params = { questionBank };
const data = await qb.getFlashcardsCollections.fetch(params);
return data.collections.map(({ id: collectionId }) => ({
params: {
questionBank,
collectionId,
},
}));
}),
).then((c) => c.flat());

return { fallback: false, paths };
},
fs,
);

export default Page;
1 change: 0 additions & 1 deletion libs/core/question-bank/src/question-bank.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,6 @@ export class QuestionBank implements IQuestionBank {
const bankPath = `/content/content-question-bank-${this.getName()}`;
const baseApiPath = `${urlPath}${bankPath}`;
const apiPath = `${baseApiPath}/${resource}.json`;
console.log("apiPath", apiPath);
const response = await fetch(apiPath);
const json = (await response.json()) as NameToType[T][];
type ArrayType = (typeof this.resourceArrays)[typeof resource];
Expand Down
2 changes: 1 addition & 1 deletion libs/trpc/server/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
export { edgeApiHandler, apiHandler } from "./next/api-handler";
export { ssrHandler } from "./next/ssr-handler";
export { staticHandler } from "./next/static-handler";
export { staticHandler, staticPathsHandler } from "./next/static-handler";
export type { TrpcHelper } from "./next/trpc-helper";
export type { AppRouter } from "./config/app-router";
24 changes: 24 additions & 0 deletions libs/trpc/server/src/next/static-handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ import { questionBanks } from "@chair-flight/core/question-bank";
import { getTrpcHelper } from "./trpc-helper";
import type { TrpcHelper } from "./trpc-helper";
import type {
GetStaticPaths,
GetStaticPathsContext,
GetStaticPathsResult,
GetStaticProps,
GetStaticPropsContext,
GetStaticPropsResult,
Expand Down Expand Up @@ -50,3 +53,24 @@ export const staticHandler = <
return response;
};
};

export const staticPathsHandler = <
Params extends ParsedUrlQuery = ParsedUrlQuery,
>(
handler: ({
context,
helper,
}: {
context: GetStaticPathsContext;
helper: TrpcHelper;
}) => Promise<GetStaticPathsResult<Params>>,
fs: FS,
): GetStaticPaths<Params> => {
return async (context) => {
await Promise.all(
Object.values(questionBanks).map((qb) => qb.preloadForStaticRender(fs)),
);
const helper = await getTrpcHelper();
return await handler({ context, helper });
};
};

0 comments on commit eb08aa7

Please sign in to comment.