changeset 40:752bb1cd2dd8

themes: improve status page
author David Demelier <markand@malikania.fr>
date Sun, 07 Aug 2022 08:43:32 +0200
parents 27f039a790f4
children 00b9af607524
files scid/page-index.c scid/pageutil.c scid/theme.c scid/theme.h themes/bulma/index.mustache themes/bulma/status.mustache themes/bulma/theme.js
diffstat 7 files changed, 82 insertions(+), 52 deletions(-) [+]
line wrap: on
line diff
--- a/scid/page-index.c	Sun Aug 07 08:42:04 2022 +0200
+++ b/scid/page-index.c	Sun Aug 07 08:43:32 2022 +0200
@@ -119,7 +119,6 @@
 		pageutil_render(req, KHTTP_200, KMIME_TEXT_HTML, data);
 		free(data);
 		json_decref(root);
-		json_decref(projects);
 	} else
 		pageutil_status(req, KHTTP_500);
 }
--- a/scid/pageutil.c	Sun Aug 07 08:42:04 2022 +0200
+++ b/scid/pageutil.c	Sun Aug 07 08:43:32 2022 +0200
@@ -41,6 +41,20 @@
 	khttp_free(req);
 }
 
+static const int statustab[] = {
+	[KHTTP_200] = 200,
+	[KHTTP_400] = 400,
+	[KHTTP_404] = 404,
+	[KHTTP_500] = 500
+};
+
+static const char * const statusmsg[] = {
+	[KHTTP_200] = "OK",
+	[KHTTP_400] = "Bad Request",
+	[KHTTP_404] = "Not Found",
+	[KHTTP_500] = "Internal Server Error"
+};
+
 void
 pageutil_status(struct kreq *req, enum khttp status)
 {
@@ -48,7 +62,11 @@
 
 	char *body;
 
-	body = theme_page_status(status);
+	/*
+	 * KHTTP_ are numbered like a standard enum, the Javascript code must
+	 * get the appropriate HTTP code instead.
+	 */
+	body = theme_page_status(statustab[status], statusmsg[status]);
 	pageutil_render(req, status, KMIME_TEXT_HTML, body);
 	free(body);
 }
--- a/scid/theme.c	Sun Aug 07 08:42:04 2022 +0200
+++ b/scid/theme.c	Sun Aug 07 08:43:32 2022 +0200
@@ -360,12 +360,12 @@
 }
 
 char *
-theme_page_status(enum khttp status)
+theme_page_status(int status, const char *message)
 {
 	json_t *doc;
 	char *ret;
 
-	doc = util_json_pack("{si}", "status", status);
+	doc = util_json_pack("{si ss}", "status", status, "message", message);
 	ret = call(doc, "onPageStatus");
 
 	json_decref(doc);
--- a/scid/theme.h	Sun Aug 07 08:42:04 2022 +0200
+++ b/scid/theme.h	Sun Aug 07 08:43:32 2022 +0200
@@ -109,12 +109,13 @@
  * }
  * ```
  *
- * \param status the status code
+ * \param status the status code (e.g. 404)
+ * \param message the status message (e.g. Not found)
  * \return a newly allocated rendered string
  * \note You must free the return value
  */
 char *
-theme_page_status(enum khttp status);
+theme_page_status(int status, const char *msg);
 
 /**
  * Cleanup theme resources.
--- a/themes/bulma/index.mustache	Sun Aug 07 08:42:04 2022 +0200
+++ b/themes/bulma/index.mustache	Sun Aug 07 08:43:32 2022 +0200
@@ -1,45 +1,45 @@
-<div class="columns" style="flex-wrap: wrap">
-	{{#projects}}
-	<div class="column">
-		<div class="card">
-			<div class="card-content">
-				<div class="media">
-					<div class="media-content">
-						<p class="title is-4"><a href="/projects/{{name}}">{{name}}</a></p>
-						<p class="subtitle is-6">{{desc}}</p>
-					</div>
-				</div>
+				<div class="columns" style="flex-wrap: wrap">
+					{{#projects}}
+					<div class="column">
+						<div class="card">
+							<div class="card-content">
+								<div class="media">
+									<div class="media-content">
+										<p class="title is-4"><a href="/projects/{{name}}">{{name}}</a></p>
+										<p class="subtitle is-6">{{desc}}</p>
+									</div>
+								</div>
 
-				<div class="content">
-					{{#jobs}}
-					<table class="table">
-						<thead>
-							<tr>
-								<th>job</th>
-								<th>tag</th>
-								<th>status</th>
-							</tr>
-						</thead>
-					{{#jobs}}
-						<tr>
-							<td><a href="jobs/{{job}}">{{id}}</a></td>
-							<td>{{tag}}</td>
-							<td><span class="tag {{classname}} is-light">{{status}}</span></td>
-						</tr>
-					{{/jobs}}
-					</table>
-					{{/jobs}}
+								<div class="content">
+									{{#jobs}}
+									<table class="table">
+										<thead>
+											<tr>
+												<th>job</th>
+												<th>tag</th>
+												<th>status</th>
+											</tr>
+										</thead>
+									{{#jobs}}
+										<tr>
+											<td><a href="jobs/{{job}}">{{id}}</a></td>
+											<td>{{tag}}</td>
+											<td><span class="tag {{classname}} is-light">{{status}}</span></td>
+										</tr>
+									{{/jobs}}
+									</table>
+									{{/jobs}}
 
-					<br/>
-					{{#jobs}}
-					<p class="is-size-7">{{n-success}} successful jobs, {{n-failed}} failed jobs</p>
-					{{/jobs}}
-					{{^jobs}}
-					<p class="is-size-7">no jobs yet.</p>
-					{{/jobs}}
+									<br/>
+									{{#jobs}}
+									<p class="is-size-7">{{n-success}} successful jobs, {{n-failed}} failed jobs</p>
+									{{/jobs}}
+									{{^jobs}}
+									<p class="is-size-7">no jobs yet.</p>
+									{{/jobs}}
+								</div>
+							</div>
+						</div>
+					</div>
+					{{/projects}}
 				</div>
-			</div>
-		</div>
-	</div>
-	{{/projects}}
-</div>
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/themes/bulma/status.mustache	Sun Aug 07 08:43:32 2022 +0200
@@ -0,0 +1,1 @@
+				<h1>{{status}} - {{message}}</h1>
--- a/themes/bulma/theme.js	Sun Aug 07 08:42:04 2022 +0200
+++ b/themes/bulma/theme.js	Sun Aug 07 08:43:32 2022 +0200
@@ -16,12 +16,18 @@
  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  */
 
-function onPageIndex(rdr, data)
+function render(rdr, page, title, data)
 {
 	Scid.render(rdr, "header.mustache", {
-		title: "sci -- index page"
+		title: title
 	});
 
+	Scid.render(rdr, page, data);
+	Scid.render(rdr, "footer.mustache");
+}
+
+function onPageIndex(rdr, data)
+{
 	/*
 	 * Add is-danger/is-success for every job depending on their success
 	 * status, this is required to show the appropriate tag.
@@ -35,6 +41,11 @@
 		}
 	}
 
-	Scid.render(rdr, "index.mustache", data);
-	Scid.render(rdr, "footer.mustache");
+	render(rdr, "index.mustache", "sci -- index page", data);
 }
+
+function onPageStatus(rdr, data)
+{
+	Scid.print(JSON.stringify(data));
+	render(rdr, "status.mustache", "sci -- " + data.status, data);
+}