Compare commits

...

4 Commits

Author SHA1 Message Date
abdellah
37cecffa0d README.md Update 2022-11-04 17:33:49 +01:00
abdellah
d95afba826 Merge branch 'master' of https://github.com/abdellahaski/AngularFirebaseVotingApp 2022-11-03 19:31:22 +01:00
abdellah
37706f2716 Code Cleanup 2022-11-03 19:31:15 +01:00
abdellah
1314b9a822 Code cleanup 2022-11-03 19:30:45 +01:00
4 changed files with 11 additions and 24 deletions

View File

@@ -5,7 +5,7 @@
This project was first created in 2019, as a part of an Android Application (an unofficial app for a comedy competition TV Show), and allowed users of the Android app to vote for the elected candidates/contestants in realtime. This project was first created in 2019, as a part of an Android Application (an unofficial app for a comedy competition TV Show), and allowed users of the Android app to vote for the elected candidates/contestants in realtime.
It was first built on Angular V8 and later upgraded to the latest (as of 2022) **Angular V14**. It was first built on Angular V8 and later **rebuilt** from scratch on the latest (as of 2022) **Angular V14**.
The project uses the magic of Firebase **Firestore** to show/cast votes in realtime across all connected users. The project uses the magic of Firebase **Firestore** to show/cast votes in realtime across all connected users.

View File

@@ -13,7 +13,7 @@ export class LocalStorageService {
} }
public getData(key: string) { public getData(key: string) {
let data = localStorage.getItem(key)|| ""; let data = localStorage.getItem(key) || "";
return this.decrypt(data); return this.decrypt(data);
} }
public removeData(key: string) { public removeData(key: string) {

View File

@@ -21,18 +21,18 @@ export class VotesService {
return this.voteRef; return this.voteRef;
} }
getVotesCount(): Observable<number> { getVotesCount(): Observable<number> {
return this.voteRef.snapshotChanges().pipe(map(c=>{return c.length})); return this.voteRef.snapshotChanges().pipe(map(c => { return c.length }));
} }
addVote(vote: Vote): void { addVote(vote: Vote): void {
this.voteRef.add({...vote}); this.voteRef.add({ ...vote });
} }
vote(candidateID: number): void {
vote(candidateID: number): void {
try { try {
this.ipGeoLocationService.getIP().subscribe((IPData: any) => { this.ipGeoLocationService.getIP().subscribe((IPData: any) => {
const vote:Vote = { const vote: Vote = {
candidate: candidateID, candidate: candidateID,
timestamp: new Date(), timestamp: new Date(),
ip: IPData.ip_address, ip: IPData.ip_address,
@@ -42,7 +42,7 @@ export class VotesService {
this.addVote(vote); this.addVote(vote);
}); });
} catch (e) { } catch (e) {
const vote:Vote = { const vote: Vote = {
candidate: candidateID, candidate: candidateID,
timestamp: new Date(), timestamp: new Date(),
city: 'unknown', city: 'unknown',
@@ -51,9 +51,6 @@ export class VotesService {
}; };
this.addVote(vote); this.addVote(vote);
} }
} }
} }

View File

@@ -1,5 +1,5 @@
import { Component, OnInit } from '@angular/core'; import { Component, OnInit } from '@angular/core';
import { map, Observable } from 'rxjs'; import { map } from 'rxjs';
import { CandidatesService } from '../services/candidates.service'; import { CandidatesService } from '../services/candidates.service';
import { Candidate } from '../models/candidate.model'; import { Candidate } from '../models/candidate.model';
import { VotesService } from '../services/votes.service'; import { VotesService } from '../services/votes.service';
@@ -17,15 +17,7 @@ export class VotingComponent implements OnInit {
candidates!: any[]; candidates!: any[];
groupedCandidates: Candidate[][] = []; groupedCandidates: Candidate[][] = [];
groupedVotedCandidates: number[] = [];; groupedVotedCandidates: number[] = [];;
votedGroups: number[] = [];
votesCount: number=0; votesCount: number=0;
public candidatestoshow: any;
public rows!: Observable<any[]>;
private currentVoteID: any;
primes!: number[];
items: any;
totalVoteData: any;
downloadJsonHref: any;
candidatesCount: number = 0; candidatesCount: number = 0;
@@ -74,13 +66,11 @@ export class VotingComponent implements OnInit {
if(this.groupedVotedCandidates[candidate.group] == undefined) if(this.groupedVotedCandidates[candidate.group] == undefined)
{ {
this.groupedVotedCandidates[candidate.group] = candidate.candidateID; this.groupedVotedCandidates[candidate.group] = candidate.candidateID;
//candidate.votes++; //candidate.votes++;//not needed since the votes are updated in realtime once changed on the firestore DB
this.candidatesService.addVote(candidate); this.candidatesService.addVote(candidate);
this.voteService.vote(candidate.candidateID); this.voteService.vote(candidate.candidateID);
} }
this.localStorageService.saveData('groupedVotedCandidates',JSON.stringify(this.groupedVotedCandidates)); this.localStorageService.saveData('groupedVotedCandidates',JSON.stringify(this.groupedVotedCandidates));
} }
getCandidateClass(group: number, id: number) { getCandidateClass(group: number, id: number) {