view_calendar.js: finalized a fetch design pattern
authorArt Cancro <ajc@citadel.org>
Wed, 27 Dec 2023 19:48:52 +0000 (14:48 -0500)
committerArt Cancro <ajc@citadel.org>
Wed, 27 Dec 2023 19:48:52 +0000 (14:48 -0500)
This uses "fetch" followed by ".then" etc. etc. etc.
and is tuned to be a design pattern for future code

webcit-ng/server/calendar_functions.c
webcit-ng/static/js/view_calendar.js

index 7d88996ed1e16271c6528297b1b8b794fcf948ad..fe179cb008cc86daa3953026a7118fd58a2af9b0 100644 (file)
@@ -45,5 +45,9 @@ void calendar_msglist(struct http_transaction *h, struct ctdlsession *c, char *r
        array_free(msglist);
 
        // FIXME we still fail because we aren't finished yet
-       do_404(h);
+       add_response_header(h, strdup("Content-type"), strdup("application/json"));
+       h->response_code = 200;
+       h->response_string = strdup("OK");
+       h->response_body = "{ \"one\":111 , \"two\":222 , \"three\":333 }";
+       h->response_body_length = strlen(h->response_body);
 }
index 17a1a4ea5c62c8a5b56d15d76617ea6486532031..1ac05266a0a5aa1d3dcdac9829f42e5840d4b57e 100644 (file)
@@ -8,20 +8,22 @@
 
 // RENDERER FOR THIS VIEW
 function view_render_calendar() {
-       document.getElementById("ctdl-main").innerHTML = `STUB RENDERER FOR CALENDAR ROOM`;
-
        fetch(
                "/ctdl/r/" + escapeHTMLURI(current_room) + "/calendar::"
        )
-       .then((response) => {
+       .then(response => {
                if (response.ok) {
                        return(response.json());
                }
+               else {
+                       throw new Error(`${response.status} ${response.statusText}`);
+               }
        })
-       .then((j) => {
-               console.log("Something");
+       .then(j => {
+               document.getElementById("ctdl-main").innerHTML = JSON.stringify(j);
        })
-       .catch((error) => {
-               console.log("Error: " + error);
+       .catch(error => {
+               console.log(error);
+               document.getElementById("ctdl-main").innerHTML = `<div class="ctdl-fatal-error">${error}</div>`;
        });
 }