forked from QasimWani/LeetHub
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwelcome.js
355 lines (325 loc) · 11.5 KB
/
welcome.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
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
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
const option = () => {
return $('#type').val();
};
const repositoryName = () => {
return $('#name').val().trim();
};
/* Status codes for creating of repo */
const statusCode = (res, status, name) => {
switch (status) {
case 304:
$('#success').hide();
$('#error').text(
`Error creating ${name} - Unable to modify repository. Try again later!`,
);
$('#error').show();
break;
case 400:
$('#success').hide();
$('#error').text(
`Error creating ${name} - Bad POST request, make sure you're not overriding any existing scripts`,
);
$('#error').show();
break;
case 401:
$('#success').hide();
$('#error').text(
`Error creating ${name} - Unauthorized access to repo. Try again later!`,
);
$('#error').show();
break;
case 403:
$('#success').hide();
$('#error').text(
`Error creating ${name} - Forbidden access to repository. Try again later!`,
);
$('#error').show();
break;
case 422:
$('#success').hide();
$('#error').text(
`Error creating ${name} - Unprocessable Entity. Repository may have already been created. Try Linking instead (select 2nd option).`,
);
$('#error').show();
break;
default:
/* Change mode type to commit */
chrome.storage.local.set({ mode_type: 'commit' }, () => {
$('#error').hide();
$('#success').html(
`Successfully created <a target="blank" href="${res.html_url}">${name}</a>. Start <a href="http://leetcode.com">LeetCoding</a>!`,
);
$('#success').show();
$('#unlink').show();
/* Show new layout */
document.getElementById('hook_mode').style.display = 'none';
document.getElementById('commit_mode').style.display =
'inherit';
});
/* Set Repo Hook */
chrome.storage.local.set(
{ leethub_hook: res.full_name },
() => {
console.log('Successfully set new repo hook');
},
);
break;
}
};
const createRepo = (token, name) => {
const AUTHENTICATION_URL = 'https://api.github.com/user/repos';
let data = {
name,
private: true,
auto_init: true,
description:
'Collection of LeetCode questions to ace the coding interview! - Created using [LeetHub](https://github.com/QasimWani/LeetHub)',
};
data = JSON.stringify(data);
const xhr = new XMLHttpRequest();
xhr.addEventListener('readystatechange', function () {
if (xhr.readyState === 4) {
statusCode(JSON.parse(xhr.responseText), xhr.status, name);
}
});
xhr.open('POST', AUTHENTICATION_URL, true);
xhr.setRequestHeader('Authorization', `token ${token}`);
xhr.setRequestHeader('Accept', 'application/vnd.github.v3+json');
xhr.send(data);
};
/* Status codes for linking of repo */
const linkStatusCode = (status, name) => {
let bool = false;
switch (status) {
case 301:
$('#success').hide();
$('#error').html(
`Error linking <a target="blank" href="${`https://github.com/${name}`}">${name}</a> to LeetHub. <br> This repository has been moved permenantly. Try creating a new one.`,
);
$('#error').show();
break;
case 403:
$('#success').hide();
$('#error').html(
`Error linking <a target="blank" href="${`https://github.com/${name}`}">${name}</a> to LeetHub. <br> Forbidden action. Please make sure you have the right access to this repository.`,
);
$('#error').show();
break;
case 404:
$('#success').hide();
$('#error').html(
`Error linking <a target="blank" href="${`https://github.com/${name}`}">${name}</a> to LeetHub. <br> Resource not found. Make sure you enter the right repository name.`,
);
$('#error').show();
break;
default:
bool = true;
break;
}
$('#unlink').show();
return bool;
};
/*
Method for linking hook with an existing repository
Steps:
1. Check if existing repository exists and the user has write access to it.
2. Link Hook to it (chrome Storage).
*/
const linkRepo = (token, name) => {
const AUTHENTICATION_URL = `https://api.github.com/repos/${name}`;
const xhr = new XMLHttpRequest();
xhr.addEventListener('readystatechange', function () {
if (xhr.readyState === 4) {
const res = JSON.parse(xhr.responseText);
const bool = linkStatusCode(xhr.status, name);
if (xhr.status === 200) {
// BUG FIX
if (!bool) {
// unable to gain access to repo in commit mode. Must switch to hook mode.
/* Set mode type to hook */
chrome.storage.local.set({ mode_type: 'hook' }, () => {
console.log(`Error linking ${name} to LeetHub`);
});
/* Set Repo Hook to NONE */
chrome.storage.local.set({ leethub_hook: null }, () => {
console.log('Defaulted repo hook to NONE');
});
/* Hide accordingly */
document.getElementById('hook_mode').style.display =
'inherit';
document.getElementById('commit_mode').style.display =
'none';
} else {
/* Change mode type to commit */
/* Save repo url to chrome storage */
chrome.storage.local.set(
{ mode_type: 'commit', repo: res.html_url },
() => {
$('#error').hide();
$('#success').html(
`Successfully linked <a target="blank" href="${res.html_url}">${name}</a> to LeetHub. Start <a href="http://leetcode.com">LeetCoding</a> now!`,
);
$('#success').show();
$('#unlink').show();
},
);
/* Set Repo Hook */
chrome.storage.local.set(
{ leethub_hook: res.full_name },
() => {
console.log('Successfully set new repo hook');
/* Get problems solved count */
chrome.storage.local.get('stats', (psolved) => {
const { stats } = psolved;
if (stats && stats.solved) {
$('#p_solved').text(stats.solved);
$('#p_solved_easy').text(stats.easy);
$('#p_solved_medium').text(stats.medium);
$('#p_solved_hard').text(stats.hard);
}
});
},
);
/* Hide accordingly */
document.getElementById('hook_mode').style.display = 'none';
document.getElementById('commit_mode').style.display =
'inherit';
}
}
}
});
xhr.open('GET', AUTHENTICATION_URL, true);
xhr.setRequestHeader('Authorization', `token ${token}`);
xhr.setRequestHeader('Accept', 'application/vnd.github.v3+json');
xhr.send();
};
const unlinkRepo = () => {
/* Set mode type to hook */
chrome.storage.local.set({ mode_type: 'hook' }, () => {
console.log(`Unlinking repo`);
});
/* Set Repo Hook to NONE */
chrome.storage.local.set({ leethub_hook: null }, () => {
console.log('Defaulted repo hook to NONE');
});
/* Hide accordingly */
document.getElementById('hook_mode').style.display = 'inherit';
document.getElementById('commit_mode').style.display = 'none';
};
/* Check for value of select tag, Get Started disabled by default */
$('#type').on('change', function () {
const valueSelected = this.value;
if (valueSelected) {
$('#hook_button').attr('disabled', false);
} else {
$('#hook_button').attr('disabled', true);
}
});
$('#hook_button').on('click', () => {
/* on click should generate: 1) option 2) repository name */
if (!option()) {
$('#error').text(
'No option selected - Pick an option from dropdown menu below that best suits you!',
);
$('#error').show();
} else if (!repositoryName()) {
$('#error').text(
'No repository name added - Enter the name of your repository!',
);
$('#name').focus();
$('#error').show();
} else {
$('#error').hide();
$('#success').text('Attempting to create Hook... Please wait.');
$('#success').show();
/*
Perform processing
- step 1: Check if current stage === hook.
- step 2: store repo name as repoName in chrome storage.
- step 3: if (1), POST request to repoName (iff option = create new repo) ; else display error message.
- step 4: if proceed from 3, hide hook_mode and display commit_mode (show stats e.g: files pushed/questions-solved/leaderboard)
*/
chrome.storage.local.get('leethub_token', (data) => {
const token = data.leethub_token;
if (token === null || token === undefined) {
/* Not authorized yet. */
$('#error').text(
'Authorization error - Grant LeetHub access to your GitHub account to continue (launch extension to proceed)',
);
$('#error').show();
$('#success').hide();
} else if (option() === 'new') {
createRepo(token, repositoryName());
} else {
chrome.storage.local.get('leethub_username', (data2) => {
const username = data2.leethub_username;
if (!username) {
/* Improper authorization. */
$('#error').text(
'Improper Authorization error - Grant LeetHub access to your GitHub account to continue (launch extension to proceed)',
);
$('#error').show();
$('#success').hide();
} else {
linkRepo(token, `${username}/${repositoryName()}`, false);
}
});
}
});
}
});
$('#unlink a').on('click', () => {
unlinkRepo();
$('#unlink').hide();
$('#success').text(
'Successfully unlinked your current git repo. Please create/link a new hook.',
);
});
/* Detect mode type */
chrome.storage.local.get('mode_type', (data) => {
const mode = data.mode_type;
if (mode && mode === 'commit') {
/* Check if still access to repo */
chrome.storage.local.get('leethub_token', (data2) => {
const token = data2.leethub_token;
if (token === null || token === undefined) {
/* Not authorized yet. */
$('#error').text(
'Authorization error - Grant LeetHub access to your GitHub account to continue (click LeetHub extension on the top right to proceed)',
);
$('#error').show();
$('#success').hide();
/* Hide accordingly */
document.getElementById('hook_mode').style.display =
'inherit';
document.getElementById('commit_mode').style.display = 'none';
} else {
/* Get access to repo */
chrome.storage.local.get('leethub_hook', (repoName) => {
const hook = repoName.leethub_hook;
if (!hook) {
/* Not authorized yet. */
$('#error').text(
'Improper Authorization error - Grant LeetHub access to your GitHub account to continue (click LeetHub extension on the top right to proceed)',
);
$('#error').show();
$('#success').hide();
/* Hide accordingly */
document.getElementById('hook_mode').style.display =
'inherit';
document.getElementById('commit_mode').style.display =
'none';
} else {
/* Username exists, at least in storage. Confirm this */
linkRepo(token, hook);
}
});
}
});
document.getElementById('hook_mode').style.display = 'none';
document.getElementById('commit_mode').style.display = 'inherit';
} else {
document.getElementById('hook_mode').style.display = 'inherit';
document.getElementById('commit_mode').style.display = 'none';
}
});