Skip to content
Snippets Groups Projects
lecturer-edit-template-view.component.ts 3.72 KiB
Newer Older
import {Component, OnDestroy, OnInit} from '@angular/core';
import {QuestionDao} from 'src/app/dao/question.dao';
import {MinutePaperWrapperDao} from 'src/app/dao/minute-paper-wrapper.dao';
import {LecturerInfoService} from 'src/app/services/lecturer-info.service';
import {LecturerDataService} from 'src/app/services/lecturer-data.service';
import {LecturerViewService} from 'src/app/services/lecturer-view.service';
import {Router} from '@angular/router';
import {MinutePaperDao} from 'src/app/dao/minute-paper.dao';

@Component({
  selector: 'app-lecturer-edit-template-view',
  templateUrl: './lecturer-edit-template-view.component.html',
  styleUrls: ['./lecturer-edit-template-view.component.css']
})
export class LecturerEditTemplateViewComponent implements OnInit, OnDestroy {

  private lecturerId: string;
  public questions: QuestionDao[];
  public name: String;
  public topic: String;
  public isAnonymous: Boolean;
  public currentTemplate: MinutePaperWrapperDao;
  public nameInvalid: Boolean;
  public topicInvalid: Boolean;
  public isSubmitted: Boolean;
  public noQuestions: Boolean;
  public invalidQuestions: Boolean;

  constructor(private lecturerInfoService: LecturerInfoService,
              private lecturerDataService: LecturerDataService,
              private lecturerViewService: LecturerViewService,
              private router: Router) {
  }

  ngOnInit(): void {
    this.nameInvalid = false;
    this.topicInvalid = false;
    this.isSubmitted = false;
    this.noQuestions = false;
    this.invalidQuestions = false;
    this.lecturerId = sessionStorage.getItem('username');
    this.questions = [];
    this.currentTemplate = this.lecturerDataService.currentTemplate;
    this.initialiseForms();
  }

  ngOnDestroy(): void {
    this.questions = [];
  }

  private initialiseForms(): void {
    this.questions = this.currentTemplate.questions;
    this.name = this.currentTemplate.paper.name;
    this.topic = this.currentTemplate.paper.topic;
    this.isAnonymous = this.currentTemplate.paper.anonymous;
  }

  private createWrapperObject() {
    const paper = new MinutePaperDao(this.currentTemplate.paper.paperId, this.lecturerId, 'NO_CLASS',
      this.name, this.topic, true, this.isAnonymous, '0000-00-00');
    return new MinutePaperWrapperDao(paper, this.questions);
  }

  private validateForm(): Boolean {
    this.isSubmitted = true;

    if (this.questions.length === 0) {
      this.noQuestions = true;
    }

    this.invalidQuestions = this.validateQuestions();

    this.nameInvalid = this.empty(this.name);

    this.topicInvalid = this.empty(this.topic);

    return !(this.nameInvalid || this.topicInvalid || this.noQuestions || this.invalidQuestions);
  }

  private validateQuestions(): Boolean {
    let result = false;
    this.questions.forEach(q => {
      if (this.empty(q.questionText)) {
        result = true;
      }
    });
    return result;
  }

  public empty(text: String): Boolean {
    if (text.replace(/\s/g, '').length === 0 || text === '') {
      return true;
    }
    return false;
  }

    if (this.validateForm()) {
      this.lecturerInfoService.createTemplate(this.createWrapperObject()).subscribe(() => {
        this.router.navigate(['/template-view']);
        this.ngOnDestroy();
      });
    }
  }

  public cancel() {
    const choice = confirm('Are you sure you want to cancel?\n' + 'All changes will be lost!');
    if (choice) {
      this.router.navigate(['/template-view']);
      this.ngOnDestroy();
    }
  }

  public flipIsAnonymous(): void {
    this.isAnonymous = !this.isAnonymous;
  }

  public addQuestion(): void {
    this.questions.push(new QuestionDao('0', '0', ''));
  }

  public removeQuestion(index: number): void {
    this.questions.splice(index, 1);
  }
}