From 210f310afecd42855352345e637e7452869143a6 Mon Sep 17 00:00:00 2001 From: zapdos26 Date: Sat, 18 Jan 2020 16:37:31 -0500 Subject: [PATCH] Added async/await to fix firefox issue + other things (#16) * Added async/await to fix firefox issue * Bump Version * Removed random F * Made skeleton post fetch function to fix duplicate code. * Adds checkResult to verify status received is ok on fetch; Renamed postServer to postToServer; Prevent notes from being empty; Removed window reload upon adding note/call; Added form reset on adding a note/call; Added refresh of notes on adding a call; Added await to getNotes in deleteCall(); Added that was missing in the admin.html Currently the reset of the addCall causes the validator to run (this occurs even with reloading the page). This will need to be looked into for FireFox. * Change version to align with e04222f --- docker-compose.yml | 2 +- package-lock.json | 2 +- package.json | 2 +- public/admin.html | 1 + public/js/admin.js | 50 +++++++++++++++++++++++++--------------------- 5 files changed, 31 insertions(+), 26 deletions(-) diff --git a/docker-compose.yml b/docker-compose.yml index fedb5fd..c21c363 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -29,4 +29,4 @@ services: - "33060:3306" environment: TZ: "America/New York" - MYSQL_ROOT_PASSWORD: "root" \ No newline at end of file + MYSQL_ROOT_PASSWORD: "root" diff --git a/package-lock.json b/package-lock.json index 50cdd8e..c2ca669 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,6 +1,6 @@ { "name": "headsup", - "version": "2.0.0", + "version": "2.0.1", "lockfileVersion": 1, "requires": true, "dependencies": { diff --git a/package.json b/package.json index 3530577..7b1ad0a 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "headsup", - "version": "1.1.1", + "version": "1.1.2", "description": "RPIA's digital whiteboard system", "main": "server.js", "scripts": { diff --git a/public/admin.html b/public/admin.html index 75e995b..d728f43 100644 --- a/public/admin.html +++ b/public/admin.html @@ -92,6 +92,7 @@ + diff --git a/public/js/admin.js b/public/js/admin.js index daedcc7..5c0eafc 100644 --- a/public/js/admin.js +++ b/public/js/admin.js @@ -1,5 +1,6 @@ async function getNotes() { const response = await fetch('/notes'); + checkResult(response); const notes = (await response.json()).data; let html = ''; if (notes.length !== 0) { @@ -25,35 +26,23 @@ async function getNotes() { } // eslint-disable-next-line -function createNote() { +async function createNote() { const note = document.querySelector('#add-a-note').value; - fetch('/note/create', { - method: 'POST', - headers: { - 'Content-Type': 'application/json' - }, - redirect: 'follow', - body: JSON.stringify({ note }) - }); - window.location.reload(); + if (note === '') return; + await postToServer('/note/create', {note: note}); + document.querySelector('#add-a-note').value = ''; + await getNotes(); } // eslint-disable-next-line async function deleteNote(note_id) { - await fetch('/note/delete', { - method: 'POST', - headers: { - 'Content-Type': 'application/json' - }, - redirect: 'follow', - body: JSON.stringify({ note: note_id }) - }); - document.querySelector('#notes').innerHTML = ''; - getNotes(); + await postToServer('/note/delete', {note: note_id}); + await getNotes(); } async function generateCategoryDropdown() { const response = await fetch('/public/js/call-categories.json'); + checkResult(response); const categories = await response.json(); for (const category of categories) { const option = document.createElement('option'); @@ -71,15 +60,30 @@ async function addCall() { const category = document.querySelector('#category').value; const response = document.querySelector('#response').value; const call_data = { prid, cc, driver, category, response }; - fetch('/call/create', { + await postToServer('/call/create',call_data); + document.querySelector('#prid').value = ''; + document.querySelector('#tic').value = ''; + document.querySelector('#driver').value = ''; + document.querySelector('#category').value = ''; + document.querySelector('#response').value = ''; +} + +async function postToServer(endpoint, body) { + const response = await fetch(endpoint, { method: 'POST', headers: { 'Content-Type': 'application/json' }, redirect: 'follow', - body: JSON.stringify(call_data) + body: JSON.stringify(body) }); - window.location.reload(); + checkResult(response); +} + +function checkResult(response) { + if (!response.ok) { + throw response.statusText; + } } getNotes();