src/app/home/home.component.ts
selector | app-home |
styleUrls | home.component.scss |
templateUrl | home.component.html |
constructor(dataService: ApiService, questionService: QuestionService)
|
organizeQues |
organizeQues()
|
Filters out and drops the expired questions from the question set obtained from the server.
Returns:
void
|
openForm |
openForm()
|
Displays the form to post a question when the 'Post a Question' button is clicked.
Returns:
void
|
closeForm |
closeForm()
|
Closes the form popup.
Returns:
void
|
resetForm |
resetForm()
|
Resets the question upload form to the default empty values.
Returns:
void
|
submitForm |
submitForm()
|
Posts the question data to be uploaded to the server and notifies if the title clashes with any of those of the already existing
Returns:
void
|
errorMessage |
errorMessage: |
ques_final |
ques_final: |
questions |
questions: |
questiontoPost |
questiontoPost: |
user |
user: |
import {Component, OnInit} from '@angular/core';
// import {ProblemService} from '../problem.service';
import { ApiService } from '../api.service';
// import {Problem} from '../problem';
import { User } from '../user';
import { Question } from '../question';
import { QuestionService } from '../question.service';
@Component({
selector: 'app-home',
templateUrl: './home.component.html',
styleUrls: ['./home.component.scss']
})
export class HomeComponent implements OnInit {
user: User;
// problems: Problem[];
questiontoPost: Question = {
title: "",
username: "",
statement: "",
tc1: "",
out1: "",
tc2: "",
out2: "",
stime: "",
etime: ""
};
questions: Question[];
ques_final: Question[] = [];
errorMessage: any;
constructor(private dataService: ApiService,
private questionService: QuestionService) {
}
ngOnInit(): void {
this.user = JSON.parse(this.dataService.getToken());
this.questiontoPost.username = this.user.username;
// this.getProblems();
this.questionService.getQues()
.subscribe(data => {
this.questions = data;
// console.log(this.questions);
this.organizeQues();
},
error => this.errorMessage = <any>error
);
}
/**
* Filters out and drops the expired questions from the question set obtained from the server.
*/
organizeQues(): void {
this.questions.forEach(element => {
let d = new Date();
let s = new Date(element.stime);
let e = new Date(element.etime);
if(s < d && e > d) {
this.ques_final.push(element);
}
});
// console.log(this.ques_final);
}
// getProblems(): void {
// this.problemService.getProblems()
// .subscribe(problems => this.problems = problems);
// }
/**
* Displays the form to post a question when the 'Post a Question' button is clicked.
*/
openForm(): void {
document.getElementById('popupForm').style.display = 'block';
}
/**
* Closes the form popup.
*/
closeForm(): void {
document.getElementById('popupForm').style.display = 'none';
}
/**
* Resets the question upload form to the default empty values.
*/
resetForm(): void {
this.questiontoPost = {
title: "",
username: this.user.username,
statement: "",
tc1: "",
out1: "",
tc2: "",
out2: "",
stime: "",
etime: ""
};
}
/**
* Posts the question data to be uploaded to the server and notifies if the title clashes with any of those of the already existing
* questions.
*/
submitForm() {
this.questiontoPost.stime = this.questiontoPost.stime.split('T').join(' ');
this.questiontoPost.etime = this.questiontoPost.etime.split('T').join(' ');
this.questionService.uploadQues(this.questiontoPost).subscribe(
(event: any) => {
if (typeof (event) === 'object') {
this.questiontoPost = null;
(document.getElementById('quesform') as HTMLInputElement).value = '';
}
let s = new Date(this.questiontoPost.stime);
let d = new Date();
console.log(d);
let e = new Date(this.questiontoPost.etime);
if(s < d && d < e) {
this.ques_final.push(this.questiontoPost);
}
this.resetForm();
this.closeForm();
},
(err) => {
alert("Question Already exists!");
}
);
}
}