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 --- views/event.handlebars | 110 +++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 102 insertions(+), 8 deletions(-) (limited to 'views/event.handlebars') 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'); + }) }); -- cgit v1.2.3