diff options
| author | ivar <i@oiee.no> | 2025-10-19 23:41:23 +0200 |
|---|---|---|
| committer | ivar <i@oiee.no> | 2025-10-19 23:41:23 +0200 |
| commit | 3f4c0720e1e3421431e7baa20882a4a4512a7fab (patch) | |
| tree | 734ca81d7d0841d8863e3f523ebba14c282dc681 /src/wwwroot/scripts/cabins.js | |
| download | fagprove-master.tar.xz fagprove-master.zip | |
Diffstat (limited to 'src/wwwroot/scripts/cabins.js')
| -rw-r--r-- | src/wwwroot/scripts/cabins.js | 211 |
1 files changed, 211 insertions, 0 deletions
diff --git a/src/wwwroot/scripts/cabins.js b/src/wwwroot/scripts/cabins.js new file mode 100644 index 0000000..ad7a870 --- /dev/null +++ b/src/wwwroot/scripts/cabins.js @@ -0,0 +1,211 @@ +const cabinGrid = $("#cabinGrid"); +cabinGrid.kendoGrid({ + noRecords: { + template: "Fant ingen hytter" + }, + filterable: false, + sortable: false, + resizable: true, + scrollable: true, + messages: { + commands: { + cancel: "Avbryt endringer", + canceledit: "Avbryt", + create: "Legg til ny hytte", + destroy: "Slett", + edit: "Rediger", + save: "Lagre endringer", + select: "Velg", + update: "Oppdater" + } + }, + editable: false, + toolbar: "<button class='k-button k-button-icontext' id='newCabinButton'><span class='k-icon k-i-plus'></span>Legg til hytte</button>", + dataSource: { + transport: { + read: "/api/cabins", + parameterMap: function (data, type) { + if (data.email) { + if (!$.isEmail(data.email)) { + $.notificate("Ugyldig e-postadresse", "", "error", false); + throw new Error("Invalid email not going through with the request"); + } + } + switch (type) { + case "create": + return JSON.stringify({ + name: data.name, + email: data.email, + role: data.role + }); + case "read": + break; + default: + return JSON.stringify({ + name: data.name, + role: data.role, + email: data.email, + id: data.id + }); + } + } + }, + schema: { + model: { + id: "id", + fields: { + id: {editable: false}, + name: { + editable: true, + validation: {required: true} + }, + categoryId: { + editable: true, + validation: {required: true}, + }, + email: { + editable: true, + validation: {required: true} + } + } + } + }, + error: function (e) { + if (e.xhr.responseJSON.error) { + $.notificate("En feil oppstod", e.xhr.responseJSON.error, "error"); + } + }, + requestEnd: function (e) { + switch (e.type) { + case "create": + $.notificate( + "Bruker opprettet", + "En velkomstmail er sendt til " + e.response.email, + "success" + ); + usersGrid.dataSource.read(); + break; + case "update": + $.notificate( + "Bruker oppdatert", + "Brukeren er oppdatert", + "success" + ); + usersGrid.dataSource.read(); + break; + case "destroy": + $.notificate( + "Bruker slettet", + "Brukeren <b>" + e.response + "</b> er slettet.", + "success" + ); + usersGrid.dataSource.read(); + break; + } + }, + }, + columns: [ + { + field: "name", + title: "Navn", + filterable: { + multi: true, + search: true + } + }, + { + field: "categoryName", + title: "Hyttefelt" + }, + { + command: + { + template: $("#cabinRowCommandButtons").html() + }, + title: " ", + } + ] +}).data("kendoGrid"); + +const cabinModal = $("#cabinModal"); +$("#newCabinButton").on("click", function () { + cabinModal.html($("#newCabinInfoModalTemplate").html()).modal("show"); + $(".ui.selection.dropdown").dropdown(); + let form = $("#newCabinForm"); + $("#submitNewCabinForm").on("click", function (e) { + let values = form.form("get values"); + if (!values) return; + if (!values.name) { + $.notificate("Ugyldig", "Hytten trenger ett navn", "error"); + return; + } + if (!values.categoryId) { + $.notificate("Ugyldig", "Hytten trenger ett hyttefelt", "error"); + return; + } + values.capacity = parseInt(values.capacity); + $.ajax({ + data: JSON.stringify(values), + url: "/api/cabins/create", + contentType: "application/json", + processData: false, + method: "post", + success: function (e) { + cabinModal.modal("hide").empty(); + cabinGrid.data("kendoGrid").dataSource.read(); + console.log(e); + $.notificate("Opprettet ny hytte", "Hytten " + e + " er opprettet", "success"); + }, + error: function (e) { + $.notificate("En feil oppstod", "Vennligst prøv igjen senere", "error"); + } + }) + }); +}); + +function deleteCabin(e) { + let dataItem = cabinGrid.data("kendoGrid").dataItem($(e).closest("tr")); + if (confirm("Er du sikker på at du vil slette: " + dataItem.name + "?")) { + $.ajax({ + url: "/api/cabins/delete", + method: "delete", + data: JSON.stringify(dataItem), + contentType: "application/json", + processData: false, + success: function (e) { + cabinGrid.data("kendoGrid").dataSource.read(); + $.notificate("Hytte slettet", dataItem.name + " er slettet", "error"); + } + }) + } +} + +function openEditCabinModal(e) { + let dataItem = cabinGrid.data("kendoGrid").dataItem($(e).closest("tr")); + let template = kendo.template($("#editCabinInfoModalTemplate").html()); + let generatedHtml = template(dataItem); + cabinModal.html(generatedHtml).modal("show"); + $(".ui.selection.dropdown").dropdown("set selected", dataItem.categoryId); + $("#submitEditCabinForm").on("click", function (e) { + let form = $("#editCabinForm"); + let values = form.form("get values"); + if (!values) return; + if (!values.name) return; + values.capacity = parseInt(values.capacity); + $.ajax({ + url: "/api/cabins/update", + method: "put", + data: JSON.stringify(values), + contentType: "application/json", + processData: false, + success: function (e) { + cabinModal.modal("hide").empty(); + cabinGrid.data("kendoGrid").dataSource.read(); + $.notificate("Oppdatert", dataItem.name + " er oppdatert", "success"); + }, + error: function (e) { + $.notificate("En feil oppstod", e.error ? e.error : "Vennligst prøv igjen senere", "error"); + } + }) + }) +} |
