commit 36090048851cdf7e07ac53209d78429cb3a5f157
parent 3b59becd574888d4dce219fb6f4e1b4e337a62f7
Author: Jacob R. Edwards <jacob@jacobedwards.org>
Date:   Thu, 29 Feb 2024 20:15:06 -0800
Add timekeeping page
It's not much of anything right now. Just a paragraph with a number
which can be updated every second using JavaScript or simply reloaded
(which resets it to it's original value since there is no backend
yet).
Diffstat:
3 files changed, 49 insertions(+), 0 deletions(-)
diff --git a/Makefile b/Makefile
@@ -20,6 +20,9 @@ clean:
 	rm -f ${name} ${name}.o ${obj}
 
 install: ${name}
+	mkdir -p ${prefix}/scripts
+	cp scripts/* ${prefix}/scripts
+
 	# Otherwise 'Text file is busy'
 	pkill kfcgi || true
 	cp -af ${name} ${prefix}
@@ -27,6 +30,7 @@ install: ${name}
 
 uninstall:
 	rm -f ${prefix}/${name}
+	rm -rf ${prefix}/scripts
 
 .PHONY: clean install uninstall
 .SUFFIX: .c .o
diff --git a/scripts/main.js b/scripts/main.js
@@ -0,0 +1,16 @@
+counter = document.querySelector("#counter")
+
+function updatetime() {
+	counter.textContent = String(Number(counter.textContent) + 1)
+	if (!document.hidden)
+		setTimeout(updatetime, 1000)
+}
+
+<!--
+document.addEventListener("visibilitychange", () => {
+	if (!document.hidden) {
+		updatetime()
+});
+-->
+
+updatetime()
diff --git a/timekeeper.c b/timekeeper.c
@@ -40,6 +40,7 @@ enum Page {
 	PageLogin,
 	PageLogout,
 	PageAccount,
+	PageMain,
 	Page404,
 	PageMax
 };
@@ -71,6 +72,7 @@ static char *pages[] = {
 	[PageLogin] = "login",
 	[PageLogout] = "logout",
 	[PageAccount] = "account",
+	[PageMain] = "main",
 	[Page404] = "404"
 };
 
@@ -321,6 +323,9 @@ pageindex(struct pagedata *pd)
 		if ((status = khtml_puts(&pd->html, " | ")) != KCGI_OK ||
 		    (status = htmllink(pd, pages[PageAccount], "Manage account")))
 			return status;
+		if ((status = khtml_puts(&pd->html, " | ")) != KCGI_OK ||
+		    (status = htmllink(pd, pages[PageMain], "Timekeeper")))
+			return status;
 	}
 
 	return khtml_close(&pd->html);
@@ -523,6 +528,29 @@ pageaccount(struct pagedata *pd)
 	return khtml_close(&pd->html);
 }
 
+enum kcgi_err
+pagemain(struct pagedata *pd)
+{
+	enum kcgi_err status;
+	struct user *user;
+
+	user = sitegetlogin(pd);
+	if (!user)
+		return errorpage(pd, KHTTP_401);
+
+	if ((status = khttp_head(&pd->req, kresps[KRESP_STATUS], "%s", khttps[KHTTP_200])) != KCGI_OK ||
+	    (status = khttp_head(&pd->req, kresps[KRESP_CONTENT_TYPE], "%s", kmimetypes[KMIME_TEXT_HTML])) != KCGI_OK ||
+	    (status = khttp_body(&pd->req)))
+		return status;
+	khttp_puts(&pd->req, "<!doctype html><html><head><title>Main</title></head><body><h1>Main</h1>"
+		"<noscript><a href=\".\">Reload</a></noscript>"
+		"<p id=\"counter\">0</p>"
+		"<script src=\"scripts/main.js\"></script>"
+		"</body></html>");
+	freeuser(user);
+	return KCGI_OK;
+}
+
 int
 main(void)
 {
@@ -554,6 +582,7 @@ main(void)
 		[PageLogin] = pagelogin,
 		[PageLogout] = pagelogout,
 		[PageAccount] = pageaccount,
+		[PageMain] = pagemain,
 		[Page404] = page404
 	};