From f7070adeb0a4931cc52347fce53d3aae7792a2f6 Mon Sep 17 00:00:00 2001 From: Tommy Gomez Date: Thu, 16 Jul 2020 20:47:04 +0000 Subject: [PATCH 1/2] fix error handling within Promise's catch --- src/main/webapp/script.js | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/src/main/webapp/script.js b/src/main/webapp/script.js index 47ca81f..79d95e2 100644 --- a/src/main/webapp/script.js +++ b/src/main/webapp/script.js @@ -38,3 +38,33 @@ function createCommentElement(comment) { function addParagraph(content) { return "

" + content + "

"; } + +function newGroupchat() { + const request = new Request("/load-groupchat", {method: 'POST'}); + fetch(request).then(response => response.text()).then((key) => { + redirectToGroupchat(key); + }); +} + +function loadGroupchat() { + const urlParams = new URLSearchParams(window.location.search); + const key = urlParams.get('key'); + fetch('/load-groupchat?key=' + key).then(response => response.json()) + .catch(error => { + alert('Error: Groupchat does not exist'); + window.location.href = 'index.html'; + }).then((messages) => { + const messageContainer = document.getElementById('messages'); + messageContainer.innerHTML = ''; + for (var i = 0; i < messages.length; i++) { + const message = document.createElement('p'); + message.innerText = messages[i]; + messageContainer.appendChild(message); + } + }); +} + +function redirectToGroupchat(keyParameter) { + const key = keyParameter ? keyParameter : document.getElementById('groupchat-key').value; + window.location.href = "/groupchat.html?key=" + key; +} From 2d25b1432fbc8045bdc966319449fc657da6d27f Mon Sep 17 00:00:00 2001 From: Tommy Gomez Date: Thu, 16 Jul 2020 22:49:46 +0000 Subject: [PATCH 2/2] add form to groupchat.html to submit new messages --- src/main/webapp/groupchat.html | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/src/main/webapp/groupchat.html b/src/main/webapp/groupchat.html index 24e1803..fa867ad 100644 --- a/src/main/webapp/groupchat.html +++ b/src/main/webapp/groupchat.html @@ -10,5 +10,12 @@

Groupchat

+
+ +
+
+ + +
\ No newline at end of file