Skip to content

Commit

Permalink
load data from json in outcomes and tables
Browse files Browse the repository at this point in the history
  • Loading branch information
ncukondo committed Nov 29, 2023
1 parent 98dcf2a commit 9751e8f
Show file tree
Hide file tree
Showing 18 changed files with 165 additions and 487 deletions.
2 changes: 1 addition & 1 deletion src/components/Outcomes.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@ import { applyMappedInfo } from "@libs/textMapper";
import type { MappedInfo } from "@libs/textMapper";
import type { Tree } from "@libs/treeUtils";
import { reduceTree } from "@libs/treeUtils";
import { AttrInfo } from "@services/attrInfo";
import type {
OutcomeInfo,
Outcomel1 as L1,
Outcomel2 as L2,
Outcomel3 as L3,
Outcomel4 as L4,
} from "@services/outcomes";
import { AttrInfo } from "@services/replaceMap";

type PropType<T extends OutcomeInfo> = {
item: T;
Expand Down
2 changes: 1 addition & 1 deletion src/components/StyledText.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import Link from "next/link";
import { applyMappedInfo, MappedInfo } from "@libs/textMapper";
import { AttrInfo } from "@services/replaceMap";
import { AttrInfo } from "@services/attrInfo";

const StyledText = ({ text, map }: { text: string; map: MappedInfo<AttrInfo>[] }) => {
if (map.length === 0) return <>{text}</>;
Expand Down
1 change: 1 addition & 0 deletions src/libs/textMapper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ type MappedText<T> = {
};
type TextMapper<T> = (res: RegExpExecArray) => MapperResult<T>;
type ReplaceMap<T> = { reg: RegExp; mapper: TextMapper<T> }[];

const mapText = <T>(text: string, replaceMap: ReplaceMap<T>): MappedText<T> => {
const seek: (str: string) => (string | MapperResultWithPos<T> | null)[] = (str: string) => {
const res = replaceMap.reduce(
Expand Down
1 change: 1 addition & 0 deletions src/libs/treeUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@ const searchMap = <T, R>(tree: Tree<T>, searchMapper: TreeSearchMapper<T, R>) =>
};

type TreeMapper<T, R> = (item: T, parents: T[], hasChildren: boolean) => R;

const mapTree = <T, R>(tree: Tree<T>, mapper: TreeMapper<T, R>) => {
const walkTree: (currTree: Tree<T>, currParents?: T[]) => Tree<R> = (
currTree: Tree<T>,
Expand Down
35 changes: 0 additions & 35 deletions src/pages/forms/outcomes.tsx

This file was deleted.

77 changes: 0 additions & 77 deletions src/pages/forms/skills.tsx

This file was deleted.

68 changes: 0 additions & 68 deletions src/pages/forms/symptoms.tsx

This file was deleted.

7 changes: 2 additions & 5 deletions src/pages/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,13 @@ import { OutcomesTree } from "@components/Outcomes";
import { MenuItem, OutcomesTOC } from "@components/OutcomesTOC";
import type { Tree } from "@libs/treeUtils";
import { Locale, useLocaleText } from "@services/i18n/i18n";
import { loadFullOutcomesTable, makeOutcomesTree } from "@services/outcomes";
import { loadOutcomesTree } from "@services/outcomes";
import type { OutcomeInfo } from "@services/outcomes";
import { loadTableInfoDict } from "@services/tables";

type PageProps = { outcomesTree: Tree<OutcomeInfo> };

export const getStaticProps: GetStaticProps<PageProps> = async ({ locale }) => {
const table = loadFullOutcomesTable(locale as Locale);
const tableInfoDict = loadTableInfoDict(locale as Locale);
const outcomesTree = makeOutcomesTree(table, tableInfoDict, locale as Locale);
const outcomesTree = await loadOutcomesTree(locale as Locale);

return {
props: { outcomesTree },
Expand Down
10 changes: 4 additions & 6 deletions src/pages/list.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,10 @@ import {
schemaItemsWithValue,
useClearItemList,
} from "@services/itemList/local";
import { loadFullOutcomesTable, makeOutcomesTree } from "@services/outcomes";
import { loadOutcomesTree } from "@services/outcomes";
import type { OutcomeInfo } from "@services/outcomes";
import { searchOutcomes, searchTables } from "@services/search";
import { getAllTables, loadTableInfoDict, TableInfoSet } from "@services/tables";
import { getAllTables, TableInfoSet } from "@services/tables";
import { itemIdToUrl, objectiveIdToUrl } from "@services/urls";

type PageProps = {
Expand All @@ -35,10 +35,8 @@ type PageProps = {
};

export const getStaticProps: GetStaticProps<PageProps> = async ({ locale }) => {
const table = loadFullOutcomesTable(locale as Locale);
const tableInfoDict = loadTableInfoDict(locale as Locale);
const outcomesTree = makeOutcomesTree(table, tableInfoDict, locale as Locale);
const allTables = getAllTables(locale as Locale);
const outcomesTree = await loadOutcomesTree(locale as Locale);
const allTables = await getAllTables(locale as Locale);

return {
props: { outcomesTree, allTables },
Expand Down
10 changes: 4 additions & 6 deletions src/pages/search.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,19 @@ import { BackButton } from "@components/buttons/BackButton";
import { HeaderedTable } from "@libs/tableUtils";
import { Tree } from "@libs/treeUtils";
import { Locale, useLocaleText } from "@services/i18n/i18n";
import { loadFullOutcomesTable, makeOutcomesTree } from "@services/outcomes";
import { loadOutcomesTree } from "@services/outcomes";
import type { OutcomeInfo } from "@services/outcomes";
import { searchOutcomes, searchTables } from "@services/search";
import { getAllTables, loadTableInfoDict, TableInfoSet } from "@services/tables";
import { getAllTables, TableInfoSet } from "@services/tables";

type PageProps = {
outcomesTree: Tree<OutcomeInfo>;
allTables: TableInfoSet[];
};

export const getStaticProps: GetStaticProps<PageProps> = async ({ locale }) => {
const table = loadFullOutcomesTable(locale as Locale);
const tableInfoDict = loadTableInfoDict(locale as Locale);
const outcomesTree = makeOutcomesTree(table, tableInfoDict, locale as Locale);
const allTables = getAllTables(locale as Locale);
const outcomesTree = await loadOutcomesTree(locale as Locale);
const allTables = await getAllTables(locale as Locale);

return {
props: { outcomesTree, allTables },
Expand Down
18 changes: 10 additions & 8 deletions src/pages/tables/[id].tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,24 @@ import type { NextPage, GetStaticProps, GetStaticPaths } from "next";
import Head from "next/head";
import { Table } from "@components/Table";
import { BackButton } from "@components/buttons/BackButton";
import { dropColumnsByNames } from "@libs/tableUtils";
import { fmt } from "@libs/utils";
import { Locale, Locales, useLocaleText } from "@services/i18n/i18n";
import { getTable, getTableFiles, TableInfoSet } from "@services/tables";
import { getTable, getTableFiles, TableFile, TableInfoSet } from "@services/tables";

type PageProps = TableInfoSet;

type PathParams = {
id: string;
};

export const getStaticPaths: GetStaticPaths<PathParams> = () => {
const paths = (["en", "ja"] as Locales)
.map(locale => getTableFiles(locale as Locale).map(file => ({ params: { id: file }, locale })))
.flat();
export const getStaticPaths: GetStaticPaths<PathParams> = async () => {
const paths = (
await Promise.all(
(["en", "ja"] as Locales).map(async locale =>
(await getTableFiles(locale as Locale)).map(file => ({ params: { id: file }, locale })),
),
)
).flat();
return {
paths,
fallback: false,
Expand All @@ -27,14 +30,13 @@ export const getStaticProps: GetStaticProps<PageProps> = async ({ locale, params
const { id } = params as PathParams;

return {
props: getTable(id, locale as Locale),
props: await getTable(id as TableFile, locale as Locale),
};
};

const TableOfId: NextPage<PageProps> = ({ table, tableInfo, attrInfo }: PageProps) => {
const { t } = useLocaleText("@pages/list/table/[id]");
const name = `${t("table") + tableInfo.number}. ${tableInfo.item}`;
const [header, ...body] = dropColumnsByNames(table, ["id", "index", "H28ID"]);
return (
<>
<Head>
Expand Down
6 changes: 1 addition & 5 deletions src/pages/tables/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,9 @@ import { getTalbleInfoList, TableInfo } from "@services/tables";

type PageProps = { tables: TableInfo[] };

type PathParams = {
id: string;
};

export const getStaticProps: GetStaticProps<PageProps> = async ({ locale }) => {
return {
props: { tables: getTalbleInfoList(locale as Locale) },
props: { tables: await getTalbleInfoList(locale as Locale) },
};
};

Expand Down
10 changes: 4 additions & 6 deletions src/pages/x/[id].tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,10 @@ import {
schemaItemsWithValue,
} from "@services/itemList/local";
import { getItemListFromId, getItemListFromIds } from "@services/itemList/server";
import { loadFullOutcomesTable, makeOutcomesTree } from "@services/outcomes";
import { loadOutcomesTree } from "@services/outcomes";
import type { OutcomeInfo } from "@services/outcomes";
import { searchOutcomes, searchTables } from "@services/search";
import { getAllTables, loadTableInfoDict, TableInfoSet } from "@services/tables";
import { getAllTables, TableInfoSet } from "@services/tables";
import { itemIdToUrl } from "@services/urls";

type PageProps = {
Expand Down Expand Up @@ -65,10 +65,8 @@ const getServerItems = async (ids: readonly string[]) => {
};

export const getStaticProps: GetStaticProps<PageProps> = async ({ locale, params }) => {
const table = loadFullOutcomesTable(locale as Locale);
const tableInfoDict = loadTableInfoDict(locale as Locale);
const outcomesTree = makeOutcomesTree(table, tableInfoDict, locale as Locale);
const allTables = getAllTables(locale as Locale);
const outcomesTree = await loadOutcomesTree(locale as Locale);
const allTables = await getAllTables(locale as Locale);
const id = (params?.id as string) ?? "";
const itemList = await getServerItem(id);
const children =
Expand Down
8 changes: 3 additions & 5 deletions src/services/__test__/curriculumMapTable.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,11 @@ import { parseCSV } from "@libs/csv";
import { makeOutcomeTableData } from "@services/curriculumMapTable";
import { Locale } from "@services/i18n/i18n";
import { ServerItemList } from "@services/itemList/server";
import { loadFullOutcomesTable, makeOutcomesTree } from "@services/outcomes";
import { getAllTables, loadTableInfoDict } from "@services/tables";
import { loadOutcomesTree } from "@services/outcomes";
import { getAllTables } from "@services/tables";

const locale: Locale = "ja";
const table = loadFullOutcomesTable(locale as Locale);
const tableInfoDict = loadTableInfoDict(locale as Locale);
const outcomesTree = makeOutcomesTree(table, tableInfoDict, locale as Locale);
const outcomesTree = await loadOutcomesTree(locale as Locale);
const allTables = getAllTables(locale as Locale);

const dummyItems: ServerItemList[] = [
Expand Down
Loading

0 comments on commit 9751e8f

Please sign in to comment.