summaryrefslogtreecommitdiffstats
path: root/src/wwwroot/scripts/cabins.js
blob: ad7a8704ad407260ccb27ee5dd33011735fc60ff (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
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: "&nbsp;",
        }
    ]
}).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");
            }
        })
    })
}