Skip to content

Commit

Permalink
Add jquery-confirm, fix toasts/alerts, fix print in inventar_add + ot…
Browse files Browse the repository at this point in the history
…her boilerplate business
  • Loading branch information
sondregronas committed Aug 7, 2023
1 parent c9174ba commit 515f5d3
Show file tree
Hide file tree
Showing 16 changed files with 262 additions and 53 deletions.
9 changes: 8 additions & 1 deletion BookingSystem/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,13 @@ def get_items_overdue() -> flask.Response:
return flask.jsonify([item.api_repr() for item in items])


@api.route('/items/user/<userid>', methods=['GET'])
@login_required(admin_only=True, api=True)
def get_items_by_userid(userid: str) -> flask.Response:
items = inventory.get_all_unavailable()
return flask.jsonify([item.api_repr() for item in items if item.borrowed_to == userid])


@api.route('/items/add', methods=['POST'])
@login_required(admin_only=True)
def add_item() -> flask.Response:
Expand Down Expand Up @@ -132,7 +139,7 @@ def book_equipment() -> flask.Response:

for item in item_ids:
inventory.register_out(item_id=item, userid=userid, days=days)
return flask.Response('Utstyr ble utlevert.', status=200)
return flask.Response(f'Utstyret ble utlevert til {user.get(userid).get("name")}.', status=200)


@api.route('/return/<item_id>', methods=['POST'])
Expand Down
9 changes: 9 additions & 0 deletions BookingSystem/static/css/jquery-confirm.min.css

Large diffs are not rendered by default.

10 changes: 10 additions & 0 deletions BookingSystem/static/js/jquery-confirm.min.js

Large diffs are not rendered by default.

13 changes: 5 additions & 8 deletions BookingSystem/templates/admin_settings.html
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@ <h3>Dersom du er forvirret av denne siden, så kan du trygt forlate den :)</h3>
</hgroup>

{% if last_sent %}
<a href="" onclick="sendReport()">
<a onclick="sendReport()">
<button>Send rapport (Sist sendt {{ last_sent | strfunixtime }})</button>
</a>
{% else %}
<a href="" onclick="sendReport()">
<a onclick="sendReport()">
<button>Send rapport</button>
</a>
{% endif %}
Expand Down Expand Up @@ -41,9 +41,7 @@ <h3>En kategori per linje</h3>
let last_sent_day = new Date(last_sent * 1000).getDate();
let today = new Date().getDate();
if (last_sent_day === today) {
if (confirm("Det er allerede sendt ut rapport idag. Vil du likevel sende en ny?")) {
_send()
}
confirmMail("Bekreft utsendelse", "Det er allerede sendt ut rapport idag. Vil du likevel sende en ny?", _send)
} else {
_send()
}
Expand All @@ -54,11 +52,10 @@ <h3>En kategori per linje</h3>
url: "{{url_for('api.email_report')}}",
type: 'POST',
success: function (result) {
cueAlert('Sukksess!', 'Rapporten ble sendt ut.', 'success')
location.reload();
customAlert('Sukksess!', 'Rapporten ble sendt ut.', 'success')
},
error: function (result) {
cueAlert('Info', result.responseText, 'info')
customAlert('Info', result.responseText, 'info')
}
});
}
Expand Down
59 changes: 54 additions & 5 deletions BookingSystem/templates/booking.html
Original file line number Diff line number Diff line change
Expand Up @@ -33,21 +33,21 @@ <h3>Dersom du ikke finner eleven i listen, må eleven logge inn på nettstedet o
{% endfor %}
</select>

<br><br>

<button type="submit">Lån ut</button>
</form>

<table id="user-bookings"></table>

