Full Project upgrade

This commit is contained in:
abdellah
2022-11-03 13:26:26 +01:00
parent 7575a2c72b
commit 2c74c2ba9d
28 changed files with 626 additions and 20742 deletions

View File

@@ -0,0 +1,95 @@
import { Component, OnInit } from '@angular/core';
import { map, Observable } from 'rxjs';
import { CandidatesService } from '../services/candidates.service';
import { Candidate } from '../models/candidate.model';
import { VotesService } from '../services/votes.service';
import { LocalStorageService } from '../services/local-storage.service';
@Component({
selector: 'app-voting',
templateUrl: './voting.component.html',
styleUrls: ['./voting.component.scss']
})
export class VotingComponent implements OnInit {
groups: number[] = [];
candidates!: any[];
groupedCandidates: Candidate[][] = [];
groupedVotedCandidates: number[] = [];;
votedGroups: number[] = [];
votesCount: number=0;
public candidatestoshow: any;
public rows!: Observable<any[]>;
private currentVoteID: any;
primes!: number[];
items: any;
totalVoteData: any;
downloadJsonHref: any;
candidatesCount: number = 0;
constructor(private candidatesService: CandidatesService,private voteService:VotesService, private localStorageService:LocalStorageService) {
}
ngOnInit(): void {
this.retrieveCandidates();
this.voteService.getVotesCount().subscribe(count=>this.votesCount=count);
if(this.localStorageService.getData('groupedVotedCandidates')!='')
this.groupedVotedCandidates=JSON.parse(this.localStorageService.getData('groupedVotedCandidates'));
}
retrieveCandidates(): void {
this.candidatesService.getAll().snapshotChanges().pipe(
map(changes =>
changes.map(c =>
({ id: c.payload.doc.id, ...c.payload.doc.data() })
)
)
).subscribe(data => {
this.candidates = data;
this.candidatesCount = this.candidates.length;
this.groupCandidates();
});
}
groupCandidates(): void {
this.groups = [];
this.groupedCandidates = [];
this.candidates.forEach(candidate => {
if (!(this.groupedCandidates[candidate.group] instanceof Array)) {
this.groupedCandidates[candidate.group] = [];
this.groups.push(candidate.group);
}
this.groupedCandidates[candidate.group].push(candidate);
});
this.groups = this.groups.sort();
}
vote(candidate: Candidate) {
if(this.groupedVotedCandidates[candidate.group] == undefined)
{
this.groupedVotedCandidates[candidate.group] = candidate.candidateID;
//candidate.votes++;
this.candidatesService.addVote(candidate);
this.voteService.vote(candidate.candidateID);
}
this.localStorageService.saveData('groupedVotedCandidates',JSON.stringify(this.groupedVotedCandidates));
}
getCandidateClass(group: number, id: number) {
if (this.groupedVotedCandidates[group] != undefined) {
return {
elect: this.groupedVotedCandidates[group] === id,
lost: this.groupedVotedCandidates[group] !== id,
};
}
return '';
}
}