summaryrefslogtreecommitdiff
path: root/templates/admin/changeMID.html
blob: 125bc4855139dd4fa9107a9b1272db04939edace (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 "layout.html" %}
{% load staticfiles %}

{% block content %}
<div class="container">
    <div class="row">
        {% include "account/sub_nav.html" %}
        <div class="span12">
            {% include "admin/sub_nav.html" %}

            Username: <br>

            <input type="text" id="username" list="users" placeholder="Enter Username">
            <datalist id="users"></datalist>

            <br><br>

            <select id="mid">
                {% for mid in mid_count %}
                <option id = "{{ mid.2 }}">
                    MID : {{ mid.0 }} &emsp;&emsp;USER COUNT : {{mid.1}}
                </option>
                {% endfor %}
            </select>
            <br>
            <button id="update-btn" class="btn btn-primary" onclick="updateMID()"> Update MID </button>
        </div>
    </div>

    <script type="text/javascript">
        
    function updateMID() {
        $("#update-btn").prop('disabled',true);
        var BASE_URL = window.location.origin + "/sbhs";
        var username = $("#username").val().split(" ")[0];
        var board_id = $("#mid").children(":selected").attr("id");

        if(username == "")
        {
            alert("Username field must not be empty");
            return;
        }

        var request = $.ajax({
            url:BASE_URL + "/admin/updatemid",
            method : "POST",
            data : {
                "username" : username,
                "board_id" : board_id
            }
        });

        request.done(function(data){
            if(data.status_code == 200) {
                alert(data.message);
                $("#update-btn").prop('disabled',false);
                sessionStorage.setItem("sbhsuser"+username, [username, board_id]);
                $("#username").val("");
                location.reload();
            }
            else {
                alert(data.message);
                $("#update-btn").prop('disabled',false);
            }                
        });
    }

    // for live search of usernames
    if (sessionStorage.getItem("fetchedUsers") == null) {
var BASE_URL = window.location.origin + "/sbhs";
        var request = $.ajax({
            url: BASE_URL + '/admin/getusers',
            method: 'POST'
        });

        request.done(function(data){
            if (data.status_code == 200) {
                sessionStorage.setItem("fetchedUsers", true);
                populateUsers(data.message);
                appendUsers();
            }
            else {
                console.log("Sorry. Unable to implement live search");
            }
        });
    }

    function populateUsers(data) {
        for (user of data) {
            sessionStorage.setItem("sbhsuser"+user[0], [user[0], user[1]]);
        } 
    }

    function appendUsers() {   
        if (sessionStorage.getItem("fetchedUsers")) {
            var i, matchedUsers = [];
            query = "sbhsuser";
            for (i in sessionStorage) {
              if (sessionStorage.hasOwnProperty(i)) {
                if (i.match(query)) {
                  value = sessionStorage.getItem(i);
                  matchedUsers.push(value);
                }
              }
            }
            options = "";
            for (matchedUser of matchedUsers) {
                options += "<option>" + matchedUser.split(",")[0] + "  (" + matchedUser.split(",")[1] + ")" + "</option>";
            }
            if (options.length != 0) {
                document.getElementById("users").innerHTML = options;
            }    
        }
    }
    appendUsers();
    </script>
</div>
{% endblock %}