From 28b03d63f720a0a5cd9a10cfa7252d05b2aa7621 Mon Sep 17 00:00:00 2001 From: Zachary Golba Date: Sat, 24 Sep 2016 17:20:43 -0400 Subject: [PATCH] docs: update examples (#419) docs: update examples --- examples/social-network/app/models/action.js | 112 ++++++++++--------- examples/social-network/package.json | 2 +- examples/todo/package.json | 2 +- 3 files changed, 60 insertions(+), 56 deletions(-) diff --git a/examples/social-network/app/models/action.js b/examples/social-network/app/models/action.js index 1aa3fbc3..8b687490 100644 --- a/examples/social-network/app/models/action.js +++ b/examples/social-network/app/models/action.js @@ -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 */ @@ -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 + }); + } + } } } } diff --git a/examples/social-network/package.json b/examples/social-network/package.json index b8afd44e..3d7ef7f4 100644 --- a/examples/social-network/package.json +++ b/examples/social-network/package.json @@ -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" }, diff --git a/examples/todo/package.json b/examples/todo/package.json index ea6aa931..a1ef4038 100644 --- a/examples/todo/package.json +++ b/examples/todo/package.json @@ -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" },