Skip to content

Commit 28b03d6

Browse files
authored
docs: update examples (#419)
docs: update examples
1 parent 6b2f86d commit 28b03d6

File tree

3 files changed

+60
-56
lines changed

3 files changed

+60
-56
lines changed

examples/social-network/app/models/action.js

Lines changed: 58 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@ import Post from 'app/models/post';
66
import Reaction from 'app/models/reaction';
77
import User from 'app/models/user';
88

9+
const createMessageTemplate = resourceType => (name, reactionType) => (
10+
`${name} reacted to your ${resourceType} with ${reactionType}`
11+
);
12+
913
/* TODO: Add support for polymorphic relationship to a 'trackable'.
1014
* https://github.com/postlight/lux/issues/75
1115
*/
@@ -18,70 +22,70 @@ class Action extends Model {
1822

1923
async notifyOwner() {
2024
const { trackableId, trackableType } = this;
21-
let params;
2225

2326
if (trackableType === 'Comment') {
24-
const {
25-
postId,
26-
userId
27-
} = await Comment.find(trackableId, {
28-
select: ['postId', 'userId']
29-
});
27+
const trackable = await Comment
28+
.first()
29+
.select('postId', 'userId')
30+
.where({ id: trackableId });
3031

31-
const [
32-
{ name: userName },
33-
{ userId: recipientId }
34-
] = await Promise.all([
35-
User.find(userId, { select: ['name'] }),
36-
Post.find(postId, { select: ['userId'] })
37-
]);
32+
if (trackable) {
33+
const [user, post] = await Promise.all([
34+
User
35+
.first()
36+
.select('name')
37+
.where({ id: trackable.userId }),
38+
Post
39+
.first()
40+
.select('userId')
41+
.where({ id: trackable.postId })
42+
]);
3843

39-
params = {
40-
recipientId,
41-
message: `${userName} commented on your post!`
42-
};
44+
if (user && post) {
45+
await Notification.create({
46+
message: `${user.name} commented on your post!`,
47+
recipientId: post.userId
48+
});
49+
}
50+
}
4351
} else if (trackableType === 'Reaction') {
4452
let reactableId;
45-
let reactableModel = Post;
46-
47-
const {
48-
commentId,
49-
postId,
50-
type,
51-
userId
52-
} = await Reaction.find(trackableId, {
53-
select: [
54-
'commentId',
55-
'postId',
56-
'type',
57-
'userId'
58-
]
59-
});
53+
let createMessage;
54+
let ReactableModel;
6055

61-
if (!postId) {
62-
reactableId = commentId;
63-
reactableModel = Comment;
64-
} else {
65-
reactableId = postId;
66-
}
56+
const reaction = await Reaction
57+
.first()
58+
.where({ id: trackableId });
6759

68-
const [
69-
{ name: userName },
70-
{ userId: recipientId }
71-
] = await Promise.all([
72-
User.find(userId, { select: ['name'] }),
73-
reactableModel.find(reactableId, { select: ['userId'] })
74-
]);
60+
if (reaction) {
61+
if (!reaction.postId) {
62+
ReactableModel = Comment;
63+
createMessage = createMessageTemplate('comment');
64+
reactableId = reaction.commentId;
65+
} else {
66+
ReactableModel = Post;
67+
createMessage = createMessageTemplate('post');
68+
reactableId = reaction.postId;
69+
}
7570

76-
params = {
77-
recipientId,
78-
message: `${userName} reacted with ${type} to your ` +
79-
`${reactableModel.name.toLowerCase()}!`
80-
};
81-
}
71+
const [user, reactable] = await Promise.all([
72+
User
73+
.first()
74+
.select('name')
75+
.where({ id: reaction.userId }),
76+
ReactableModel
77+
.first()
78+
.select('userId')
79+
.where({ id: reactableId })
80+
]);
8281

83-
if (params) {
84-
return await Notification.create(params);
82+
if (user && reactable) {
83+
await Notification.create({
84+
message: createMessage(user.name, reaction.type),
85+
recipientId: reactable.userId
86+
});
87+
}
88+
}
8589
}
8690
}
8791
}

examples/social-network/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
"babel-core": "6.14.0",
1414
"babel-preset-lux": "1.2.0",
1515
"bcryptjs": "2.3.0",
16-
"knex": "0.11.10",
16+
"knex": "0.12.1",
1717
"lux-framework": "1.0.0-rc.7",
1818
"sqlite3": "3.1.4"
1919
},

examples/todo/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
"dependencies": {
1313
"babel-core": "6.14.0",
1414
"babel-preset-lux": "1.2.0",
15-
"knex": "0.11.10",
15+
"knex": "0.12.1",
1616
"lux-framework": "1.0.0-rc.7",
1717
"sqlite3": "3.1.4"
1818
},

0 commit comments

Comments
 (0)