diff --git a/common/index.tsx b/common/index.tsx new file mode 100644 index 0000000..2fd71b8 --- /dev/null +++ b/common/index.tsx @@ -0,0 +1,14 @@ +export const get = async (url: string) => { + const resp = await fetch(url); + const json = await resp.json(); + return json; +}; +export const post = async (url: string, json: any) => { + const resp = await fetch(url, { + method: "POST", + headers: { "Content-Type": "application/json" }, + body: JSON.stringify(json), + }); + const result = await resp.json(); + return result; +}; diff --git a/components/Timetable.tsx b/components/Timetable.tsx index b09774c..975b5e1 100644 --- a/components/Timetable.tsx +++ b/components/Timetable.tsx @@ -1,4 +1,5 @@ import React from "react"; +import { get, post } from "@/common"; interface Conflicts { [index: string]: HTMLInputElement[]; @@ -47,17 +48,12 @@ const Timetable = ({ user }) => { } } // post request - const resp = await fetch("/api/record", { - method: "POST", - headers: { "Content-Type": "appliction/json" }, - body: JSON.stringify({ - name: target.name, - checked: target.checked, - user, - }), + const json = await post("/api/record", { + name: target.name, + checked: target.checked, + user, }); - if (!resp.ok) { - const json = await resp.json(); + if (json.error !== undefined) { alert(json.error); // revert conflict changed input for (const { input, disable } of changedInputs) { @@ -138,8 +134,7 @@ const Timetable = ({ user }) => { }; const refresh = async () => { - const resp = await fetch(`/api/record?name=${user}`); - const json = await resp.json(); + const json = await get(`/api/record?name=${user}`); const occupied: string[] = json.occupied; const myselect: string[] = json.myselect; console.log(json); @@ -170,16 +165,13 @@ const Timetable = ({ user }) => { }); React.useEffect(() => { - fetch("/api/html") - .then((resp) => resp.json()) - .then((json) => { - console.log(ref); - ref.current.innerHTML = json.html; - }) - .then(() => { - handleInput({ target: ref.current }); - refresh(); - }); + const main = async () => { + const json = await get("/api/html"); + ref.current.innerHTML = json.html; + handleInput({ target: ref.current }); + refresh(); + }; + main(); }, []); return ( diff --git a/pages/api/admin.ts b/pages/api/admin.ts index 6a7c91c..81a2cf8 100644 --- a/pages/api/admin.ts +++ b/pages/api/admin.ts @@ -1,5 +1,5 @@ import type { NextApiRequest, NextApiResponse } from "next"; -import store from "@/store"; +import {store, html} from "@/store"; export default function handler( req: NextApiRequest, @@ -9,15 +9,7 @@ export default function handler( // update store console.log('admin', req.body) const json = req.body - for (const key in json) { - store[key] = json[key]; - } - const keys = Object.keys(json) - for (const key in store) { - if (json[key] === undefined) { - delete store[key] - } - } + store.update(json) } - res.status(200).json(store); + res.status(200).json(store.get()); } diff --git a/pages/api/begin.ts b/pages/api/begin.ts deleted file mode 100644 index 970ae32..0000000 --- a/pages/api/begin.ts +++ /dev/null @@ -1,10 +0,0 @@ -import type { NextApiRequest, NextApiResponse } from "next"; -import config from "@/config"; - -export default function handler( - req: NextApiRequest, - res: NextApiResponse, -) { - config.begin = true; - res.status(200).json(config); -} diff --git a/pages/api/config.ts b/pages/api/config.ts new file mode 100644 index 0000000..892a1c4 --- /dev/null +++ b/pages/api/config.ts @@ -0,0 +1,11 @@ +import type { NextApiRequest, NextApiResponse } from "next"; +import config from "@/config"; + +export default function handler(req: NextApiRequest, res: NextApiResponse) { + if (req.method === "POST") { + // update config + config.begin = req.body.begin ?? config.begin; + config.limit = req.body.limit ?? config.limit; + } + res.status(200).json(config); +} diff --git a/pages/api/html.ts b/pages/api/html.ts index c375267..447948a 100644 --- a/pages/api/html.ts +++ b/pages/api/html.ts @@ -1,467 +1,11 @@ import type { NextApiRequest, NextApiResponse } from "next"; -import store from "@/store"; +import { html } from "@/store"; -export default function handler( - req: NextApiRequest, - res: NextApiResponse>, -) { +export default function handler(req: NextApiRequest, res: NextApiResponse) { + if (req.method === "POST") { + html.set(req.body.html); + } res.status(200).json({ - html: ` - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
T6值班2月6日/周一2月7日/周二2月8日/周三2月9日/周四2月10日/周五T6值班2月11日/周六2月12日/周日
08:00-10:001245609:00-12:003238
10:00-12:00789101112:00-15:003339
12:00-14:00121314151615:00-17:303440
14:00-16:001718192021


16:00-18:002223242526


18:00-21:002728293031


T6值班2月13日/周一2月14日/周二





08:00-10:004450





10:00-12:004551





12:00-14:004652





14:00-16:004753





16:00-18:004854





18:00-21:004955























V26值班2月6日/周一2月7日/周二2月8日/周三2月9日/周四2月10日/周五


08:00-10:0012456


10:00-12:007891011