<script>
$(document).ready(function () {
$('#user').chosen(
$("#user").chosen(
{
no_results_text: 'Fant ikke brukere med navnet',
search_contains: true,
inherit_select_classes: true,
}
);
$('#equipment').chosen(
$("#equipment").chosen(
{
no_results_text: 'Fant ikke utstyr med navnet',
search_contains: true,
Expand All @@ -62,7 +62,6 @@ <h3>Dersom du ikke finner eleven i listen, må eleven logge inn på nettstedet o
data: $('form').serialize(),
type: 'POST',
success: function (response, status) {
// alert the response code
if (status === 'success') {
cueAlert('Sukksess!', response, 'success')
window.location.href = "{{ url_for('app.index') }}";
Expand All @@ -81,7 +80,57 @@ <h3>Dersom du ikke finner eleven i listen, må eleven logge inn på nettstedet o
}
});
});
});

let user = $('#user');
user.change(function () {
let table = $('#user-bookings');
table.empty();

let url = "{{ url_for('api.get_items_by_userid', userid = 'REPLACEME') }}";
url = url.replace('REPLACEME', user.val());
$.ajax({
url: url,
data: {user: user.val()},
type: 'GET',
success: function (response, status) {
// Spawn table, send info toast if user has items, send warning toast if user has overdue items
if (response.length > 0) {
let any_overdue = false;
table.append('<tr><th><b>Løpenummer</b></th><th><b>Utstyr</b></th><th><b>Kategori</b></th><th><b>Frist for levering</b></th></tr>');
for (let i = 0; i < response.length; i++) {
let item = response[i];
let due_date = new Date(item.order_due_date)
let overdue = due_date < new Date();
let row = $('<tr></tr>');
if (overdue) {
any_overdue = true;
row = $('<tr class="item-row--overdue"></tr>');
}
row.append('<td>' + item.id + '</td>');
row.append('<td>' + item.name + '</td>');
row.append('<td>' + item.category + '</td>');
row.append('<td>' + due_date.toLocaleDateString('nb-NO', {
day: '2-digit',
month: '2-digit',
year: 'numeric'
}) + '</td>');
table.append(row);
}
if (any_overdue) {
iziToast.warning({
title: 'Advarsel',
message: `Valgt bruker har utstyr som er over forfallsdato!`,
});
} else {
iziToast.info({
title: 'Info',
message: `Valgt bruker har aktive lån.`,
})
}
}
},
});
});
</script>

Expand Down
2 changes: 1 addition & 1 deletion BookingSystem/templates/forms/categories.html
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<form action="{{ url_for('api.update_categories') }}" method="post"
onclick="cueAlert('Sukksess!', 'Kategoriene ble oppdatert.', 'success')">
onclick="cueToast('Sukksess!', 'Kategoriene ble oppdatert.', 'success')">
<textarea name="categories" id="categories" cols="30" rows="10">{{ '\n'.join(all_categories) }}</textarea>
<input type="submit" value="Oppdater">
</form>
2 changes: 1 addition & 1 deletion BookingSystem/templates/forms/emails.html
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<form action="{{ url_for('api.update_emails') }}" method="post"
onclick="cueAlert('Sukksess!', 'E-postene ble oppdatert.', 'success')">
onclick="cueToast('Sukksess!', 'E-postene ble oppdatert.', 'success')">
<textarea name="emails" id="emails" cols="30" rows="10"
placeholder="Eksempel: [email protected]">{{ '\n'.join(all_emails) }}</textarea>
<input type="submit" value="Oppdater">
Expand Down
2 changes: 1 addition & 1 deletion BookingSystem/templates/forms/groups.html
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<form action="{{ url_for('api.update_groups') }}" method="post"
onclick="cueAlert('Sukksess!', 'Klasselisten ble oppdatert.', 'success')">
onclick="cueToast('Sukksess!', 'Klasselisten ble oppdatert.', 'success')">
<textarea name="groups" id="groups" cols="30" rows="10"
placeholder="Eksempel: 1IMA (Ola Nordmann)">{{ '\n'.join(all_groups) }}</textarea>
<input type="submit" value="Oppdater">
Expand Down
4 changes: 2 additions & 2 deletions BookingSystem/templates/inventar.html
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,11 @@ <h3>Her kan du legge til og endre inventaren. Du kan også skrive ut etiketter.<
<td>
{% if item.available %}
<a href="{{ url_for('app.edit_item', item_id=item.id) }}">Rediger</a>
|
<a href="{{ url_for('app.print_item', item_id=item.id) }}">Etikett</a>
{% else %}
Utlånt
{% endif %}
|
<a href="{{ url_for('app.print_item', item_id=item.id) }}">Etikett</a>
</td>
</tr>
{% endfor %}
Expand Down
53 changes: 28 additions & 25 deletions BookingSystem/templates/inventar_add.html
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,9 @@ <h3>Her kan du legge til nytt inventar. Legg gjerne til flere, så forlater du b
title: 'Suksess!',
message: response,
})
if ($('#print_label_count').val() > 0) {
sendToPrint();
}
},
error: function (response) {
iziToast.error({
Expand All @@ -82,34 +85,34 @@ <h3>Her kan du legge til nytt inventar. Legg gjerne til flere, så forlater du b
})
}
});
});
});

