summary refs log tree commit diff
path: root/events.c
diff options
context:
space:
mode:
authorJune McEnroe <june@causal.agency>2020-07-11 21:24:56 -0400
committerJune McEnroe <june@causal.agency>2020-07-11 21:25:50 -0400
commiteaf1dddbe5326f347757c21247ba5b8b0047d81c (patch)
tree4a8c1355de8bc79bd8186590eacc9e52b69808da /events.c
parentCheck database version against a range (diff)
downloadscooper-eaf1dddbe5326f347757c21247ba5b8b0047d81c.tar.gz
scooper-eaf1dddbe5326f347757c21247ba5b8b0047d81c.zip
Add date jump form to events page
Sad that Safari doesn't implement anything for the date input type.
Diffstat (limited to 'events.c')
-rw-r--r--events.c86
1 files changed, 68 insertions, 18 deletions
diff --git a/events.c b/events.c
index 679a8af..89da92d 100644
--- a/events.c
+++ b/events.c
@@ -22,6 +22,72 @@
 
 #include "server.h"
 
+static enum kcgi_err redirect(struct kreq *req, struct Scope scope) {
+	struct tm *tm = gmtime(&(time_t) { time(NULL) });
+	if (!tm) err(EX_OSERR, "gmtime");
+
+	char time[sizeof("0000-00-00T00:00:00")];
+	strftime(time, sizeof(time), "%FT%T", tm);
+
+	char *url = khttp_urlpart(
+		NULL, NULL, Pages[Events],
+		Keys[Network].name, scope.network,
+		Keys[Context].name, scope.context,
+		Keys[Before].name, time,
+		NULL
+	);
+	enum kcgi_err error = httpRedirect(req, url);
+	free(url);
+	return error;
+}
+
+static enum kcgi_err
+dateForm(struct khtmlreq *html, struct Scope scope, const char *_time) {
+	struct tm tm = {0};
+	if (!strptime(_time, "%F", &tm)) {
+		struct tm *now = gmtime(&(time_t) { time(NULL) });
+		if (!now) err(EX_OSERR, "gmtime");
+		tm = *now;
+	}
+	char date[sizeof("0000-00-00")];
+	strftime(date, sizeof(date), "%F", &tm);
+	return 0
+		|| khtml_attr(
+			html, KELEM_FORM,
+			KATTR_METHOD, "get",
+			KATTR_ACTION, Pages[Events],
+			KATTR__MAX
+		)
+		|| khtml_attr(
+			html, KELEM_INPUT,
+			KATTR_TYPE, "hidden",
+			KATTR_NAME, Keys[Network].name,
+			KATTR_VALUE, scope.network,
+			KATTR__MAX
+		)
+		|| khtml_attr(
+			html, KELEM_INPUT,
+			KATTR_TYPE, "hidden",
+			KATTR_NAME, Keys[Context].name,
+			KATTR_VALUE, scope.context,
+			KATTR__MAX
+		)
+		|| khtml_attr(
+			html, KELEM_INPUT,
+			KATTR_TYPE, "date",
+			KATTR_NAME, Keys[After].name,
+			KATTR_VALUE, date,
+			KATTR__MAX
+		)
+		|| khtml_attr(
+			html, KELEM_INPUT,
+			KATTR_TYPE, "submit",
+			KATTR_VALUE, "Jump",
+			KATTR__MAX
+		)
+		|| khtml_closeelem(html, 1);
+}
+
 const char *EventsAfterQuery = SQL(
 	SELECT
 		events.event,
@@ -71,26 +137,9 @@ const char *EventsBeforeQuery = SQL(
 enum kcgi_err pageEvents(struct kreq *req) {
 	struct Scope scope = htmlScope(req);
 	if (!scope.network || !scope.context) return httpFail(req, KHTTP_400);
-
 	if (!req->fieldmap[After] && !req->fieldmap[Before]) {
-		struct tm *tm = gmtime(&(time_t) { time(NULL) });
-		if (!tm) err(EX_OSERR, "gmtime");
-
-		char time[sizeof("0000-00-00T00:00:00")];
-		strftime(time, sizeof(time), "%FT%T", tm);
-
-		char *url = khttp_urlpart(
-			NULL, NULL, Pages[Events],
-			Keys[Network].name, scope.network,
-			Keys[Context].name, scope.context,
-			Keys[Before].name, time,
-			NULL
-		);
-		enum kcgi_err error = httpRedirect(req, url);
-		free(url);
-		return error;
+		return redirect(req, scope);
 	}
-
 	const char *time = req->fieldmap[Before]
 		? req->fieldmap[Before]->parsed.s
 		: req->fieldmap[After]->parsed.s;
@@ -104,6 +153,7 @@ enum kcgi_err pageEvents(struct kreq *req) {
 		|| khtml_open(&html, req, KHTML_PRETTY)
 		|| htmlHead(&html, scope.context)
 		|| htmlNav(&html, scope)
+		|| dateForm(&html, scope, time)
 		|| khtml_elem(&html, KELEM_TABLE);
 	if (error) return error;