Skip to content

Commit

Permalink
docs: update examples (#419)
Browse files Browse the repository at this point in the history
docs: update examples
  • Loading branch information
zacharygolba authored Sep 24, 2016
1 parent 6b2f86d commit 28b03d6
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 56 deletions.
112 changes: 58 additions & 54 deletions examples/social-network/app/models/action.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ import Post from 'app/models/post';
import Reaction from 'app/models/reaction';
import User from 'app/models/user';

const createMessageTemplate = resourceType => (name, reactionType) => (
`${name} reacted to your ${resourceType} with ${reactionType}`
);

/* TODO: Add support for polymorphic relationship to a 'trackable'.
* https://github.com/postlight/lux/issues/75
*/
Expand All @@ -18,70 +22,70 @@ class Action extends Model {

async notifyOwner() {
const { trackableId, trackableType } = this;
let params;

if (trackableType === 'Comment') {
const {
postId,
userId
} = await Comment.find(trackableId, {
select: ['postId', 'userId']
});
const trackable = await Comment
.first()
.select('postId', 'userId')
.where({ id: trackableId });

const [
{ name: userName },
{ userId: recipientId }
] = await Promise.all([
User.find(userId, { select: ['name'] }),
Post.find(postId, { select: ['userId'] })
]);
if (trackable) {
const [user, post] = await Promise.all([
User
.first()
.select('name')
.where({ id: trackable.userId }),
Post
.first()
.select('userId')
.where({ id: trackable.postId })
]);

params = {
recipientId,
message: `${userName} commented on your post!`
};
if (user && post) {
await Notification.create({
message: `${user.name} commented on your post!`,
recipientId: post.userId
});
}
}
} else if (trackableType === 'Reaction') {
let reactableId;
let reactableModel = Post;

const {
commentId,
postId,
type,
userId
} = await Reaction.find(trackableId, {
select: [
'commentId',
'postId',
'type',
'userId'
]
});
let createMessage;
let ReactableModel;

if (!postId) {
reactableId = commentId;
reactableModel = Comment;
} else {
reactableId = postId;
}
const reaction = await Reaction
.first()
.where({ id: trackableId });

const [
{ name: userName },
{ userId: recipientId }
] = await Promise.all([
User.find(userId, { select: ['name'] }),
reactableModel.find(reactableId, { select: ['userId'] })
]);
if (reaction) {
if (!reaction.postId) {
ReactableModel = Comment;
createMessage = createMessageTemplate('comment');
reactableId = reaction.commentId;
} else {
ReactableModel = Post;
createMessage = createMessageTemplate('post');
reactableId = reaction.postId;
}

params = {
recipientId,
message: `${userName} reacted with ${type} to your ` +
`${reactableModel.name.toLowerCase()}!`
};
}
const [user, reactable] = await Promise.all([
User
.first()
.select('name')
.where({ id: reaction.userId }),
ReactableModel
.first()
.select('userId')
.where({ id: reactableId })
]);

if (params) {
return await Notification.create(params);
if (user && reactable) {
await Notification.create({
message: createMessage(user.name, reaction.type),
recipientId: reactable.userId
});
}
}
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion examples/social-network/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
"babel-core": "6.14.0",
"babel-preset-lux": "1.2.0",
"bcryptjs": "2.3.0",
"knex": "0.11.10",
"knex": "0.12.1",
"lux-framework": "1.0.0-rc.7",
"sqlite3": "3.1.4"
},
Expand Down
2 changes: 1 addition & 1 deletion examples/todo/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
"dependencies": {
"babel-core": "6.14.0",
"babel-preset-lux": "1.2.0",
"knex": "0.11.10",
"knex": "0.12.1",
"lux-framework": "1.0.0-rc.7",
"sqlite3": "3.1.4"
},
Expand Down

0 comments on commit 28b03d6

Please sign in to comment.