-
Notifications
You must be signed in to change notification settings - Fork 7
/
editTasks.html
313 lines (284 loc) · 10.7 KB
/
editTasks.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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="icon" href="./timetable.png" type="image/x-icon" />
<link rel="manifest" href="./manifest.json" />
<link rel="apple-touch-icon" href="./timetable.png" />
<title>Tasks | Edit via JSON</title>
<!-- Include CodeMirror CSS -->
<link rel="stylesheet" href="./Assets/codemirror/codemirror.min.css">
<link rel="stylesheet" href="./Assets/codemirror/material.min.css">
<link rel="stylesheet" href="https://fonts.googleapis.com/css2?family=Material+Symbols+Outlined:opsz,wght,FILL,[email protected],100..700,0..1,-50..200" />
<style>
#user-info {
font-family: Verdana, Geneva, Tahoma, sans-serif;
color: black;
text-align: center;
}
b {
font-family: Verdana, Geneva, Tahoma, sans-serif;
color: black;
text-align: center;
}
#json-editor {
height: 80vh;
margin: 20px 0;
}
#save-button {
padding: 10px 20px;
font-size: 16px;
background-color: rgb(2, 185, 2);
color: #fff;
border: none;
border-radius: 5px;
cursor: pointer;
}
#cancel-button {
padding: 10px 20px;
font-size: 16px;
background-color: black;
color: #fff;
border: none;
border-radius: 5px;
cursor: pointer;
}
#save-button:hover {
transform: scale(1.05);
transition: all 0.2s ease-in;
}
#cancel-button:hover {
transform: scale(1.05);
transition: all 0.2s ease-in;
}
#use-gui {
padding: 10px 20px;
font-size: 16px;
background-color: rgb(254, 164, 38);
color: #fff;
border: none;
border-radius: 5px;
cursor: pointer;
}
#use-gui:hover {
transform: scale(1.05);
transition: all 0.2s ease-in;
}
::-webkit-scrollbar {
width: 10px;
}
/* Track */
::-webkit-scrollbar-track {
background: rgba(0, 0, 0, 0);
}
/* Handle */
::-webkit-scrollbar-thumb {
background: #888;
border-radius: 8px;
}
/* Handle on hover */
::-webkit-scrollbar-thumb:hover {
background: #555;
}
.btn-container {
display: flex;
justify-content: space-between;
}
.acc-container {
display: flex;
justify-content: center;
align-items: center;
}
.bubble {
margin-left: 10px;
background-color: #dbdbdb;
border-radius: 20px;
padding-top: 2px;
padding-bottom: 2px;
padding-left: 8px;
padding-right: 8px;
display: flex;
justify-content: center;
align-items: center;
height: 20px;
}
#loggedin {
height: fit-content;
}
.CodeMirror {
height: 100% !important;
}
</style>
</head>
<body>
<div class="acc-container">
<div id="loggedin"><b>Logged in as: </b></div>
<div class="bubble"><p id="user-info"></p></div>
</div>
<div id="json-editor"></div>
<div class="btn-container">
<button id="cancel-button">Go Back</button>
<button id="use-gui">Use GUI</button>
<button id="save-button">Save</button>
</div>
<!-- Include CodeMirror JS -->
<script src="./Assets/codemirror/codemirror.min.js"></script>
<script src="./Assets/codemirror/javascript.min.js"></script>
<script>
function getQueryParams() {
const params = {};
window.location.search.substr(1).split('&').forEach(param => {
const [key, value] = param.split('=');
params[key] = decodeURIComponent(value);
});
return params;
}
document.getElementById('cancel-button').addEventListener('click', () => {
window.location.href = './upcoming.html';
});
document.getElementById('use-gui').addEventListener('click', () => {
window.location.href = `./editTasksGUI.html?page=${getQueryParams().page}`;
});
// ----------------- Example content -----------------
// const content = `{
// "version": "1.0.0",
// "tasks": [
// {
// "id": 1,
// "title": "Task 1",
// "description": "Description of task 1",
// "status": "todo"
// },
// {
// "id": 2,
// "title": "Task 2",
// "description": "Description of task 2",
// "status": "in-progress"
// },
// {
// "id": 3,
// "title": "Task 3",
// "description": "Description of task 3",
// "status": "done"
// }
// ]
// }`
// const editor = CodeMirror(document.getElementById('json-editor'), {
// value: content,
// mode: 'javascript',
// theme: 'material',
// lineNumbers: true
// });
// ---------------------------------------------------
document.addEventListener('DOMContentLoaded', async () => {
const params = getQueryParams();
const params_accessToken = params.access_token;
const page = params.page;
let accessToken = null;
let sha;
let content;
let editor;
const backendUrl = 'https://timetable-oauth.vercel.app';
if (params_accessToken) {
accessToken = params_accessToken;
localStorage.setItem('accessToken', params_accessToken);
} else {
accessToken = localStorage.getItem('accessToken');
}
if (accessToken) {
// Fetch user info as an example
fetch('https://api.github.com/user', {
headers: {
'Authorization': `token ${accessToken}`
}
})
.then(response => {
// Redirect to login page if token is invalid
if (response.ok) {
return response.json();
} else {
localStorage.removeItem('accessToken');
window.location.href = `${backendUrl}/login?page=${page}`;
}
})
.then(data => {
document.getElementById('user-info').textContent = data.name;
});
// Fetch the file to get its current SHA
const owner = 'Abhinav-ark';
const repo = 'timetable_csea';
const filename = page === 'common' ? 'tasks.js' :
page === 'pe4' ? 'tasksPE4.js' :
page === 'pe5' ? 'tasksPE5.js' :
page === 'pe6' ? 'tasksPE6.js' :
'tasks.js';
const path = `tasks/${filename}`;
// Save button handler
document.getElementById('save-button').addEventListener('click', () => {
// yes or no prompt
if (confirm('Are you sure you want to save the file?')) {
const updatedContent = editor.getValue();
const encodedContent = btoa(updatedContent);
fetch(`https://api.github.com/repos/${owner}/${repo}/contents/${path}`, {
method: 'PUT',
headers: {
'Authorization': `token ${accessToken}`,
'Content-Type': 'application/json'
},
body: JSON.stringify({
message: 'Updated file from editor',
content: encodedContent,
sha: sha // Include the SHA of the file being updated
})
})
.then(response => response.json())
.then(async (data) => {
if (data.content) {
alert('File saved successfully. Please wait for 2 minutes for the changes to propagate.');
await fetchFile();
} else {
alert('Error saving file');
await fetchFile();
localStorage.removeItem('accessToken');
}
});
}
});
const fetchFile = async () => {
await fetch(`https://api.github.com/repos/${owner}/${repo}/contents/${path}`, {
headers: {
'Authorization': `token ${accessToken}`
}
})
.then(response => response.json())
.then(fileData => {
sha = fileData.sha;
content = atob(fileData.content);
// Check if editor is already initialized
if (!editor) {
// Initialize CodeMirror editor
editor = CodeMirror(document.getElementById('json-editor'), {
value: content,
mode: 'javascript',
theme: 'material',
lineNumbers: true
});
} else {
// Update the content of the existing editor
editor.setValue(content);
}
})
.catch(error => {
console.error('Error fetching file:', error);
alert('Error fetching file');
});
}
await fetchFile();
} else {
window.location.href = `${backendUrl}/login?page=${page}`;
}
});
</script>
</body>
</html>