Skip to content

Instantly share code, notes, and snippets.

@johnthepink
Created January 16, 2016 21:58
Show Gist options
  • Save johnthepink/35c9cd10096a865061a8 to your computer and use it in GitHub Desktop.
Save johnthepink/35c9cd10096a865061a8 to your computer and use it in GitHub Desktop.
likeable.js
import { Likes } from "apollos/core/lib/collections"
import { nav as navActions, liked as likedActions } from "apollos/core/client/actions"
import Helpers from "app/client/helpers"
const Likeable = {
componentWillMount: function() {
this.likeableAction = this.onClickAction.bind(this)
},
onClickAction: function() {
const entry = this.getEntry();
this.updateRedux(entry);
this.updateDatabase(entry);
return {
type: "FALSY",
payload: {}
}
},
getEntry: function() {
const data = this.data;
if (data.devotion) return data.devotion
if (data.article) return data.article
if (data.story) return data.story
if (data.currentSermon) return data.currentSermon
if (data.series) return data.series
if (data.album) return data.album
},
updateRedux: function(entry) {
this.props.dispatch(likedActions.toggle({
entryId: entry.entryId
}));
},
updateDatabase: function(entry) {
// find existing like
const foundLike = Likes.findOne({
userId: Meteor.user()._id,
entryId: entry.entryId
});
// update database
if (foundLike) {
Likes.remove(foundLike._id);
} else {
Likes.insert({
userId: Meteor.user()._id,
entryId: entry.entryId,
title: entry.title,
image: entry.channelName === "sermons" ?
Helpers.backgrounds.image(this.data.series) :
Helpers.backgrounds.image(entry),
link: Helpers.content.links(entry),
icon: Helpers.categories.icon(entry),
category: Helpers.categories.name(entry),
date: entry.meta.date,
status: entry.status,
dateLiked: new Date()
});
}
}
}
export default Likeable
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment