Commit c1502b82 authored by Ayush's avatar Ayush

Refactor and optimize the code

parent f0c56e5d
import { Injectable, Output, EventEmitter } from '@angular/core'; import {EventEmitter, Injectable, Output} from '@angular/core';
import { map } from 'rxjs/operators'; import {map} from 'rxjs/operators';
import { HttpClient } from '@angular/common/http'; import {HttpClient} from '@angular/common/http';
import {Observable} from 'rxjs';
@Injectable({ @Injectable({
providedIn: 'root' providedIn: 'root'
...@@ -8,41 +9,46 @@ import { HttpClient } from '@angular/common/http'; ...@@ -8,41 +9,46 @@ import { HttpClient } from '@angular/common/http';
export class ApiService { export class ApiService {
redirectUrl: string; redirectUrl: string;
baseUrl: string = "http://localhost/sfcode/backend/"; baseUrl = 'http://localhost/sfcode/backend/';
@Output() getLoggedInState: EventEmitter<any> = new EventEmitter(); @Output() getLoggedInState: EventEmitter<any> = new EventEmitter();
constructor(private httpClient: HttpClient) { }
public userLogin(username: string, password: string) { constructor(private httpClient: HttpClient) {
return this.httpClient.post<any>(this.baseUrl + '/login.php', { username, password }) }
public userLogin(username: string, password: string): Observable<any> {
return this.httpClient.post<any>(this.baseUrl + '/login.php', {username, password})
.pipe(map(user => { .pipe(map(user => {
if (user.length == 0) throw new Error(); if (user.length === 0) {
throw new Error();
}
this.setToken(JSON.stringify(user)); this.setToken(JSON.stringify(user));
this.getLoggedInState.emit(true); this.getLoggedInState.emit(true);
return user; return user;
})); }));
} }
public userReg(name, email, pwd, username) { public userReg(name, email, pwd, username): Observable<any> {
return this.httpClient.post<any>(this.baseUrl + '/register.php', { name, email, pwd, username }) return this.httpClient.post<any>(this.baseUrl + '/register.php', {name, email, pwd, username})
.pipe(map(user => { .pipe(map(user => {
return user; return user;
})); }));
} }
setToken(token: string) { setToken(token: string): void {
localStorage.setItem('sfcode_user_token_2n1289bpxd', token); localStorage.setItem('sfcode_user_token_2n1289bpxd', token);
} }
getToken() {
getToken(): string {
return localStorage.getItem('sfcode_user_token_2n1289bpxd'); return localStorage.getItem('sfcode_user_token_2n1289bpxd');
} }
deleteToken() {
deleteToken(): void {
localStorage.removeItem('sfcode_user_token_2n1289bpxd'); localStorage.removeItem('sfcode_user_token_2n1289bpxd');
} }
isLoggedIn() {
isLoggedIn(): boolean {
const userToken = this.getToken(); const userToken = this.getToken();
if (userToken != null) { return userToken != null;
return true
}
return false;
} }
} }
...@@ -7,5 +7,6 @@ ...@@ -7,5 +7,6 @@
<button id="try">Try Code</button> <button id="try">Try Code</button>
<button id="submit">Submit Code</button> <button id="submit">Submit Code</button>
</div> </div>
<label for="editor"></label>
<textarea name="editor" id="editor"></textarea> <textarea name="editor" id="editor"></textarea>
</div> </div>
...@@ -50,6 +50,10 @@ ...@@ -50,6 +50,10 @@
} }
} }
label {
display: none;
}
@media screen and (max-width: 1000px) { @media screen and (max-width: 1000px) {
.container { .container {
padding: 20px 0; padding: 20px 0;
......
import { Component, OnInit } from '@angular/core'; import {Component, OnInit} from '@angular/core';
import { Router } from '@angular/router'; import {Router} from '@angular/router';
import { Problem } from '../problem'; import {Problem} from '../problem';
import { ProblemService } from '../problem.service'; import {ProblemService} from '../problem.service';
declare const CodeMirror: any; declare const CodeMirror: any;
@Component({ @Component({
...@@ -11,42 +12,44 @@ declare const CodeMirror: any; ...@@ -11,42 +12,44 @@ declare const CodeMirror: any;
}) })
export class ArenaComponent implements OnInit { export class ArenaComponent implements OnInit {
constructor(public router: Router, private problemService: ProblemService) { }
id: number; id: number;
problem: Problem; problem: Problem;
constructor(public router: Router, private problemService: ProblemService) {
}
ngOnInit(): void { ngOnInit(): void {
this.id = parseInt(this.router.url.split('/')[2]); this.id = parseInt(this.router.url.split('/')[2], 10);
this.getProblem(); this.getProblem();
var editor_area = document.getElementById('editor'); const editorArea = document.getElementById('editor');
var editor = CodeMirror.fromTextArea(editor_area, { const editor = CodeMirror.fromTextArea(editorArea as HTMLTextAreaElement, {
lineNumbers: true, lineNumbers: true,
theme: "material-ocean", theme: 'material-ocean',
mode: "text/x-c++src", mode: 'text/x-c++src',
autoCloseBrackets: true, autoCloseBrackets: true,
matchBrackets: true matchBrackets: true
}); });
editor.setSize("auto", "70vh"); editor.setSize('auto', '70vh');
editor.setValue(this.problem.default_code[0]); editor.setValue(this.problem.default_code[0]);
Object.assign((document.getElementsByClassName('CodeMirror')[0] as HTMLTextAreaElement).style, { Object.assign((document.getElementsByClassName('CodeMirror')[0] as HTMLTextAreaElement).style, {
border: "2px solid #ddf", border: '2px solid #ddf',
padding: "20px", padding: '20px',
fontFamily: "\"Anonymous Pro\", monospace", fontFamily: '"Anonymous Pro", monospace',
}); });
var active_lang = 0; let activeLang = 0;
var langs = ["C++", "Python", "Java"]; const langs = ['C++', 'Python', 'Java'];
var langs_mime = ["text/x-c++src", "text/x-python", "text/x-java"]; const langsMime = ['text/x-c++src', 'text/x-python', 'text/x-java'];
var problem = this.problem; const problem = this.problem;
document.getElementById('toggle-lang').onclick = function () { document.getElementById('toggle-lang').onclick = function(): void {
let th = this as HTMLDivElement; const th = this as HTMLDivElement;
active_lang = (active_lang + 1) % 3; activeLang = (activeLang + 1) % 3;
th.innerHTML = langs[active_lang]; th.innerHTML = langs[activeLang];
editor.setValue(problem.default_code[active_lang]); editor.setValue(problem.default_code[activeLang]);
editor.setOption("mode", langs_mime[active_lang]); editor.setOption('mode', langsMime[activeLang]);
} };
} }
...@@ -54,7 +57,7 @@ export class ArenaComponent implements OnInit { ...@@ -54,7 +57,7 @@ export class ArenaComponent implements OnInit {
this.problemService.getProblems() this.problemService.getProblems()
.subscribe(problems => { .subscribe(problems => {
this.problem = problems.find(i => i.id === this.id); this.problem = problems.find(i => i.id === this.id);
}) });
} }
} }
<div id="count">{{problems.length}} problems available!</div> <div id="count">{{problems.length}} problems available!</div>
<div class="card" *ngFor="let problem of problems"> <div *ngFor="let problem of problems" class="card">
<div class="title">{{problem.title}}</div> <div class="title">{{problem.title}}</div>
<div class="details">{{problem.details}}</div> <div class="details">{{problem.details}}</div>
<button class="attempt" [routerLink]="['/arena', problem.id]">Attempt</button> <button [routerLink]="['/arena', problem.id]" class="attempt">Attempt</button>
<span class="stats"><span *ngIf="problem.n_attempts != 0">{{problem.n_correct}} accepted out of <span class="stats"><span *ngIf="problem.n_attempts != 0">{{problem.n_correct}} accepted out of
{{problem.n_attempts}} {{problem.n_attempts}}
({{problem.n_correct / problem.n_attempts * 100}}%)</span><span *ngIf="problem.n_attempts === 0">No attempt ({{problem.n_correct / problem.n_attempts * 100}}%)</span><span *ngIf="problem.n_attempts === 0">No attempt
......
...@@ -4,5 +4,6 @@ ...@@ -4,5 +4,6 @@
<button id="input">Input</button> <button id="input">Input</button>
<button id="run">Run Code</button> <button id="run">Run Code</button>
</div> </div>
<label for="editor"></label>
<textarea name="editor" id="editor"></textarea> <textarea name="editor" id="editor"></textarea>
</div> </div>
...@@ -4,15 +4,19 @@ ...@@ -4,15 +4,19 @@
#toggle-lang { #toggle-lang {
position: absolute; position: absolute;
background: var(--color); background: var(--bgcolor);
color: var(--bgcolor); color: var(--color);
padding: 5px 10px; padding: 5px 10px;
right: 20px; right: 22px;
top: 20px; top: 22px;
z-index: 100; z-index: 100;
cursor: pointer; cursor: pointer;
} }
label {
display: none;
}
#attempt { #attempt {
position: absolute; position: absolute;
right: 24px; right: 24px;
......
...@@ -12,25 +12,25 @@ export class IdeComponent implements OnInit { ...@@ -12,25 +12,25 @@ export class IdeComponent implements OnInit {
constructor(public router: Router) { } constructor(public router: Router) { }
ngOnInit(): void { ngOnInit(): void {
var editor_area = document.getElementById('editor'); const editorArea = document.getElementById('editor');
var editor = CodeMirror.fromTextArea(editor_area, { const editor = CodeMirror.fromTextArea(editorArea as HTMLTextAreaElement, {
lineNumbers: true, lineNumbers: true,
theme: "material-ocean", theme: 'material-ocean',
mode: "text/x-c++src", mode: 'text/x-c++src',
autoCloseBrackets: true, autoCloseBrackets: true,
matchBrackets: true matchBrackets: true
}); });
editor.setSize("auto", "70vh"); editor.setSize('auto', '70vh');
Object.assign((document.getElementsByClassName('CodeMirror')[0] as HTMLTextAreaElement).style, { Object.assign((document.getElementsByClassName('CodeMirror')[0] as HTMLTextAreaElement).style, {
border: "2px solid #ddf", border: '2px solid #ddf',
padding: "20px", padding: '20px',
fontFamily: "\"Anonymous Pro\", monospace", fontFamily: '"Anonymous Pro", monospace',
}); });
var active_lang = 0; let activeLang = 0;
var langs = ["C++", "Python", "Java"]; const langs = ['C++', 'Python', 'Java'];
var langs_mime = ["text/x-c++src", "text/x-python", "text/x-java"]; const langsMime = ['text/x-c++src', 'text/x-python', 'text/x-java'];
var default_code = [`#include <iostream> const defaultCode = [`#include <iostream>
using namespace std; using namespace std;
int main() { int main() {
...@@ -40,16 +40,16 @@ int main() { ...@@ -40,16 +40,16 @@ int main() {
public static void main(String[] args) { public static void main(String[] args) {
System.out.println("Hello, World!"); System.out.println("Hello, World!");
} }
}`] }`];
editor.setValue(default_code[active_lang]); editor.setValue(defaultCode[activeLang]);
document.getElementById('toggle-lang').onclick = function () { document.getElementById('toggle-lang').onclick = function(): void {
let th = this as HTMLDivElement; const th = this as HTMLDivElement;
active_lang = (active_lang + 1) % 3; activeLang = (activeLang + 1) % 3;
th.innerHTML = langs[active_lang]; th.innerHTML = langs[activeLang];
editor.setValue(default_code[active_lang]); editor.setValue(defaultCode[activeLang]);
editor.setOption("mode", langs_mime[active_lang]); editor.setOption('mode', langsMime[activeLang]);
} };
} }
......
...@@ -13,17 +13,17 @@ export class LDModeComponent implements OnInit { ...@@ -13,17 +13,17 @@ export class LDModeComponent implements OnInit {
ngOnInit(): void { ngOnInit(): void {
this.currentTheme = 0; this.currentTheme = 0;
var btn = document.getElementById('button') as HTMLButtonElement; const btn = document.getElementById('button') as HTMLButtonElement;
btn.onclick = () => { btn.onclick = () => {
this.currentTheme = 1 - this.currentTheme; this.currentTheme = 1 - this.currentTheme;
document.querySelector('body').classList.toggle('light'); document.querySelector('body').classList.toggle('light');
if (btn.innerHTML === 'Light') btn.innerHTML = 'Dark'; if (btn.innerHTML === 'Light') { btn.innerHTML = 'Dark'; }
else btn.innerHTML = 'Light'; else { btn.innerHTML = 'Light'; }
var str = this.currentTheme == 1 ? "2px solid #112" : "2px solid #ddf"; const str = this.currentTheme === 1 ? '2px solid #112' : '2px solid #ddf';
Object.assign((document.getElementsByClassName('CodeMirror')[0] as HTMLTextAreaElement).style, { Object.assign((document.getElementsByClassName('CodeMirror')[0] as HTMLTextAreaElement).style, {
border: str border: str
}); });
} };
} }
} }
import { Component, OnInit } from '@angular/core'; import {Component, OnInit} from '@angular/core';
import { FormGroup, FormBuilder, Validators, NgForm } from '@angular/forms'; import {AbstractControl, FormBuilder, FormGroup, Validators} from '@angular/forms';
import { Router } from '@angular/router'; import {Router} from '@angular/router';
import { first } from 'rxjs/operators'; import {first} from 'rxjs/operators';
import { ApiService } from '../api.service'; import {ApiService} from '../api.service';
@Component({ @Component({
selector: 'app-login', selector: 'app-login',
...@@ -13,8 +13,9 @@ export class LoginComponent implements OnInit { ...@@ -13,8 +13,9 @@ export class LoginComponent implements OnInit {
form: FormGroup; form: FormGroup;
timeout: any; timeout: any;
user_data: any; userData: any;
submit_element: HTMLInputElement; submitElement: HTMLInputElement;
set: NodeListOf<HTMLInputElement>;
constructor(private fb: FormBuilder, private dataService: ApiService, private router: Router) { constructor(private fb: FormBuilder, private dataService: ApiService, private router: Router) {
this.form = this.fb.group({ this.form = this.fb.group({
...@@ -23,58 +24,68 @@ export class LoginComponent implements OnInit { ...@@ -23,58 +24,68 @@ export class LoginComponent implements OnInit {
}); });
} }
set: NodeListOf<HTMLInputElement>; get username(): AbstractControl {
return this.form.get('username');
}
get password(): AbstractControl {
return this.form.get('password');
}
ngOnInit(): void { ngOnInit(): void {
this.submit_element = document.getElementById('submit') as HTMLInputElement; this.submitElement = document.getElementById('submit') as HTMLInputElement;
this.set = document.querySelectorAll('input:not([type=submit])'); this.set = document.querySelectorAll('input:not([type=submit])');
this.set.forEach(function (item) { this.set.forEach(item => {
item.addEventListener('focus', function () { item.addEventListener('focus', () => {
item.parentElement.querySelector('label').classList.add('active'); item.parentElement.querySelector('label').classList.add('active');
}); });
item.addEventListener('blur', function () { item.addEventListener('blur', () => {
if (item.value == "") if (item.value === '') {
item.parentElement.querySelector('label').classList.remove('active'); item.parentElement.querySelector('label').classList.remove('active');
}
});
}); });
})
} }
updateLabels() { updateLabels(): void {
this.set.forEach(function (item) { this.set.forEach(item => {
if (item.value !== "" && item.value !== null) if (item.value !== '' && item.value !== null) {
item.parentElement.querySelector('label').classList.add('active'); item.parentElement.querySelector('label').classList.add('active');
else } else {
item.parentElement.querySelector('label').classList.remove('active'); item.parentElement.querySelector('label').classList.remove('active');
}
}); });
} }
postData(form: FormGroup) { postData(form: FormGroup): void {
if (this.submit_element.classList.contains('disabled')) return; if (this.submitElement.classList.contains('disabled')) {
this.submit_element.classList.add('disabled'); return;
}
this.submitElement.classList.add('disabled');
this.dataService.userLogin(form.value.username, form.value.password) this.dataService.userLogin(form.value.username, form.value.password)
.pipe(first()) .pipe(first())
.subscribe(data => { .subscribe(data => {
this.user_data = data; this.userData = data;
const redirect = this.dataService.redirectUrl ? this.dataService.redirectUrl : '/home'; const redirect = this.dataService.redirectUrl ? this.dataService.redirectUrl : '/home';
this.router.navigate([redirect]); this.router.navigate([redirect]).then();
}, error => { }, () => {
this.error("Incorrect username or password."); this.error('Incorrect username or password.');
form.get('password').reset(); form.get('password').reset();
}); });
} }
error(str: string) { error(str: string): void {
let co = (document.getElementById('cover') as HTMLDivElement); const co = (document.getElementById('cover') as HTMLDivElement);
let pu = (document.getElementById('error') as HTMLDivElement); const pu = (document.getElementById('error') as HTMLDivElement);
pu.querySelector('.desc').innerHTML = str; pu.querySelector('.desc').innerHTML = str;
co.querySelectorAll('.popup').forEach(function (item) { co.querySelectorAll('.popup').forEach(item => {
item.classList.remove('active'); item.classList.remove('active');
}); });
co.style.display = "block"; co.style.display = 'block';
setTimeout(function () { setTimeout(() => {
co.classList.add('active'); co.classList.add('active');
pu.classList.add('active'); pu.classList.add('active');
}, 100); }, 100);
...@@ -86,17 +97,9 @@ export class LoginComponent implements OnInit { ...@@ -86,17 +97,9 @@ export class LoginComponent implements OnInit {
setTimeout(() => { setTimeout(() => {
co.style.display = 'none'; co.style.display = 'none';
this.submit_element.classList.remove('disabled'); this.submitElement.classList.remove('disabled');
}, 500) }, 500);
}, 3000); }, 3000);
} }
get username() {
return this.form.get('username');
}
get password() {
return this.form.get('password');
}
} }
...@@ -11,15 +11,15 @@ export class ProblemService { ...@@ -11,15 +11,15 @@ export class ProblemService {
getProblems(): Observable<Problem[]> { getProblems(): Observable<Problem[]> {
var ret: Problem[] = []; const ret: Problem[] = [];
var arr: number[] = [6164, 6939, 9211, 4162, 7485, 5087, 1025, 4743, 5549, 9743]; const arr: number[] = [6164, 6939, 9211, 4162, 7485, 5087, 1025, 4743, 5549, 9743];
for (let i = 0; i < arr.length; i++) { for (const item of arr) {
ret.push({ ret.push({
id: arr[i], id: item,
title: "DSA Problem", title: 'DSA Problem',
details: "You could never solve it unless you invest 60% of your day and 100% of your mind you can. This problem is by our beloved Professor Mr. Ajit Diwan who specializes in posing mindfucking problems.", details: 'You could never solve it unless you invest 60% of your day and 100% of your mind you can. This problem is by our beloved Professor Mr. Ajit Diwan who specializes in posing mindfucking problems.',
difficulty: 0, difficulty: 0,
n_attempts: 0, n_attempts: 0,
n_correct: 0, n_correct: 0,
......
import { Component, OnInit } from '@angular/core'; import { Component, OnInit } from '@angular/core';
import { FormBuilder, FormControl, FormGroup, Validators } from '@angular/forms'; import {AbstractControl, FormBuilder, FormControl, FormGroup, Validators} from '@angular/forms';
import { Router } from '@angular/router';
import { first } from 'rxjs/operators'; import { first } from 'rxjs/operators';
import { ApiService } from '../api.service'; import { ApiService } from '../api.service';
...@@ -13,7 +12,7 @@ export class RegisterComponent implements OnInit { ...@@ -13,7 +12,7 @@ export class RegisterComponent implements OnInit {
form: FormGroup; form: FormGroup;
timeout: any; timeout: any;
constructor(private fb: FormBuilder, private dataService: ApiService, private router: Router) { constructor(private fb: FormBuilder, private dataService: ApiService) {
this.form = this.fb.group({ this.form = this.fb.group({
name: new FormControl('', [Validators.required, Validators.minLength(1), Validators.maxLength(50)]), name: new FormControl('', [Validators.required, Validators.minLength(1), Validators.maxLength(50)]),
username: new FormControl('', [Validators.required, Validators.minLength(5), Validators.maxLength(50), Validators.pattern(/^[a-zA-Z0-9_]+$/)]), username: new FormControl('', [Validators.required, Validators.minLength(5), Validators.maxLength(50), Validators.pattern(/^[a-zA-Z0-9_]+$/)]),
...@@ -24,52 +23,55 @@ export class RegisterComponent implements OnInit { ...@@ -24,52 +23,55 @@ export class RegisterComponent implements OnInit {
} }
set: NodeListOf<HTMLInputElement>; set: NodeListOf<HTMLInputElement>;
submit_element: HTMLInputElement; submitElement: HTMLInputElement;
ngOnInit(): void { ngOnInit(): void {
this.submit_element = document.getElementById('submit') as HTMLInputElement; this.submitElement = document.getElementById('submit') as HTMLInputElement;
this.set = document.querySelectorAll('input:not([type=submit])'); this.set = document.querySelectorAll('input:not([type=submit])');
this.set.forEach(function (item) { this.set.forEach(item => {
item.addEventListener('focus', function () { item.addEventListener('focus', () => {
item.parentElement.querySelector('label').classList.add('active'); item.parentElement.querySelector('label').classList.add('active');
}); });
item.addEventListener('blur', function () { item.addEventListener('blur', () => {
if (item.value == "") if (item.value === '') {
item.parentElement.querySelector('label').classList.remove('active'); item.parentElement.querySelector('label').classList.remove('active');
}
});
}); });
})
} }
updateLabels() { updateLabels(): void {
this.set.forEach(function (item) { this.set.forEach(item => {
if (item.value !== "" && item.value !== null) if (item.value !== '' && item.value !== null) {
item.parentElement.querySelector('label').classList.add('active'); item.parentElement.querySelector('label').classList.add('active');
else }
else {
item.parentElement.querySelector('label').classList.remove('active'); item.parentElement.querySelector('label').classList.remove('active');
}
}); });
} }
postData(frm: any) { postData(frm: any): void {
if (this.submit_element.classList.contains('disabled')) return; if (this.submitElement.classList.contains('disabled')) { return; }
this.submit_element.classList.add('disabled'); this.submitElement.classList.add('disabled');
if (this.form.get('name').invalid) { if (this.form.get('name').invalid) {
this.error("Invalid name. Please keep it 1 to 50 characters long."); this.error('Invalid name. Please keep it 1 to 50 characters long.');
return; return;
} else if (this.form.get('username').invalid) { } else if (this.form.get('username').invalid) {
this.error("Invalid username. Please keep it 5 to 50 characters long with only alphanumeric characters and underscores."); this.error('Invalid username. Please keep it 5 to 50 characters long with only alphanumeric characters and underscores.');
return; return;
} else if (this.form.get('email').invalid) { } else if (this.form.get('email').invalid) {
this.error("Invalid email address. Please recheck."); this.error('Invalid email address. Please recheck.');
return; return;
} else if (this.form.get('password').invalid || this.form.get('confirm_password').invalid) { } else if (this.form.get('password').invalid || this.form.get('confirm_password').invalid) {
this.error("Invalid password or the passwords don't match. Please keep the password at least 8 characters long."); this.error('Invalid password or the passwords don\'t match. Please keep the password at least 8 characters long.');
this.form.get('password').setValue(''); this.form.get('password').setValue('');
this.form.get('confirm_password').setValue(''); this.form.get('confirm_password').setValue('');
return; return;
} else if (this.form.get('password').value != this.form.get('confirm_password').value) { } else if (this.form.get('password').value !== this.form.get('confirm_password').value) {
this.error("Passwords don't match. Please recheck."); this.error('Passwords don\'t match. Please recheck.');
this.form.get('password').setValue(''); this.form.get('password').setValue('');
this.form.get('confirm_password').setValue(''); this.form.get('confirm_password').setValue('');
return; return;
...@@ -78,27 +80,28 @@ export class RegisterComponent implements OnInit { ...@@ -78,27 +80,28 @@ export class RegisterComponent implements OnInit {
this.dataService.userReg(frm.value.name, frm.value.email, frm.value.password, frm.value.username) this.dataService.userReg(frm.value.name, frm.value.email, frm.value.password, frm.value.username)
.pipe(first()) .pipe(first())
.subscribe( .subscribe(
data => { () => {
this.success(); this.success();
}, },
error => { (err: any) => {
this.error('Could not register you. Please try again after some time.'); console.log(err);
this.error('Could not register you. The username or the email ID already exists.');
}); });
} }
error(str: string) { error(str: string): void {
let co = (document.getElementById('cover') as HTMLDivElement); const co = (document.getElementById('cover') as HTMLDivElement);
let pu = (document.getElementById('error') as HTMLDivElement); const pu = (document.getElementById('error') as HTMLDivElement);
pu.querySelector('.desc').innerHTML = str; pu.querySelector('.desc').innerHTML = str;
co.querySelectorAll('.popup').forEach(function (item) { co.querySelectorAll('.popup').forEach(item => {
item.classList.remove('active'); item.classList.remove('active');
}); });
co.style.display = "block"; co.style.display = 'block';
setTimeout(function () { setTimeout(() => {
co.classList.add('active'); co.classList.add('active');
pu.classList.add('active'); pu.classList.add('active');
}, 100); }, 100);
...@@ -110,21 +113,21 @@ export class RegisterComponent implements OnInit { ...@@ -110,21 +113,21 @@ export class RegisterComponent implements OnInit {
setTimeout(() => { setTimeout(() => {
co.style.display = 'none'; co.style.display = 'none';
this.submit_element.classList.remove('disabled'); this.submitElement.classList.remove('disabled');
}, 500) }, 500);
}, 3000); }, 3000);
} }
success() { success(): void {
let co = (document.getElementById('cover') as HTMLDivElement); const co = (document.getElementById('cover') as HTMLDivElement);
let pu = (document.getElementById('success') as HTMLDivElement); const pu = (document.getElementById('success') as HTMLDivElement);
co.querySelectorAll('.popup').forEach(function (item) { co.querySelectorAll('.popup').forEach(item => {
item.classList.remove('active'); item.classList.remove('active');
}); });
co.style.display = "block"; co.style.display = 'block';
setTimeout(function () { setTimeout(() => {
co.classList.add('active'); co.classList.add('active');
pu.classList.add('active'); pu.classList.add('active');
}, 100); }, 100);
...@@ -134,8 +137,9 @@ export class RegisterComponent implements OnInit { ...@@ -134,8 +137,9 @@ export class RegisterComponent implements OnInit {
pu.classList.remove('active'); pu.classList.remove('active');
co.classList.remove('active'); co.classList.remove('active');
setTimeout(function () { setTimeout(() => {
co.style.display = 'none'; co.style.display = 'none';
this.submitElement.classList.remove('disabled');
}, 500); }, 500);
this.form.reset(); this.form.reset();
...@@ -144,10 +148,10 @@ export class RegisterComponent implements OnInit { ...@@ -144,10 +148,10 @@ export class RegisterComponent implements OnInit {
} }
get email() { return this.form.get('email'); } get email(): AbstractControl { return this.form.get('email'); }
get password() { return this.form.get('password'); } get password(): AbstractControl { return this.form.get('password'); }
get name() { return this.form.get('name'); } get name(): AbstractControl { return this.form.get('name'); }
get username() { return this.form.get('name'); } get username(): AbstractControl { return this.form.get('name'); }
} }
import {Problem} from './problem';
export interface User { export interface User {
id: number; id: number;
username: string; username: string;
...@@ -8,4 +10,5 @@ export interface User { ...@@ -8,4 +10,5 @@ export interface User {
n_attempts: number; n_attempts: number;
correct_timeline: string; correct_timeline: string;
rating: number; rating: number;
my_questions: Problem;
} }
import { Component, OnInit } from '@angular/core'; import {Component, OnInit} from '@angular/core';
import { User } from '../user'; import {User} from '../user';
import { ApiService } from '../api.service'; import {ApiService} from '../api.service';
declare var Chart: any; declare var Chart: any;
@Component({ @Component({
...@@ -15,7 +16,9 @@ export class UserComponent implements OnInit { ...@@ -15,7 +16,9 @@ export class UserComponent implements OnInit {
hist: number[] = []; hist: number[] = [];
timeline: number[]; timeline: number[];
constructor(private dataService: ApiService) { } constructor(private dataService: ApiService) {
}
ngOnInit(): void { ngOnInit(): void {
this.dataService.getLoggedInState.subscribe(state => this.changeState(state)); this.dataService.getLoggedInState.subscribe(state => this.changeState(state));
this.loggedIn = this.dataService.isLoggedIn(); this.loggedIn = this.dataService.isLoggedIn();
...@@ -34,9 +37,9 @@ export class UserComponent implements OnInit { ...@@ -34,9 +37,9 @@ export class UserComponent implements OnInit {
Chart.defaults.global.defaultFontFamily = 'Lato'; Chart.defaults.global.defaultFontFamily = 'Lato';
Chart.defaults.global.defaultFontColor = '#888'; Chart.defaults.global.defaultFontColor = '#888';
var ctx = (document.getElementById('myChart') as HTMLCanvasElement).getContext('2d'); const ctx = (document.getElementById('myChart') as HTMLCanvasElement).getContext('2d');
let myChart = new Chart(ctx, { const chart = new Chart(ctx, {
type: 'line', type: 'line',
data: { data: {
labels: Array.from(this.timeline.keys()), labels: Array.from(this.timeline.keys()),
...@@ -94,13 +97,12 @@ export class UserComponent implements OnInit { ...@@ -94,13 +97,12 @@ export class UserComponent implements OnInit {
}); });
} }
private changeState(state: boolean): void { logout(): void {
this.loggedIn = state; this.dataService.deleteToken();
} }
logout() { private changeState(state: boolean): void {
this.dataService.deleteToken(); this.loggedIn = state;
window.location.href = window.location.href;
} }
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment