diff --git a/web/static/css/skazanull.css b/web/static/css/skazanull.css index a8b5116..ed6692b 100644 --- a/web/static/css/skazanull.css +++ b/web/static/css/skazanull.css @@ -3,3 +3,12 @@ margin: 0 auto; max-width: 20%; } + +.quote-form { + position: absolute; + top: 0; + right: 0; + bottom: 0; + left: 0; + background-color: #0008; +} diff --git a/web/static/js/quotes.js b/web/static/js/quotes.js index 369c58d..3920f14 100644 --- a/web/static/js/quotes.js +++ b/web/static/js/quotes.js @@ -13,6 +13,34 @@ if (sorting == null) { sorting = "-datetime"; } +function datetimeToLocalISO(datetime) { + var options = { + year: "numeric", + month: "2-digit", + day: "2-digit", + hour: "2-digit", + minute: "2-digit", + second: "2-digit", + timeZoneName: "longOffset", + }; + + var formatter = new Intl.DateTimeFormat("sv-SE", options); + var date = new Date(datetime); + + return formatter + .formatToParts(date) + .map(({ type, value }) => { + if (type === "timeZoneName") { + return value.slice(3); + } else { + return value; + } + }) + .join("") + .replace(" ", "T") + .replace(" ", ""); +} + function escapedString(str) { return str .replace("&", "&") @@ -112,6 +140,45 @@ function reload() { load(); } +$(document).on("click", "#btn-add-open", function (e) { + now = new Date; + now = new Date(now.getTime() - now.getTimezoneOffset() * 60000); + $("#new-quote-datetime").val(now.toJSON().slice(0,19)); + $("#quote-creator").removeClass("hidden"); +}); + +$(document).on("click", "#btn-add-close", function (e) { + $("#quote-creator").addClass("hidden"); +}); + +$(document).on("submit", "#quote-create", function (e) { + e.preventDefault(); + formdata = $("#quote-create").serializeArray(); + data = {}; + $(formdata).each(function (index, obj) { + data[obj.name] = obj.value; + }); + data.datetime = datetimeToLocalISO(data.datetime); + $.ajax({ + url: "/api/quotes", + type: "POST", + contentType: "application/json", + data: JSON.stringify(data), + processData: false, + dataType: "json", + success: function (resp) { + $("#quote-creator").addClass("hidden"); + reload(); + $("#new-quote-text").val(""); + $("#new-quote-author").val(""); + }, + error: function (err) { + $("#quote-creator-error-message").text(err.responseJSON.error); + $("#quote-creator-error").removeClass("hidden"); + }, + }); +}); + $(window).on("load", function (e) { load(); }); diff --git a/web/templates/quotes.html b/web/templates/quotes.html index c37af5f..997d39c 100644 --- a/web/templates/quotes.html +++ b/web/templates/quotes.html @@ -59,6 +59,49 @@
+