summaryrefslogtreecommitdiff
path: root/dist/callback.html
blob: c01341670b37713ff9e8ab75dad94399faf6c23a (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
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
<!DOCTYPE html>
<html>

<head>
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <style>
        body {
            font-family: "Lato", sans-serif;
        }

        .sidenav {
            height: 100%;
            width: 160px;
            position: fixed;
            z-index: 1;
            top: 0;
            left: 0;
            background-color: #111;
            padding-top: 20px;
            overflow-wrap: break-word;
        }

        .sidenav a {
            padding: 6px 8px 6px 16px;
            text-decoration: none;
            font-size: 25px;
            color: #818181;
            display: block;
        }

        .sidenav h1 {
            padding: 6px 8px 6px 16px;
            text-decoration: none;
            font-size: 14px;
            color: #FFFFFF;
            display: block;
        }

        .sidenav a:hover {
            color: #f1f1f1;
        }

        .main {
            margin-left: 162px;
            /* Same as the width of the sidenav */
            font-size: 28px;
            /* Increased text to enable scrolling */
            padding: 0px 10px;
        }

        .out {
            margin-left: 162px;
            /* Same as the width of the sidenav */
            font-size: 16px;
            /* Increased text to enable scrolling */
        }

        @media screen and (max-height: 450px) {
            .sidenav {
                padding-top: 15px;
            }

            .sidenav a {
                font-size: 18px;
            }
        }
    </style>
</head>

<body onload="populate_users()">
    <div class="sidenav">
        <a>Current Users</a>
        <h1 id="userlist"></h1>
    </div>
    <div class="main">
        <h1>Spotify Intersect</h1>
        <input type="text" id="username" placeholder="Username">
        <button id="Connect_button" onclick="connect_button()">Connect to Spotify</button>
        <button id="Load_button" onclick="load_songs()" disabled=true>Load songs to database</button>
        <p id="status"></p>
        <input type="text" id="names" placeholder="Comma seperated list of usernames">
        <button id="Match_button" onclick="match_users()">Match users</button>
    </div>
    <div class="out">
        <p id="output"></p>
    </div>
    <script>
        //const host = "http://127.0.0.1:8000";
        //const api = host + "";
        const host = "https://kobert.dev";
        const api = host + "/spotify-api";
        let lobby = null;
        function getParameterByName(name) {
            let url = window.location.href;
            name = name.replace(/[\[\]]/g, "\\$&");
            var regex = new RegExp("[?&]" + name + "(=([^&#]*)|&|#|$)"),
                results = regex.exec(url);
            if (!results) return null;
            if (!results[2]) return '';
            return decodeURIComponent(results[2].replace(/\+/g, " "));
        }
        function connect_button() {
            //http://stackoverflow.com/questions/105034/how-to-create-a-guid-uuid-in-javascript
            let token = Math.random().toString(36).substring(2, 15) + Math.random().toString(36).substring(2, 15);
            sessionStorage.setItem('token', token);
            sessionStorage.setItem('lobby', lobby);
            document.getElementById("Connect_button").disabled = true;
            window.location = api + "/token/" +  document.getElementById("username").value + "/" + token;
        }
        function populate_users() {
            lobby = getParameterByName("lobby");
            if (!lobby) {
                lobby = sessionStorage.getItem('lobby');
            }
            let code = getParameterByName("code");
            if (code !== null) {
                document.getElementById("Connect_button").disabled = true;
                document.getElementById("Load_button").disabled = false;
                document.getElementById("username").disabled = false;
            }
            if (lobby === null) {
                document.getElementById("status").innerHTML = `Error: this is no valid lobby, please create a lobby first`;
                document.getElementById("Connect_button").disabled = true;
                document.getElementById("Load_button").disabled = true;
                document.getElementById("username").disabled = true;
                return;
            }
            fetch(api + "/users/" + lobby)
                .then(function (response) {
                    if (!response.ok) {
                        document.getElementById("status").innerHTML = `Error occured while loading songs: ${response.status}`;
                        throw new Error(`HTTP error! status: ${response.status}`);
                    }
                    response.text().then(function (message) {
                        document.getElementById("userlist").innerHTML = '<p>' + message.replace(/\n/g, '</p>\n<p>') + '</p>';
                    });
                });
        }
        function load_songs() {
            console.log("loading songs");
            document.getElementById("status").innerHTML = "Loading songs";
            document.getElementById("Load_button").disabled = true;
            let token = sessionStorage.getItem('token');
            fetch(api + `/callback/${token}/${lobby}/` + getParameterByName("code"))
                .then(function (response) {
                    if (!response.ok) {
                        document.getElementById("status").innerHTML = `Error occured while loading songs: ${response.status}`;
                        throw new Error(`HTTP error! status: ${response.status}`);
                    }
                    document.getElementById("status").innerHTML = "Loaded songs";
                    populate_users();
                });
            //window.location = api + "/token/" + token;
        }
        function match_users() {
            let names = document.getElementById("names").value;

            fetch(api + `/match/${lobby}/${names}`)
                .then(function (response) {
                    if (!response.ok) {
                        document.getElementById("status").innerHTML = `Error occured while loading songs: ${response.status}`;
                        throw new Error(`HTTP error! status: ${response.status}`);
                    }
                    response.text().then(function (message) {
                        document.getElementById("output").innerHTML = message.replace(/\n/g, '<br>\n');
                    });
                });
        }
    </script>
</body>

</html>