aboutsummaryrefslogtreecommitdiff
path: root/resources/assets/javascripts/bootstrap/consultations.js
blob: 51ffa8530859a373efa7ade3f23c81123a9233c7 (plain)
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
import { $gettext } from '../lib/gettext';

$(document).on('click', '.consultation-delete-check:not(.ignore)', event => {
    const form       = $(event.target).closest('form');
    const checkboxes = form.find(':checkbox[name="slot-id[]"]:checked');
    const ids        = checkboxes.map((index, element) => element.value.split('-').pop()).get();

    if (!ids.length) {
        return false;
    }

    let requests = ids.map(id => {
        return STUDIP.jsonapi.withPromises().get(`consultation-slots/${id}/bookings`).then(response => response.data.length);
    });
    Promise.all(requests).then((...results) => {
        if (results.some(result => result > 0)) {
            $(event.target).addClass('ignore').click().removeClass('ignore');
        } else {
            STUDIP.Dialog.confirm($gettext('Wollen Sie diese Termine wirklich löschen?')).done(() => {
                $('<input type="hidden" name="delete" value="1"/>').appendTo(form);
                form.submit();
            });
        }
    });

    event.preventDefault();
});