From 6d90e121d7dc9e0f5b83410a21a4289219e1c87f Mon Sep 17 00:00:00 2001 From: Lukas Kimpel <lukas.kimpel@mni.thm.de> Date: Fri, 16 Mar 2018 00:01:11 +0100 Subject: [PATCH] Implement submit function to add answers as a participant Add file-private class to implement answer logic with indexes --- .../participant-choice-content.component.html | 6 ++-- .../participant-choice-content.component.ts | 32 ++++++++++++++++++- 2 files changed, 34 insertions(+), 4 deletions(-) diff --git a/src/app/participant-choice-content/participant-choice-content.component.html b/src/app/participant-choice-content/participant-choice-content.component.html index 295ef1677..b731b9397 100644 --- a/src/app/participant-choice-content/participant-choice-content.component.html +++ b/src/app/participant-choice-content/participant-choice-content.component.html @@ -1,10 +1,10 @@ -<form> +<form (ngSubmit)="submitAnswer()"> <h1 class="mat-headline">{{ content.subject }}</h1> <p>{{ content.body }}</p> <mat-divider></mat-divider> <mat-list> - <mat-list-item *ngFor="let answer of content.options"> - <mat-checkbox color="primary">{{ answer.label }}</mat-checkbox> + <mat-list-item *ngFor="let answer of checkedAnswers"> + <mat-checkbox color="primary" [(ngModel)]="answer.checked" name="answer">{{ answer.answerOption.label }}</mat-checkbox> </mat-list-item> </mat-list> <button mat-raised-button color="primary">Send answer</button> diff --git a/src/app/participant-choice-content/participant-choice-content.component.ts b/src/app/participant-choice-content/participant-choice-content.component.ts index 365adb1b4..1840583d1 100644 --- a/src/app/participant-choice-content/participant-choice-content.component.ts +++ b/src/app/participant-choice-content/participant-choice-content.component.ts @@ -1,6 +1,17 @@ import { Component, OnInit } from '@angular/core'; import { ChoiceContent } from '../choice-content'; import { AnswerOption } from '../answer-option'; +import { ContentAnswerService } from '../content-answer.service'; + +class CheckedAnswer { + answerOption: AnswerOption; + checked: boolean; + + constructor(answerOption: AnswerOption, checked: boolean) { + this.answerOption = answerOption; + this.checked = checked; + } +} @Component({ selector: 'app-participant-choice-content', @@ -22,10 +33,29 @@ export class ParticipantChoiceContentComponent implements OnInit { ], [2, 3, 4], true); + checkedAnswers: CheckedAnswer[] = []; - constructor() { + constructor(private answerService: ContentAnswerService) { } ngOnInit() { + this.initAnswers(); + } + + initAnswers(): void { + for (const answerOption of this.content.options) { + this.checkedAnswers.push(new CheckedAnswer(answerOption, false)); + } + } + + submitAnswer(): void { + const selectedAnswers: number[] = []; + for (let i = 0; i < this.checkedAnswers.length; i++) { + if (this.checkedAnswers[i].checked) { + selectedAnswers.push(i); + } + } + // ToDo: Implement function in service + // this.answerService.addChoiceAnswer(selectedAnswers); } } -- GitLab