Skip to content

Commit

Permalink
fix new post/comment actions
Browse files Browse the repository at this point in the history
  • Loading branch information
echenley committed Aug 31, 2015
1 parent cd0ab7e commit eb29cee
Showing 1 changed file with 77 additions and 53 deletions.
130 changes: 77 additions & 53 deletions src/js/actions/Actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,41 +12,45 @@ const usersRef = baseRef.child('users');
// used to create email hash for gravatar
import md5 from 'md5';

const Actions = Reflux.createActions({
const Actions = Reflux.createActions([
// user actions
'login': {},
'logout': {},
'register': {},
'login',
'logout',
'register',
'updateLatestPost',
// post actions
'upvotePost': {},
'downvotePost': {},
'submitPost': { asyncResult: true },
'deletePost': {},
'setSortBy': {},
'upvotePost',
'downvotePost',
'submitPost',
'deletePost',
'setSortBy',
// comment actions
'upvoteComment': {},
'downvoteComment': {},
'updateCommentCount': {},
'addComment': {},
'deleteComment': {},
'upvoteComment',
'downvoteComment',
'updateCommentCount',
'addComment',
'deleteComment',
// comment form actions
'commentFormError',
'clearCommentFormError',
// firebase actions
'watchPost': {},
'watchPosts': {},
'watchProfile': {},
'stopWatchingPost': {},
'stopWatchingPosts': {},
'stopWatchingProfile': {},
'watchPost',
'watchPosts',
'watchProfile',
'stopWatchingPost',
'stopWatchingPosts',
'stopWatchingProfile',
// modal actions
'showModal': {},
'hideModal': {},
'modalError': {}
});
'showModal',
'hideModal',
'modalError'
]);

/* User Actions
=============================== */

Actions.login.listen(function(loginData) {
baseRef.authWithPassword(loginData, error => (
Actions.login.listen((loginData) => {
baseRef.authWithPassword(loginData, (error) => (
error ? Actions.modalError(error.code) : Actions.hideModal()
));
});
Expand All @@ -58,7 +62,7 @@ function createUser(username, loginData) {
upvoted: {}
};

baseRef.createUser(loginData, function(error, userData) {
baseRef.createUser(loginData, (error, userData) => {
if (error) {
// email taken, other login errors
return Actions.modalError(error.code);
Expand All @@ -70,9 +74,9 @@ function createUser(username, loginData) {
});
}

Actions.register.listen(function(username, loginData) {
Actions.register.listen((username, loginData) => {
// check if username is already taken
usersRef.orderByChild('username').equalTo(username).once('value', function(user) {
usersRef.orderByChild('username').equalTo(username).once('value', (user) => {
if (user.val()) {
// username is taken
Actions.modalError('USERNAME_TAKEN');
Expand All @@ -88,15 +92,23 @@ Actions.register.listen(function(username, loginData) {
=============================== */

Actions.submitPost.listen(function(post) {
let newPostRef = postsRef.push(post, error => (
// this.completed is only listened to by NewPost component
error ? Actions.modalError(error) : this.completed(newPostRef.key())
));
let newPostRef = postsRef.push(post, (error) => {
if (error) { return Actions.modalError(error); }

let postId = newPostRef.key();
// add commentId to user's profile
usersRef
.child(`${post.creatorUID}/submitted/${postId}`)
.set(true, () => Actions.updateLatestPost(postId));
});
});

Actions.deletePost.listen(function(postId) {
postsRef.child(postId).set({
isDeleted: true
Actions.deletePost.listen((post) => {
postsRef.child(post.id).set({ isDeleted: true }, (error) => {
if (error) { return; }

// remove commentId from user's profile
usersRef.child(`${post.creatorUID}/submitted/${post.id}`).remove();
});
});

Expand All @@ -112,7 +124,7 @@ Actions.deletePost.listen(function(postId) {
*/

function updatePostUpvotes(postId, n) {
postsRef.child(postId + '/upvotes').transaction(curr => (curr || 0) + n);
postsRef.child(`${postId}/upvotes`).transaction(curr => (curr || 0) + n);
}

/*
Expand All @@ -124,18 +136,18 @@ function updatePostUpvotes(postId, n) {
same is true for up/downvoteComment.
*/

Actions.upvotePost.listen(function(userId, postId) {
Actions.upvotePost.listen((userId, postId) => {
// set upvote in user's profile
usersRef.child(userId + '/upvoted/' + postId).set(true, function(error) {
usersRef.child(`${userId}/upvoted/${postId}`).set(true, (error) => {
if (error) { return; }
// increment post's upvotes
updatePostUpvotes(postId, 1);
});
});

Actions.downvotePost.listen(function(userId, postId) {
Actions.downvotePost.listen((userId, postId) => {
// remove upvote from user's profile
usersRef.child(userId + '/upvoted/' + postId).remove(function(error) {
usersRef.child(`${userId}/upvoted/${postId}`).remove((error) => {
if (error) { return; }
// decrement post's upvotes
updatePostUpvotes(postId, -1);
Expand All @@ -146,21 +158,21 @@ Actions.downvotePost.listen(function(userId, postId) {
=============================== */

function updateCommentUpvotes(commentId, n) {
commentsRef.child(commentId + '/upvotes').transaction(curr => (curr || 0) + n);
commentsRef.child(`${commentId}/upvotes`).transaction(curr => (curr || 0) + n);
}

Actions.upvoteComment.listen(function(userId, commentId) {
Actions.upvoteComment.listen((userId, commentId) => {
// set upvote in user's profile
usersRef.child(userId + '/upvoted/' + commentId).set(true, function(error) {
usersRef.child(`${userId}/upvoted/${commentId}`).set(true, (error) => {
if (error) { return; }
// increment comment's upvotes
updateCommentUpvotes(commentId, 1);
});
});

Actions.downvoteComment.listen(function(userId, commentId) {
Actions.downvoteComment.listen((userId, commentId) => {
// remove upvote from user's profile
usersRef.child(userId + '/upvoted/' + commentId).remove(function(error) {
usersRef.child(`${userId}/upvoted/${commentId}`).remove((error) => {
if (error) { return; }
// decrement comment's upvotes
updateCommentUpvotes(commentId, -1);
Expand All @@ -169,20 +181,32 @@ Actions.downvoteComment.listen(function(userId, commentId) {

function updateCommentCount(postId, n) {
// updates comment count on post
postsRef.child(postId + '/commentCount').transaction(curr => (curr || 0) + n);
postsRef.child(`${postId}/commentCount`).transaction(curr => (curr || 0) + n);
}

Actions.addComment.listen(function(comment) {
commentsRef.push(comment, function(error) {
if (error) { return; }
Actions.addComment.listen((comment) => {
let newCommentRef = commentsRef.push(comment, (error) => {
if (error) {
return Actions.commentFormError('COMMENT_FAILED');
}

Actions.clearCommentFormError();

updateCommentCount(comment.postId, 1);

// add commentId to user's profile
usersRef.child(`${comment.creatorUID}/submitted/${newCommentRef.key()}`).set(true);
});
});

Actions.deleteComment.listen(function(commentId, postId) {
commentsRef.child(commentId).remove(function(error) {
Actions.deleteComment.listen((comment) => {
commentsRef.child(comment.id).remove((error) => {
if (error) { return; }
updateCommentCount(postId, -1);

updateCommentCount(comment.postId, -1);

// remove commentId from user's profile
usersRef.child(`${comment.creatorUID}/submitted/${comment.id}`).remove();
});
});

Expand Down

1 comment on commit eb29cee

@echenley
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixes #17

Please sign in to comment.