Skip to content

Commit

Permalink
fix server copy issure and add class list
Browse files Browse the repository at this point in the history
  • Loading branch information
ncukondo committed Dec 18, 2023
1 parent 9751e8f commit eb1654f
Show file tree
Hide file tree
Showing 7 changed files with 103 additions and 11 deletions.
2 changes: 1 addition & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"editor.defaultFormatter": "esbenp.prettier-vscode",
"editor.formatOnSave": true,
"editor.codeActionsOnSave": {
"source.fixAll.eslint": true
"source.fixAll.eslint": "explicit"
},
"[javascript]": {
"editor.tabSize": 2,
Expand Down
51 changes: 49 additions & 2 deletions src/pages/x/[id].tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { Tree } from "@libs/treeUtils";
import { fmt } from "@libs/utils";
import {
makeCumulativeOutcomeTableData,
makeItemTableData,
makeOutcomeTableData,
makeTableItemTableData,
} from "@services/curriculumMapTable";
Expand All @@ -27,7 +28,7 @@ import { loadOutcomesTree } from "@services/outcomes";
import type { OutcomeInfo } from "@services/outcomes";
import { searchOutcomes, searchTables } from "@services/search";
import { getAllTables, TableInfoSet } from "@services/tables";
import { itemIdToUrl } from "@services/urls";
import { itemIdToUrl, itemIdToUrlToEditFromUrl } from "@services/urls";

type PageProps = {
outcomesTree: Tree<OutcomeInfo>;
Expand Down Expand Up @@ -202,7 +203,9 @@ const CSVDownloadLinks = ({
const l4Data = makeCumulativeOutcomeTableData(items, outcomesTree, 4);
const l1to4Data = makeOutcomeTableData(items, outcomesTree, 4);
const tableData = makeTableItemTableData(items, allTables);
const itemsIdData = makeItemTableData(items);
const linkDataList = [
{ data: itemsIdData, label: t("downloadClassess"), filename: "classes.csv" },
{ data: l1Data, label: t("downloadL1"), filename: "outcomes_l1.csv" },
{ data: l2Data, label: t("downloadL2"), filename: "outcomes_l2.csv" },
{ data: l3Data, label: t("downloadL3"), filename: "outcomes_l3.csv" },
Expand All @@ -219,6 +222,47 @@ const CSVDownloadLinks = ({
);
};

const ItemsTable = ({ items }: { items: ServerItemList[] }) => {
const { t } = useLocaleText("@pages/x/[id]");
return (
<table className="m-4 table">
<thead>
<tr>
<th>{t("item-name")}</th>
<th>{t("item-place")}</th>
<th>{t("item-url")}</th>
<th>{t("item-url-to-edit")}</th>
</tr>
</thead>
<tbody>
{items.map(item => {
const url = itemIdToUrl(item.id);
const urlToEdit = itemIdToUrlToEditFromUrl(
item.id,
item.children && item.children?.length > 0,
);
return (
<tr key={item.id}>
<th>{item.name}</th>
<th>{item.place}</th>
<td>
<Link href={url} target="_blank" className="link">
{url}
</Link>
</td>
<td>
<Link href={urlToEdit} target="_blank" className="link">
{urlToEdit}
</Link>
</td>
</tr>
);
})}
</tbody>
</table>
);
};

const OutcomeAccessInfo = ({ id, isMap, name }: { id: string; isMap: boolean; name: string }) => {
const { t } = useLocaleText("@pages/x/[id]");
const link = isMap ? `/map?from=${id}` : `/list?from=${id}`;
Expand Down Expand Up @@ -315,7 +359,10 @@ const ListPage: NextPage<PageProps> = ({
)}
<ListData values={schemaWithValue} />
{children ? (
<CSVDownloadLinks {...{ items: children, allTables, outcomesTree }} />
<>
<ItemsTable items={children} />
<CSVDownloadLinks {...{ items: children, allTables, outcomesTree }} />
</>
) : (
<>
<OutcomesList {...{ allTables, outcomesTree, text, id }} />
Expand Down
16 changes: 10 additions & 6 deletions src/services/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,16 @@ const apiPost = async <PostData extends object, Response extends object>(
};

const apiGet = async <Response extends object>(api: string): Promise<Response> => {
const res = await fetch(api, {
method: "GET",
});
if (res.ok) {
const data = (await res.json()) as Response;
return data;
try {
const res = await fetch(api, {
method: "GET",
});
if (res.ok) {
const data = (await res.json()) as Response;
return data;
}
} catch (e) {
console.log(e);
}
throw new Error(`Cannot get fromapi:${api},${JSON.stringify(res, null, 2)}`);
};
Expand Down
16 changes: 16 additions & 0 deletions src/services/curriculumMapTable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { reduceTree, Tree } from "@libs/treeUtils";
import { ServerItemList } from "@services/itemList/server";
import { OutcomeInfo } from "@services/outcomes";
import { TableInfoSet } from "@services/tables";
import { itemIdToUrl, itemIdToUrlToEditFromUrl } from "./urls";

type LabelInfo = {
index: string;
Expand Down Expand Up @@ -90,6 +91,20 @@ const makeOutcomeTableData = (
return makeTableData(items, labels);
};

const makeItemTableData = (items: ServerItemList[]) => {
const header = ["name", "place", "url", "url_to_edit"];
const body = items.map(item => {
const row = [
item.name,
item.place,
itemIdToUrl(item.id),
itemIdToUrlToEditFromUrl(item.id, item.children && item.children?.length > 0),
];
return row;
});
return [header, ...body];
};

const makeCumulativeOutcomeTableData = (
items: ServerItemList[],
tree: Tree<OutcomeInfo>,
Expand All @@ -114,4 +129,5 @@ export {
makeTableTableData,
makeTableItemTableData,
makeCumulativeOutcomeTableData,
makeItemTableData,
};
11 changes: 11 additions & 0 deletions src/services/i18n/text.ts
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,17 @@ const text = {
ja: "表データと授業の関連付けデータをダウンロード(csv)",
en: "Download table data and association data with classes (csv)",
},
downloadClassess: {
ja: "授業一覧とurlをダウンロード(csv)",
en: "Download list of classes and urls (csv)",
},
"item-name": { ja: "名前", en: "name" },
"item-place": { ja: "場所", en: "place" },
"item-url": { ja: "url", en: "url" },
"item-url-to-edit": {
ja: "この内容を元に新しい項目を作成",
en: "Start a new edit based on this content",
},
notFound: {
ja: "該当する項目が見つかりません。",
en: "No matching items were found.",
Expand Down
3 changes: 2 additions & 1 deletion src/services/itemList/libs/callApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@ const shareCurriculumMapToServer = async (

const getCurriculuMapFromServer = async (id: string): Promise<Response<ServerCurriculumMap>> => {
try {
return await apiGet(`/api/v1/map/${id}`);
const data = await apiGet(`/api/v1/map/${id}`);
return { ok: true, data } as Response<ServerCurriculumMap>;
} catch (e) {
throw new Error(`Fail to get curriculumMap. ${e}`);
}
Expand Down
15 changes: 14 additions & 1 deletion src/services/urls.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,11 @@ const itemIdToUrl = (id: string) => {
return `${origin}/x/${id}`;
};

const itemIdToUrlToEditFromUrl = (id: string, isMap: boolean | null | undefined) => {
const params = new URLSearchParams([["from", id]]);
return `${origin}/${isMap ? "map" : "list"}?${params.toString()}`;
};

const objectiveIdToUrl = (id: string) => {
return `${origin}/#${id}`;
};
Expand All @@ -33,4 +38,12 @@ const isValidItemUrlOrId = (urlOrId: string) => {
return isValidShortId(id);
};

export { origin, useFullUrl, itemUrlToId, isValidItemUrlOrId, itemIdToUrl, objectiveIdToUrl };
export {
origin,
useFullUrl,
itemUrlToId,
isValidItemUrlOrId,
itemIdToUrl,
objectiveIdToUrl,
itemIdToUrlToEditFromUrl,
};

0 comments on commit eb1654f

Please sign in to comment.