12:00-14:001213141516


14:00-16:001718192021


16:00-18:002223242526


18:00-21:002728293031


V26值班2月13日/周一2月14日/周二





08:00-10:004450





10:00-12:004551





12:00-14:004652





14:00-16:004753





16:00-18:004854





18:00-21:004955














电话值班2月6日/周一2月7日/周二2月8日/周三2月9日/周四2月10日/周五电话值班+图书馆值班2月11日/周六2月12日/周日
07:50-08:501245609:00-12:003238
11:30-13:307,128,139,1410,1511,1612:00-15:003339
17:00-21:00
电话值班+图书馆值班
22,2723,2824,2925,3036,3115:00-17:303440
电话值班2月13日/周一2月14日/周二


17:30-21:003536
07:50-08:504450





11:30-13:3045,4651,52





17:00-21:00
电话值班+图书馆值班
48,4954,55





- - - - -edit.tsx:6:12 -` + html: html.get(), }); } diff --git a/pages/api/limit.ts b/pages/api/limit.ts deleted file mode 100644 index 6efb7e6..0000000 --- a/pages/api/limit.ts +++ /dev/null @@ -1,10 +0,0 @@ -import type { NextApiRequest, NextApiResponse } from "next"; -import config from "@/config"; - -export default function handler( - req: NextApiRequest, - res: NextApiResponse, -) { - config.limit = parseInt(req.query.limit) || 2; - res.status(200).json(config); -} \ No newline at end of file diff --git a/pages/api/pause.ts b/pages/api/pause.ts deleted file mode 100644 index 5fec025..0000000 --- a/pages/api/pause.ts +++ /dev/null @@ -1,10 +0,0 @@ -import type { NextApiRequest, NextApiResponse } from "next"; -import config from "@/config"; - -export default function handler( - req: NextApiRequest, - res: NextApiResponse, -) { - config.begin = false; - res.status(200).json(config); -} \ No newline at end of file diff --git a/pages/api/record.ts b/pages/api/record.ts index 2b55949..054eab1 100644 --- a/pages/api/record.ts +++ b/pages/api/record.ts @@ -1,12 +1,10 @@ // Next.js API route support: https://nextjs.org/docs/api-routes/introduction import type { NextApiRequest, NextApiResponse } from "next"; -import store from "@/store"; +import { store as storeProxy } from "@/store"; import config from "@/config"; -export default function handler( - req: NextApiRequest, - res: NextApiResponse, -) { +export default function handler(req: NextApiRequest, res: NextApiResponse) { + const store = storeProxy.get(); if (req.method === "POST") { if (!config.begin) { res.status(400).json({ diff --git a/pages/control.tsx b/pages/control.tsx new file mode 100644 index 0000000..0b7873d --- /dev/null +++ b/pages/control.tsx @@ -0,0 +1,56 @@ +import React from "react"; +import Head from "next/head"; +import { get, post } from "@/common"; + +const ControlPage = () => { + const [isBegin, setIsBegin] = React.useState(false); + const [inputLimit, setInputLimit] = React.useState("2"); + const toggleBegin = async () => { + const json = await post("/api/config", { begin: !isBegin }); + setIsBegin(json.begin); + }; + const refresh = async () => { + const json = await get("/api/config"); + setInputLimit(json.limit); + setIsBegin(json.begin); + }; + React.useEffect(() => { + refresh(); + }, []); + return ( + <> + + Create Next App + + + + +
+

+ +

+

+ ) => + setInputLimit(event.target.value) + } + /> + +

+
+ + ); +}; + +export default ControlPage; diff --git a/pages/edit.tsx b/pages/edit.tsx index cba61a9..c8db006 100644 --- a/pages/edit.tsx +++ b/pages/edit.tsx @@ -1,11 +1,33 @@ import React from "react"; +import { get, post } from "@/common"; const EditPage = () => { - return
{ - console.log(event.currentTarget.innerHTML); - }} - contentEditable="true">
; + const ref = React.useRef(); + const upload = async () => { + const html = ref.current.innerHTML; + await post("/api/html", { html }); + alert("Upload success"); + refresh(); + }; + const refresh = async () => { + const html = await get("/api/html"); + ref.current.innerHTML = html.html; + }; + React.useEffect(() => { + refresh(); + }, []); + return ( + <> + +
{ + console.log(event.currentTarget.innerHTML); + }} + contentEditable="true" + >
+ + ); }; export default EditPage; diff --git a/store/index.ts b/store/index.ts index 238ecec..53f4c1f 100644 --- a/store/index.ts +++ b/store/index.ts @@ -1,2 +1,41 @@ -const store: Record = {}; -export default store; +class Store { + record: Record; + constructor() { + this.record = {}; + } + public get() { + return this.record; + } + public set(key: string, val: string) { + this.record[key] = val; + } + public delete(key: string) { + delete this.record[key]; + } + public update(record: Record) { + for (const key in record) { + this.record[key] = record[key]; + } + for (const key in this.record) { + if (record[key] === undefined) { + delete this.record[key]; + } + } + } +} + +class HTML { + html: string; + constructor() { + this.html = ""; + } + public get() { + return this.html; + } + public set(html) { + this.html = html; + } +} + +export const html = new HTML(); +export const store = new Store();