mirror of
https://github.com/abdellahaski/AngularFirebaseVotingApp.git
synced 2025-12-08 11:19:56 +00:00
Full Project upgrade
This commit is contained in:
95
src/app/voting/voting.component.ts
Normal file
95
src/app/voting/voting.component.ts
Normal 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 '';
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user