diff --git a/.gitignore b/.gitignore index 0a3b136..96b7c01 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ +/data /html.html /json /hours.json diff --git a/components/Timetable.tsx b/components/Timetable.tsx index dcb4b64..b191ec8 100644 --- a/components/Timetable.tsx +++ b/components/Timetable.tsx @@ -35,6 +35,7 @@ const downloadObjectAsJson = (exportObj: any, exportName: string) => { const Timetable = ({ user, + isRegular = false, disableNetwork = false, disableConflictCheck = false, replaceInputType = "checkbox", @@ -221,7 +222,7 @@ const Timetable = ({ } }; React.useEffect(() => { - if (disableNetwork) return; + if (disableNetwork || isRegular) return; const interval = setInterval(() => { refresh(); @@ -233,7 +234,7 @@ const Timetable = ({ React.useEffect(() => { const main = async () => { - const json = await get("/api/html"); + const json = await get(isRegular ? "/api/html-regular" : "/api/html"); ref.current.innerHTML = json.html; handleInput({ target: ref.current }); refresh(); diff --git a/pages/api/html-regular.ts b/pages/api/html-regular.ts new file mode 100644 index 0000000..c88235f --- /dev/null +++ b/pages/api/html-regular.ts @@ -0,0 +1,16 @@ +import type { NextApiRequest, NextApiResponse } from "next"; +import { htmlRegular } 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; + } + htmlRegular.set(req.body.html); + } + res.status(200).json({ + html: htmlRegular.get(), + }); +} diff --git a/pages/api/tool.ts b/pages/api/tool.ts index 4cf661a..bf2c0fc 100644 --- a/pages/api/tool.ts +++ b/pages/api/tool.ts @@ -8,7 +8,7 @@ const read = promisify(fs.readFile); // 索引与工时 const indexToHour: Record = JSON.parse( - fs.readFileSync("./hours.json", "utf8") + fs.readFileSync("./data/hours.json", "utf8") ).selections; export default async function handler( @@ -18,7 +18,7 @@ export default async function handler( // 读入全部json文件 // users: {姓名: {坐标: 权重}} const users: Record> = {}; - const files = await g("./json/*.json"); + const files = await g("./data/json/*.json"); for (const file of files) { const jsonStr = await read(file, "utf8"); const json: { diff --git a/pages/edit-regular.tsx b/pages/edit-regular.tsx new file mode 100644 index 0000000..50d4acd --- /dev/null +++ b/pages/edit-regular.tsx @@ -0,0 +1,39 @@ +import React from "react"; +import { get, post } from "@/common"; + +const EditPage = () => { + const [token, setToken] = React.useState(""); + const ref = React.useRef(); + const upload = async () => { + const html = ref.current.innerHTML; + await post("/api/html-regular", { html }, { token }); + alert("Upload success"); + refresh(); + }; + const refresh = async () => { + const html = await get("/api/html-regular"); + ref.current.innerHTML = html.html; + }; + React.useEffect(() => { + refresh(); + }, []); + return ( + <> + setToken(event.target.value)} + /> + +
{ + console.log(event.currentTarget.innerHTML); + }} + contentEditable="true" + >
+ + ); +}; + +export default EditPage; diff --git a/pages/regular.tsx b/pages/regular.tsx index 811ca0f..34b1fb7 100644 --- a/pages/regular.tsx +++ b/pages/regular.tsx @@ -22,6 +22,7 @@ const ReportPage = () => { disableNetwork={true} apiRecordEndPoint="/api/regular" openRecordMode={true} + isRegular={true} /> diff --git a/store/index.ts b/store/index.ts index 0397333..2cb0427 100644 --- a/store/index.ts +++ b/store/index.ts @@ -36,10 +36,12 @@ class Store { class HTML { html: string; - constructor() { + filename: string; + constructor(filename: string) { + this.filename = filename; // load from file try { - this.html = fs.readFileSync("./html.html", "utf8"); + this.html = fs.readFileSync(this.filename, "utf8"); } catch { this.html = ""; } @@ -50,10 +52,11 @@ class HTML { public async set(html: string) { this.html = html; // store into file - await write("./html.html", html, "utf8"); + await write(this.filename, html, "utf8"); } } -export const html = new HTML(); -export const store = new Store("store.json"); -export const regular = new Store("regular.json"); +export const html = new HTML("./data/html.html"); +export const htmlRegular = new HTML("./data/html-regular.html"); +export const store = new Store("./data/store.json"); +export const regular = new Store("./data/regular.json");