aboutsummaryrefslogtreecommitdiffstats
path: root/src/templates/account/resource_list.html
blob: f92f78e9c7f0afeada8c4d6743f22e34d2058eb0 (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
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
{% extends "base.html" %}
{% block content %}
<div class="card_container">
{% for resource in resources %}
    <div class="card">
        <div class="card-header">
            <h3>Resource {{resource.id}}</h3>
        </div>
        <div class="card-body p-4">
            <ul class="list-group">
                <li class="list-group-item">id: {{resource.id}}</li>
                <li class="list-group-item">name: {{resource.name}}</li>
                <li class="list-group-item">description: {{resource.description}}</li>
            </ul>
        </div>
        <div class="card-footer">
            <button
                class="btn btn-danger w-100"
                onclick='delete_resource({{resource.id}});'
                data-toggle="modal"
                data-target="#resModal"
            >Delete</button>
        </div>
    </div>
{% endfor %}
</div>
<script>
    var grb_mapping = {{grb_mapping|safe|default:"{}"}};
    var booking_mapping = {{booking_mapping|safe|default:"{}"}};
    var current_resource_id = -1;
    function delete_resource(resource_id) {
        document.getElementById("confirm_delete_button").removeAttribute("disabled");
        var configs = grb_mapping[resource_id];
        var warning = document.createTextNode("Are You Sure?");
        var warning_subtext = document.createTextNode("This cannot be undone");
        if(booking_mapping[resource_id]){
            var warning = document.createTextNode("This resource is being used. It cannot be deleted.");
            var warning_subtext = document.createTextNode("If your booking just ended, you may need to give us a few minutes to clean it up before this can be removed.");

            document.getElementById("confirm_delete_button").disabled = true;
        }
        else if(configs.length > 0) {
            list_configs(configs);
            warning_text = "Are You Sure? The following Configurations will also be deleted.";
            warning = document.createTextNode(warning_text);
        }

        current_resource_id = resource_id;
        set_modal_text(warning, warning_subtext);
    }

    function set_modal_text(title, text) {
        var clear = function(node) {
            while(node.lastChild) {
            node.removeChild(node.lastChild);
            }
        }
        var warning_title = document.getElementById("config_warning");
        var warning_text = document.getElementById("warning_subtext");

        clear(warning_title);
        clear(warning_text);

        warning_title.appendChild(title);
        warning_text.appendChild(text);
        document.getElementById('modal_warning').style['max-height'] = '0px';
    }

    function list_configs(configs) {
        var list = document.getElementById("config_list");
        for(var i=0; i<configs.length; i++){
            var str = configs[i].name;
            var list_item = document.createElement("LI");
            list_item.appendChild(document.createTextNode(str));
            list.appendChild(list_item);
        }
    }

    function submit_delete_form() {
        var ajaxForm = $("#res_delete_form");
        var formData = ajaxForm.serialize();
        req = new XMLHttpRequest();
        var url = "delete/" + current_resource_id;
        req.open("POST", url, true);
        req.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
        req.onerror = function() { alert("problem submitting form"); }
        req.send(formData);
    }
</script>
<div class="modal fade" id="resModal" tabindex="-1" role="dialog" aria-labelledby="my_modal" aria-hidden="true">
    <div class="modal-dialog" style="width: 450px;" role="document">
        <div class="modal-content">
            <div class="modal-header">
                <h4 class="modal-title" id="my_modal" style="display: inline; float: left;">Delete Resource?</h4>
                <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                    <span aria-hidden="true">&times;</span>
                </button>
            </div>
            <form id="res_delete_form">
                {% csrf_token %}
            </form>
            <div class="modal-footer">
                <button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
                <button type="button" class="btn btn-primary" onclick="document.getElementById('modal_warning').style['max-height'] = '500px';">Delete</button>
            </div>
            <div id="modal_warning" class="modal-footer" style="max-height:0px;" >
                <div style="text-align:center; margin: 5px">
                    <h3 id="config_warning">Are You Sure?</h3>
                    <p id="warning_subtext">This cannot be undone</p>
                    <ul id="config_list"></ul>
                    <button class="btn" onclick="document.getElementById('modal_warning').style['max-height'] = '0px';">Nevermind</button>
                    <button class="btn btn-danger" id="confirm_delete_button" data-dismiss="modal" onclick="submit_delete_form();">I'm Sure</button>
                </div>
        </div>
    </div>
</div>

{% endblock %}