From 96deabfb27a7d0983c39d539d0b891f32b09fbfe Mon Sep 17 00:00:00 2001 From: tima101 Date: Wed, 1 May 2019 11:58:40 -0700 Subject: [PATCH] completed #23 --- api/server/api/team-member.ts | 2 -- app/components/posts/PostForm.tsx | 1 - app/lib/store/discussion.ts | 38 +++++++++++++++---------------- app/pages/discussion.tsx | 11 +++++++++ app/pages/team-settings.tsx | 3 +-- lambda/src/sendEmailForNewPost.ts | 2 -- 6 files changed, 31 insertions(+), 26 deletions(-) diff --git a/api/server/api/team-member.ts b/api/server/api/team-member.ts index 9f0e3e02..2cf17876 100644 --- a/api/server/api/team-member.ts +++ b/api/server/api/team-member.ts @@ -205,8 +205,6 @@ router.post('/posts/edit', async (req, res, next) => { postEdited({ socketId, post: updatedPost }); - // console.log(socketId); - res.json({ done: 1 }); } catch (err) { next(err); diff --git a/app/components/posts/PostForm.tsx b/app/components/posts/PostForm.tsx index d9fffef3..977828b4 100644 --- a/app/components/posts/PostForm.tsx +++ b/app/components/posts/PostForm.tsx @@ -191,7 +191,6 @@ class PostForm extends React.Component { if (discussion.notificationType === 'email') { const userIdsForLambda = discussion.memberIds.filter(m => m !== discussion.createdUserId); - console.log(discussion.notificationType, userIdsForLambda); await discussion.sendDataToLambdaApiMethod({ discussionName: discussion.name, discussionLink: `${URL_APP}/team/${discussion.team.slug}/discussions/${discussion.slug}`, diff --git a/app/lib/store/discussion.ts b/app/lib/store/discussion.ts index d943b700..739929c1 100644 --- a/app/lib/store/discussion.ts +++ b/app/lib/store/discussion.ts @@ -109,10 +109,6 @@ class Discussion { const postObj = new Post({ discussion: this, store: this.store, ...data }); - if (postObj.createdUserId !== this.store.currentUser._id) { - return; - } - this.posts.push(postObj); return postObj; @@ -210,6 +206,19 @@ class Discussion { this.team.discussions.remove(discussion); } + public handleDiscussionRealtimeEvent = data => { + console.log('discussion realtime event', data); + const { action: actionName } = data; + + if (actionName === 'added') { + this.addDiscussionToLocalCache(data.discussion); + } else if (actionName === 'edited') { + this.editDiscussionFromLocalCache(data.discussion); + } else if (actionName === 'deleted') { + this.removeDiscussionFromLocalCache(data.id); + } + }; + public handlePostRealtimeEvent(data) { const { action: actionName } = data; @@ -224,13 +233,17 @@ class Discussion { public leaveSocketRoom() { if (this.store.socket) { - this.store.socket.off('discussionEvent', this.handleDiscussionRealtimeEvent); + console.log('leaving socket discussion room', this.name); + this.store.socket.emit('leaveDiscussion', this._id); + this.store.socket.emit('leaveTeam', this.team._id); } } public joinSocketRoom() { if (this.store.socket) { - this.store.socket.on('discussionEvent', this.handleDiscussionRealtimeEvent); + console.log('joining socket discussion room', this.name); + this.store.socket.emit('joinDiscussion', this._id); + this.store.socket.emit('joinTeam', this.team._id); } } @@ -243,19 +256,6 @@ class Discussion { discussionObjs.filter(d => !d.isDraft || d.createdUserId === this.store.currentUser._id), ); } - - private handleDiscussionRealtimeEvent = data => { - console.log('discussion realtime event', data); - const { action: actionName } = data; - - if (actionName === 'added') { - this.addDiscussionToLocalCache(data.discussion); - } else if (actionName === 'edited') { - this.editDiscussionFromLocalCache(data.discussion); - } else if (actionName === 'deleted') { - this.removeDiscussionFromLocalCache(data.id); - } - }; } decorate(Discussion, { diff --git a/app/pages/discussion.tsx b/app/pages/discussion.tsx index c3afef00..6936de2f 100644 --- a/app/pages/discussion.tsx +++ b/app/pages/discussion.tsx @@ -37,6 +37,7 @@ class DiscussionComp extends React.Component { store.currentTeam.loadDiscussions().catch(err => notify(err)); } + this.props.store.socket.on('discussionEvent', this.handleDiscussionEvent); this.props.store.socket.on('postEvent', this.handlePostEvent); this.props.store.socket.on('reconnect', this.handleSocketReconnect); @@ -73,6 +74,7 @@ class DiscussionComp extends React.Component { discussion.leaveSocketRoom(); } + this.props.store.socket.off('discussionEvent', this.handleDiscussionEvent); this.props.store.socket.off('postEvent', this.handlePostEvent); this.props.store.socket.off('reconnect', this.handleSocketReconnect); } @@ -293,6 +295,15 @@ class DiscussionComp extends React.Component { this.setState({ selectedPost: post, showMarkdownClicked: true }); }; + private handleDiscussionEvent = data => { + console.log('discussion realtime event', data); + + const discussion = this.getDiscussion(this.props.discussionSlug); + if (discussion) { + discussion.handleDiscussionRealtimeEvent(data); + } + }; + private handlePostEvent = data => { console.log('post realtime event', data); diff --git a/app/pages/team-settings.tsx b/app/pages/team-settings.tsx index 48d79299..b21fc1a6 100644 --- a/app/pages/team-settings.tsx +++ b/app/pages/team-settings.tsx @@ -2,11 +2,10 @@ import Avatar from '@material-ui/core/Avatar'; import Button from '@material-ui/core/Button'; import Grid from '@material-ui/core/Grid'; import Hidden from '@material-ui/core/Hidden'; -import Paper from '@material-ui/core/Paper'; import TextField from '@material-ui/core/TextField'; -import NProgress from 'nprogress'; import { observer } from 'mobx-react'; import Head from 'next/head'; +import NProgress from 'nprogress'; import * as React from 'react'; import Table from '@material-ui/core/Table'; diff --git a/lambda/src/sendEmailForNewPost.ts b/lambda/src/sendEmailForNewPost.ts index c2281505..289224f3 100644 --- a/lambda/src/sendEmailForNewPost.ts +++ b/lambda/src/sendEmailForNewPost.ts @@ -49,8 +49,6 @@ async function sendEmailNotification({ .select('email') .setOptions({ lean: true }); - console.log('users', users); - const usersToNotify = users.filter(user => userIds.includes(user._id.toString())); console.log('usersToNotify', usersToNotify);