-
Notifications
You must be signed in to change notification settings - Fork 5
/
grades.html
176 lines (169 loc) · 4.78 KB
/
grades.html
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
173
174
175
176
---
title: Grades
layout: default
---
{% raw %}
<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
<div id="app">
<a v-bind:href="loginUrl" v-if="loginUrl">Login</a>
<template v-if="loggedIn">
<a href="#" v-on:click="logout">Logout</a>
<assignment-list></assignment-list>
</template>
</div>
<script>
const service = location.origin + location.pathname;
const baseApiUrl = "https://cos316.princeton.edu/.grader/api/";
let accessToken = window.sessionStorage.accessToken;
function fetchApi(path, params) {
if (!params) { params = {} }
if (!params.headers) { params.headers = {}; }
if (accessToken) {
params.headers["Authorization"] = "Bearer " + accessToken;
}
return fetch(baseApiUrl + path, params);
}
</script>
<script type="text/x-template" id="assignmentList">
<div v-if="assignment">
<h2>
<a href="#" v-on:click="descend(false)">Assignments</a> >
{{ assignment.slug }}
</h2>
<repo-list :assignment="assignment.slug"></repo-list>
</div>
<ul v-else>
<li v-for="assignment in assignments" :key="assignment.id">
<a href="#" v-on:click="descend(assignment)">{{ assignment.slug }}</a>
</li>
</ul>
</script>
<script>
Vue.component('assignment-list', {
template: '#assignmentList',
data: function () {
return {
assignments: [],
assignment: false
}
},
created: async function() {
this.assignments = await (await fetchApi("assignments")).json();
},
methods: {
descend: function(assignment) {
this.assignment = assignment;
return false;
}
}
});
</script>
<script type="text/x-template" id="repositoriesList">
<div v-if="repository">
<a href="#" v-on:click="repository = false">← back</a>
<repo-details :repository="repository"></repo-details>
</div>
<div v-else>
<p>{{ repositories.length }}</p>
<ul>
<li v-for="repository in repositories" :key="repository.id">
<a href="#" v-on:click="descend(repository)">{{ repository.name }}</a>
</li>
</ul>
</div>
</script>
<script>
Vue.component('repo-list', {
template: '#repositoriesList',
props: ["assignment"],
data: function () {
return {
repositories: [],
repository: false
}
},
created: async function() {
this.repositories = await (await fetchApi("assignments/" + this.assignment + "/repositories")).json();
},
methods: {
descend: function(repository) {
this.repository = repository;
return false;
}
}
});
</script>
<script type="text/x-template" id="repositoryDetails">
<div>
<h4><a :href="'https://github.com/' + repository.name" target="_new">{{ repository.name }}</a></h4>
<ol>
<li v-for="grade in grades">{{ grade.submitted.toLocaleString() }}: {{(grade.grade * 100).toFixed(0)}}%</li>
</ol>
</div>
</script>
<script>
Vue.component('repo-details', {
template: '#repositoryDetails',
props: ["repository"],
data: function () {
return {
grades: {},
}
},
created: async function() {
this.grades = (await (await fetchApi("repositories/" + this.repository.id + "/grades")).json()).map(elm => {
elm.submitted = new Date(elm.submitted);
return elm;
});
},
methods: {
descend: function(repository) {
this.repository = repository;
return false;
}
}
});
</script>
<script>
var app = new Vue({
el: '#app',
data: {
loggedIn: !!accessToken,
loginUrl: false
},
created: async function() {
const ticket = new URLSearchParams(window.location.search).get("ticket");
if (ticket) {
const response = await fetchApi("", {
method: "POST",
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
},
body: "ticket=" + ticket + "&" + "service=" + service,
});
accessToken = (await response.json()).jwt;
window.sessionStorage.accessToken = accessToken;
window.history.replaceState({}, document.title, window.location.pathname);
this.loggedIn = !!accessToken;
} else {
const response = await fetchApi("");
if (!response.ok) {
this.loggedIn = false;
this.loginUrl = (await response.json()).cas_server + "/login?service=" + location.origin + location.pathname
}
}
},
methods: {
logout: async function() {
delete window.sessionStorage.accessToken;
accessToken = undefined;
this.loggedIn = false;
const response = await fetchApi("");
if (!response.ok) {
this.loginUrl = (await response.json()).cas_server + "/login?service=" + location.origin + location.pathname
}
}
}
})
</script>
{% endraw %}