From 98cbc016ddf18a4f198330ef090b316e50950724 Mon Sep 17 00:00:00 2001 From: Raphael Kabo Date: Mon, 17 Jan 2022 10:41:07 +0000 Subject: Set up localStorage functionality and frontend --- public/js/util.js | 34 ++++++++ views/event.handlebars | 110 +++++++++++++++++++++++-- views/eventgroup.handlebars | 114 ++++++++++++++++++++++++-- views/layouts/main.handlebars | 1 + views/partials/editeventgroupmodal.handlebars | 14 +++- views/partials/editeventmodal.handlebars | 11 ++- 6 files changed, 263 insertions(+), 21 deletions(-) create mode 100644 public/js/util.js diff --git a/public/js/util.js b/public/js/util.js new file mode 100644 index 0000000..e2e9938 --- /dev/null +++ b/public/js/util.js @@ -0,0 +1,34 @@ +const getStoredToken = function(eventID) { + try { + let editTokens = JSON.parse(localStorage.getItem('editTokens')); + return editTokens[eventID]; + } catch(e) { + console.error(e); + localStorage.setItem('editTokens', JSON.stringify({})); + return false; + } +} + +const addStoredToken = function(eventID, token) { + try { + let editTokens = JSON.parse(localStorage.getItem('editTokens')); + editTokens[eventID] = token; + localStorage.setItem('editTokens', JSON.stringify(editTokens)); + } catch(e) { + console.error(e); + localStorage.setItem('editTokens', JSON.stringify({ [eventID]: token })); + return false; + } +} + +const removeStoredToken = function(eventID) { + try { + let editTokens = JSON.parse(localStorage.getItem('editTokens')); + delete editTokens[eventID]; + localStorage.setItem('editTokens', JSON.stringify(editTokens)); + } catch(e) { + console.error(e); + localStorage.setItem('editTokens', JSON.stringify({})); + return false; + } +} diff --git a/views/event.handlebars b/views/event.handlebars index 6a65f07..4fb1a46 100755 --- a/views/event.handlebars +++ b/views/event.handlebars @@ -5,16 +5,15 @@ {{/if}}
-

{{eventData.name}}

+

{{eventData.name}}

- {{#if editingEnabled}}
-
- - -
+ {{#if editingEnabled}} + + {{else}} + + {{/if}}
- {{/if}}
@@ -324,6 +323,36 @@
{{/if}} + + {{#if editingEnabled}} {{#unless eventHasConcluded}} {{> editeventmodal }} @@ -338,7 +367,7 @@
-
+ @@ -403,6 +432,47 @@ }) $(document).ready(function() { + // Save the editing token from the URL, if it is valid + const eventID = $('#eventName').attr('data-event-id'); + const urlParams = new URLSearchParams(window.location.search); + if (urlParams.has('e')) { + $.ajax({ + type: "POST", + url: `/verifytoken/event/${eventID}`, + data: { editToken: urlParams.get('e') }, + success: function(response, status, xhr) { + if (xhr.status === 200) { + addStoredToken(eventID, urlParams.get('e')); + } + }, + error: function(response, status, xhr) { + // The editing token is wrong - remove it + removeStoredToken(eventID); + window.location = window.location.pathname; + } + }); + } else if (getStoredToken(eventID)) { + const editToken = getStoredToken(eventID); + $.ajax({ + type: "POST", + url: `/verifytoken/event/${eventID}`, + data: { editToken }, + success: function(response, status, xhr) { + if (xhr.status === 200) { + window.location.search = `?e=${editToken}`; + } + }, + error: function(response, status, xhr) { + // The editing token is wrong - remove it + removeStoredToken(eventID); + } + }); + } + + if (urlParams.has('show_edit')) { + $('#editModal').modal('show'); + } + // From https://davidwalsh.name/javascript-download function downloadFile(data, fileName, type="text/plain") { // Create an invisible A element @@ -502,6 +572,30 @@ const passphrase = window.niceware.generatePassphrase(6).join('-'); modal.find('#removeAttendancePassword').val(passphrase); }); + + $('#verifyTokenForm').on('submit', function(e) { + e.preventDefault(); + let form = $(this); + $.ajax({ + type: "POST", + url: form.attr('action'), + data: form.serialize(), + success: function(response, status, xhr) { + if (xhr.status === 200) { + // Save the token to localStorage for later + addStoredToken($('#eventName').attr('data-event-id'), new FormData(form[0]).get('editToken')); + window.location.search = `?e=${new FormData(form[0]).get('editToken')}&show_edit=true`; + } + }, + error: function(response, status, xhr) { + form.find('.alert').text('That editing password is incorrect. Try again.').show(); + } + }); + }); + + $('#deleteEvent').on('click', function() { + $('#editModal').modal('hide'); + }) }); diff --git a/views/eventgroup.handlebars b/views/eventgroup.handlebars index 03a5b27..d7726bd 100755 --- a/views/eventgroup.handlebars +++ b/views/eventgroup.handlebars @@ -5,16 +5,15 @@ {{/if}}
-

{{eventGroupData.name}}

+

{{eventGroupData.name}}

- {{#if editingEnabled}}
-
- - -
+ {{#if editingEnabled}} + + {{else}} + + {{/if}}
- {{/if}}
{{#if firstLoad}} - +
diff --git a/views/partials/editeventmodal.handlebars b/views/partials/editeventmodal.handlebars index a1ccd83..b4b0ea6 100644 --- a/views/partials/editeventmodal.handlebars +++ b/views/partials/editeventmodal.handlebars @@ -1,5 +1,5 @@