displaying result percentages #93

This commit is contained in:
Nicolas Constant 2019-06-05 00:06:29 -04:00
parent 979d21bdfe
commit c122d66fb6
No known key found for this signature in database
GPG Key ID: 1E9F677FB01A5688
3 changed files with 61 additions and 22 deletions

View File

@ -1,17 +1,25 @@
<div class="poll"> <div class="poll">
<div *ngFor="let o of options"> <div *ngIf="!poll.voted && !poll.expired">
<!-- <input class="poll__input" id="{{o.title}}" type="{{choiceType}}" name="poll" value="{{o.title}}"> <div *ngFor="let o of options">
<label class="noselect poll__label" for="{{o.title}}">{{o.title}}</label> <label class="poll__container">{{o.title}}
<br /> --> <input class="poll__container__input" type="{{choiceType}}" name="{{pollName}}" value="{{o.title}}"
(change)="onSelectionChange(o)">
<label class="poll__container">{{o.title}} <span class="poll__container__checkmark"
<input class="poll__container__input" type="{{choiceType}}" name="{{pollName}}" [ngClass]="{'poll__container__checkmark--box' : choiceType=='checkbox', 'poll__container__checkmark--round': choiceType=='radio'}"></span>
value="{{o.title}}" (change)="onSelectionChange(o)"> </label>
<span class="poll__container__checkmark" [ngClass]="{'poll__container__checkmark--box' : choiceType=='checkbox', 'poll__container__checkmark--round': choiceType=='radio'}"></span> </div>
</label> </div>
<div *ngIf="poll.voted || poll.expired">
<div *ngFor="let o of options">
<div class="poll__result">
<span class="poll__result--percentage">{{ o.percentage }}%</span> {{o.title}}
</div>
</div>
</div> </div>
<div class="poll__voting"> <div class="poll__voting">
<button href class="btn btn-sm btn-custom-primary poll__btn-vote" title="don't boo, vote!" (click)="vote()">Vote</button> <button href *ngIf="!poll.voted && !poll.expired" class="btn btn-sm btn-custom-primary poll__btn-vote" title="don't boo, vote!"
<div class="poll__statistics">{{poll.votes_count}} votes - X days left</div> (click)="vote()">Vote</button>
<a href class="poll__refresh" *ngIf="poll.voted && !poll.expired">refresh </a>
<div class="poll__statistics"><span *ngIf="poll.voted"> · </span>{{poll.votes_count}} votes · X days left</div>
</div> </div>
</div> </div>

View File

@ -4,6 +4,8 @@
@import "buttons"; @import "buttons";
.poll { .poll {
color: white;
color: rgb(228, 228, 228);
&__btn-vote { &__btn-vote {
margin: 0 10px 0 0; margin: 0 10px 0 0;
@ -24,6 +26,11 @@
margin-top: 10px; margin-top: 10px;
} }
&__refresh {
font-size: 0.8em;
color: rgb(101, 121, 160);
}
&__container { &__container {
display: block; display: block;
position: relative; position: relative;
@ -108,6 +115,20 @@
background: white; background: white;
} }
} }
&__result {
margin: 0 0 5px 5px;
&--percentage {
// font-weight: bold;
// font-weight: 600;
// font-style: italic;
color: rgb(228, 228, 228);
color: white;
display: inline;
margin-right: 10px;
}
}
} }
.noselect { .noselect {

View File

@ -11,7 +11,7 @@ import { NotificationService } from '../../../../services/notification.service';
styleUrls: ['./poll.component.scss'] styleUrls: ['./poll.component.scss']
}) })
export class PollComponent implements OnInit { export class PollComponent implements OnInit {
pollName: string; pollName: string;
choiceType: string; choiceType: string;
private pollSelection: number[] = []; private pollSelection: number[] = [];
@ -26,18 +26,18 @@ export class PollComponent implements OnInit {
ngOnInit() { ngOnInit() {
this.pollName = this.poll.id; this.pollName = this.poll.id;
//this.poll.multiple = true; //this.poll.multiple = true;
if(this.poll.multiple){ if (this.poll.multiple) {
this.choiceType = 'checkbox'; this.choiceType = 'checkbox';
} else { } else {
this.choiceType = 'radio'; this.choiceType = 'radio';
} }
let i = 0; let i = 0;
for(let opt of this.poll.options){ for (let opt of this.poll.options) {
let optWrapper = new PollOptionWrapper(i, opt); let optWrapper = new PollOptionWrapper(i, opt, this.poll.votes_count);
this.options.push(optWrapper); this.options.push(optWrapper);
i++; i++;
} }
@ -56,10 +56,10 @@ export class PollComponent implements OnInit {
return false; return false;
} }
onSelectionChange(entry: PollOptionWrapper){ onSelectionChange(entry: PollOptionWrapper) {
let index = entry.id; let index = entry.id;
if(this.poll.multiple){ if (this.poll.multiple) {
if(this.pollSelection.includes(index)){ if (this.pollSelection.includes(index)) {
this.pollSelection = this.pollSelection.filter(x => x !== index); this.pollSelection = this.pollSelection.filter(x => x !== index);
} else { } else {
this.pollSelection.push(index); this.pollSelection.push(index);
@ -73,13 +73,23 @@ export class PollComponent implements OnInit {
} }
class PollOptionWrapper implements PollOption { class PollOptionWrapper implements PollOption {
constructor(index: number, option: PollOption){ constructor(index: number, option: PollOption, totalVotes: number) {
this.id = index; this.id = index;
this.title = option.title; this.title = option.title;
this.votes_count = option.votes_count; this.votes_count = option.votes_count;
console.log(this.votes_count);
console.log(totalVotes);
console.log(this.votes_count / totalVotes);
console.log( (this.votes_count / totalVotes) * 100);
if (totalVotes === 0) {
this.percentage = '0';
} else {
this.percentage = ((this.votes_count / totalVotes) * 100).toFixed(0);
}
} }
id: number; id: number;
title: string; title: string;
votes_count: number; votes_count: number;
percentage: string;
} }