if ($('#print_label_count').val() > 0) {
url = '{{ url_for("api.print_label", item_id="REPLACE_ME") }}';
url = url.replace('REPLACE_ME', $('#id').val());
$.ajax({
type: 'POST',
url: url,
data: $('form').serialize(),
success: function (response) {
iziToast.info({
title: 'Utskrift',
message: response,
})
},
error: function (response) {
var msg = response.responseText;
if (response.status === 502) {
msg = "Klarte ikke koble til etikett tjeneren.";
}
iziToast.error({
title: 'Utskrift',
message: msg,
})
}
});
function sendToPrint() {
url = '{{ url_for("api.print_label", item_id="REPLACE_ME") }}';
url = url.replace('REPLACE_ME', $('#id').val());
$.ajax({
type: 'POST',
url: url,
data: $('form').serialize(),
success: function (response) {
iziToast.info({
title: 'Utskrift',
message: response,
})
},
error: function (response) {
var msg = response.responseText;
if (response.status === 502) {
msg = "Klarte ikke koble til etikett tjeneren.";
}
iziToast.error({
title: 'Utskrift',
message: msg,
})
}
});
});
}
</script>

{% endblock %}
6 changes: 3 additions & 3 deletions BookingSystem/templates/inventar_edit.html
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ <h3>Redigerer {{ item.id }} ({{ item.name }})</h3>
</form>

<button class="red-button"
onclick="if (confirm('Er du sikker på at du vil slette denne gjenstanden?')) deleteItem()">
onclick="confirmDelete('Bekreft', `Er du sikker på at du vil slette ${$('#id').val()}?`, deleteItem)">
Slett
</button>

Expand All @@ -49,7 +49,7 @@ <h3>Redigerer {{ item.id }} ({{ item.name }})</h3>
type: 'POST',
url: '{{ url_for("api.delete_item", item_id=item.id) }}',
success: function (response) {
cueAlert('Sukksess!', `${$('#id').val()} ble slettet.`, 'success');
cueToast('Sukksess!', `${$('#id').val()} ble slettet.`, 'success');
window.location.href = '{{ url_for("app.inventar") }}';
},
error: function (response) {
Expand All @@ -76,7 +76,7 @@ <h3>Redigerer {{ item.id }} ({{ item.name }})</h3>
url: '{{ url_for("api.edit_item", item_id=item.id) }}',
data: $('form').serialize(),
success: function (response) {
cueAlert('Sukksess!', `${$('#id').val()} ble oppdatert.`, 'success');
cueToast('Sukksess!', `${$('#id').val()} ble oppdatert.`, 'success');
window.location.href = '{{ url_for("app.inventar") }}';
},
error: function (response) {
Expand Down
4 changes: 2 additions & 2 deletions BookingSystem/templates/inventar_print.html
Original file line number Diff line number Diff line change
Expand Up @@ -50,15 +50,15 @@ <h3>For {{ item.id }} ({{ item.name }})</h3>
url: '{{ url_for("api.print_label", item_id=item.id) }}',
data: $('form').serialize(),
success: function (response) {
cueAlert('Utskrift', response.responseText, 'success')
cueToast('Utskrift', response.responseText, 'success')
window.location.href = '{{ url_for("app.inventar") }}';
},
error: function (response) {
var msg = response.responseText;
if (response.status === 502) {
msg = "Klarte ikke koble til etikett tjeneren.";
}
iziToast.error({
iziToast.warning({
title: 'Utskrift',
message: msg,
})
Expand Down
23 changes: 23 additions & 0 deletions BookingSystem/templates/partials/css.html
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{% include 'partials/css_student.html' %}
<link rel="stylesheet" href="/static/css/font-awesome.min.css">
<link rel="stylesheet" href="/static/css/iziToast.min.css">
<link rel="stylesheet" href="/static/css/jquery-confirm.min.css">

<style>
.red-button {
Expand Down Expand Up @@ -37,4 +38,26 @@
padding: 1rem;
z-index: 999;
}

.jconfirm.jconfirm-modern .jconfirm-box {
background-color: var(--card-background-color) !important;
}

.jconfirm-bg {
background-color: var(--background-color) !important;
}

.jconfirm-title {
color: var(--h1-color) !important;
}

.jconfirm-content {
color: var(--color) !important;
}

.jconfirm button {
width: auto !important;
}


</style>
2 changes: 2 additions & 0 deletions BookingSystem/templates/partials/head.html
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,7 @@
{% endif %}

<title>Utstyrbase</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta charset="utf-8">

{% block extra_head %}{% endblock %}
Loading

0 comments on commit 515f5d3

Please sign in to comment.