Skip to content

Instantly share code, notes, and snippets.

@captaincole
Created February 27, 2018 21:50
Show Gist options
  • Select an option

  • Save captaincole/b49ad648bbd4ad19716e0ebabe2a36af to your computer and use it in GitHub Desktop.

Select an option

Save captaincole/b49ad648bbd4ad19716e0ebabe2a36af to your computer and use it in GitHub Desktop.
import {Component, Element, Prop, State} from '@stencil/core';
import firebase from 'firebase';
@Component({
tag: 'job-item',
styleUrl: 'job-item.scss'
})
export class JobItem {
@Prop() job: any;
@State() vote: any;
@State() points: number;
@State() votesRef: any;
componentDidLoad() {
// Look for votes
this.updateWatchers();
// Listen to firebase users...
}
updateWatchers() {
// Find a reference to only votes that are for the associated job
this.votesRef = firebase.database().ref('votes').orderByChild('voteOn').equalTo(this.job.id);
// Attach a listner for changes on this reference
this.votesRef.on('value', (snapshot) => {
// Listen For New Votes, Update Vote Count And Current Vote
console.log('Vote count changed', this.job.company);
let voteCount = 0;
snapshot.forEach( (vote): any => {
let voteValue = vote.val();
voteCount += voteValue.points;
// Could do some other stuff here
});
console.log('voteCount' , voteCount);
this.job.points = voteCount;
this.points = voteCount;
}, (err) => {
console.warn(err);
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment