diff --git a/.gitignore b/.gitignore index ac28837..c27c882 100644 --- a/.gitignore +++ b/.gitignore @@ -1,6 +1,8 @@ /html.html /json /hours.json +/regular.json +/record.json # See https://help.github.com/articles/ignoring-files/ for more about ignoring files. # dependencies diff --git a/components/Timetable.tsx b/components/Timetable.tsx index 3e6b3fa..052ac14 100644 --- a/components/Timetable.tsx +++ b/components/Timetable.tsx @@ -11,10 +11,15 @@ interface ConflictsTmp { interface IndexToElement { [index: string]: HTMLInputElement; } +interface IndexToCell { + [index: string]: HTMLTableCellElement; +} const indexToElement: IndexToElement = {}; +const indexToCell: IndexToCell = {}; const conflicts: Conflicts = {}; const marks: (HTMLInputElement | null)[][] = []; +const tds: (HTMLTableCellElement | null)[][] = []; const downloadObjectAsJson = (exportObj: any, exportName: string) => { const dataStr = @@ -33,6 +38,8 @@ const Timetable = ({ disableNetwork = false, disableConflictCheck = false, replaceInputType = "checkbox", + apiRecordEndPoint = "/api/record", + openRecordMode = false, }) => { const [editable, setEditable] = React.useState(true); const ref = React.useRef(); @@ -102,6 +109,8 @@ const Timetable = ({ // empty marks marks.length = 0; + // empty tds + tds.length = 0; const table = target.children[0]; table.setAttribute("border", "1"); @@ -112,8 +121,9 @@ const Timetable = ({ for (const tr_index in tbody.children) { const tr = tbody.children[tr_index]; const row: (HTMLInputElement | null)[] = []; + const rowTD: (HTMLTableCellElement | null)[] = []; for (const td_index in tr.children) { - const td = tr.children[td_index]; + const td: HTMLTableCellElement = tr.children[td_index]; if (td.tagName !== "TD") continue; if (td.getAttribute("bgcolor")?.toUpperCase() !== "#39CEFF") { row.push(null); @@ -135,10 +145,13 @@ const Timetable = ({ td.innerHTML = ""; td.appendChild(input); indexToElement[index] = input; + indexToCell[index] = td; row.push(input); + rowTD.push(td); } marks.push(row); + tds.push(rowTD); // console.log("marks", marks); } @@ -159,7 +172,20 @@ const Timetable = ({ }; const refresh = async () => { - const json = await get(`/api/record?name=${user}`); + const json = await get(`${apiRecordEndPoint}?name=${user}`); + if (openRecordMode) { + for (const index in json) { + const input = indexToElement[index]; + const td= indexToCell[index]; + if (json[index] !== user) { + td.innerHTML = json[index] + } else { + input.checked = true; + input.disabled = true; + } + } + return; + } const occupied: string[] = json.occupied; const myselect: string[] = json.myselect; console.log(json); @@ -172,6 +198,7 @@ const Timetable = ({ const includes = myselect.includes(index); indexToElement[index].checked = includes; // after checked, find conflicts input + if (disableConflictCheck) continue; if (includes) { for (const input of conflicts[index]) { if (input.name === index) continue; @@ -232,7 +259,7 @@ const Timetable = ({ overflow: "scroll", }} onInput={handleInput} - > + >{" "}
> ); diff --git a/config/index.ts b/config/index.ts index d65e503..137ee42 100644 --- a/config/index.ts +++ b/config/index.ts @@ -1,4 +1,6 @@ -export default { - begin: false, - limit: 2, -} \ No newline at end of file +const obj = { + begin: false, + limit: 2, + token: process.env.TOKEN || "woshimima", +}; +export default obj; diff --git a/pages/api/regular.ts b/pages/api/regular.ts new file mode 100644 index 0000000..41631e5 --- /dev/null +++ b/pages/api/regular.ts @@ -0,0 +1,16 @@ +import type { NextApiRequest, NextApiResponse } from "next"; +import { regular } from "@/store"; +import config from "@/config"; + +export default function handler(req: NextApiRequest, res: NextApiResponse) { + if (req.method === "POST") { + if (req.headers.token !== config.token) { + res.status(403).json({ + error: "wrong token", + }); + return; + } + regular.update(req.body); + } + res.status(200).json(regular.get()); +} diff --git a/pages/regular.tsx b/pages/regular.tsx new file mode 100644 index 0000000..811ca0f --- /dev/null +++ b/pages/regular.tsx @@ -0,0 +1,32 @@ +import React from "react"; +import Head from "next/head"; +import Timetable from "@/components/Timetable"; +import UserInputWrap from "@/components/UserInputWrap"; + +const ReportPage = () => { + const [user, setUser] = React.useState(""); + const [begin, setBegin] = React.useState(false); + return ( + <> + +