forked from Rahela-HYF/class-7
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinit.js
103 lines (83 loc) · 3.17 KB
/
init.js
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
const init = async () => {
window.state = await fetch('./data/index.json')
.then(resp => resp.json())
.catch(err => {
console.log(err);
return err;
});
if (window.state instanceof Error) return;
state.students = await fetch('./data/students.json')
.then(resp => resp.json())
.catch(err => {
console.log(err);
return err;
});
if (window.state.students instanceof Error) return;
state.modules = await fetch('./data/modules.json')
.then(resp => resp.json())
.catch(err => {
console.log(err);
return err;
});
if (window.state.modules instanceof Error) return;
const pendingAssignments = state.modules.map(module => {
// use hard-coded assignments if they exist
// allowing class repos to "freeze" as module assignments change with time
if (module.projects || module.exercises || module.assessments) {
return Promise.resolve({})
}
if (module.status !== 'to do') {
const url = 'https://hackyourfuture.be/' + module.name + '/assignments.json'
// const url = `https://${state.userName}.github.io/${module.name}/assignments.json`
// https://stackoverflow.com/questions/43262121/trying-to-use-fetch-and-pass-in-mode-no-cors
return fetch(url)
// return fetch('https://cors-anywhere.herokuapp.com/' + url)
.then(res => res.json())
.then(assignments => Object.assign(module, assignments))
.catch(err => {
console.log(err)
return err;
});
} else {
return Promise.resolve({});
}
});
await Promise.all(pendingAssignments)
const urlParams = new URL(window.location.href).searchParams;
const studentParam = urlParams.get("student");
state.currentStudent = state.students
.find(student => student.name === studentParam)
|| state.students
.find(student => student.userName === studentParam);
const moduleParam = urlParams.get("module");
state.currentModule = state.modules
.find(module => module.name === moduleParam)
|| state.modules
.find(module => module.board === Number(moduleParam))
|| state.modules
.find(module => module.repo === moduleParam);
console.log('initial state:', state);
state.root = document.getElementById('root');
classOverview(state);
document.getElementById('class-name').innerHTML = state.repoName;
const repoButton = document.createElement('button');
repoButton.innerHTML = 'to class wiki';
const a = document.createElement('a');
a.href = "https://github.com/" + state.userName + "/" + state.repoName + '/wiki';
a.target = "_blank";
a.appendChild(repoButton);
document.getElementById('top-buttons').appendChild(a);
document.getElementById('bottom-buttons').appendChild(a.cloneNode(true));
document.getElementById('go-home-top').onclick = async () => {
document.getElementById('root').innerHTML = '';
state.currentModule = null;
state.currentStudent = null;
await classOverview(state);
}
document.getElementById('go-home-bottom').onclick = async () => {
document.getElementById('root').innerHTML = '';
state.currentModule = null;
state.currentStudent = null;
await classOverview(state);
}
};