Skip to content

Commit 6b3974b

Browse files
author
Vikas Agarwal
committed
Github #166, Messaging: unposted content alert
-- Implemented the required behaviour with Dashboard page. -- Tried to handle the warning of multiple leave hooks for the same route, however, the solution didn't work. Moving ahead as right now it is a warning and not causing any problem. May be we can launch a challenge to get it fixed.
1 parent 396cd01 commit 6b3974b

File tree

4 files changed

+78
-8
lines changed

4 files changed

+78
-8
lines changed

src/components/Feed/NewPost.jsx

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,8 @@ class NewPost extends React.Component {
128128
this.setState({editorState})
129129
this.validateSubmitState()
130130
if (this.props.onNewPostChange) {
131-
this.props.onNewPostChange(this.refs.title.value, stateToMarkdown(editorState.getCurrentContent()))
131+
// NOTE: uses getPlainText method to avoid newline character for empty content
132+
this.props.onNewPostChange(this.refs.title.value, editorState.getCurrentContent().getPlainText())
132133
}
133134
}
134135

@@ -143,7 +144,8 @@ class NewPost extends React.Component {
143144
const { editorState } = this.state
144145
this.validateSubmitState()
145146
if (this.props.onNewPostChange) {
146-
this.props.onNewPostChange(this.refs.title.value, stateToMarkdown(editorState.getCurrentContent()))
147+
// NOTE: uses getPlainText method to avoid newline character for empty content
148+
this.props.onNewPostChange(this.refs.title.value, editorState.getCurrentContent().getPlainText())
147149
}
148150
}
149151

src/projects/detail/Dashboard.jsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import Sticky from 'react-stickynode'
55

66
require('./Dashboard.scss')
77

8-
const Dashboard = ({project, currentMemberRole}) => (
8+
const Dashboard = ({project, currentMemberRole, route}) => (
99
<div>
1010
<div className="dashboard-container">
1111
<div className="left-area">
@@ -16,7 +16,7 @@ const Dashboard = ({project, currentMemberRole}) => (
1616
</Sticky>
1717
</div>
1818
<div className="right-area">
19-
<FeedContainer currentMemberRole={currentMemberRole} project={project} />
19+
<FeedContainer currentMemberRole={currentMemberRole} project={project} route={route} />
2020
</div>
2121
</div>
2222
</div>

src/projects/detail/containers/FeedContainer.js

Lines changed: 40 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import React, { PropTypes } from 'react'
2+
import { withRouter } from 'react-router'
23
import _ from 'lodash'
34
import {
45
THREAD_MESSAGES_PAGE_SIZE,
@@ -36,7 +37,16 @@ class FeedView extends React.Component {
3637
this.onNewCommentChange = this.onNewCommentChange.bind(this)
3738
this.onShowAllComments = this.onShowAllComments.bind(this)
3839
this.onAddNewComment = this.onAddNewComment.bind(this)
39-
this.state = { feeds : [], showAll: [] }
40+
this.onLeave = this.onLeave.bind(this)
41+
this.isChanged = this.isChanged.bind(this)
42+
this.onNewPostChange = this.onNewPostChange.bind(this)
43+
this.state = { feeds : [], showAll: [], newPost: {} }
44+
}
45+
46+
componentDidMount() {
47+
const routeLeaveHook = this.props.router.setRouteLeaveHook(this.props.route, this.onLeave)
48+
window.addEventListener('beforeunload', this.onLeave)
49+
this.setState({ routeLeaveHook })
4050
}
4151

4252
componentWillMount() {
@@ -47,6 +57,27 @@ class FeedView extends React.Component {
4757
this.init(nextProps)
4858
}
4959

60+
componentWillUnmount() {
61+
if (this.state.routeLeaveHook) {
62+
this.state.routeLeaveHook()
63+
}
64+
window.removeEventListener('beforeunload', this.onLeave)
65+
}
66+
67+
// Notify user if they navigate away while the form is modified.
68+
onLeave(e) {
69+
if (this.isChanged()) {
70+
return e.returnValue = 'You have uposted content. Are you sure you want to leave?'
71+
}
72+
}
73+
74+
isChanged() {
75+
const { newPost } = this.state
76+
const hasComment = !_.isUndefined(_.find(this.state.feeds, (feed) => feed.newComment && feed.newComment.length))
77+
const hasThread = (newPost.title && !!newPost.title.trim().length) || ( newPost.content && !!newPost.content.trim().length)
78+
return hasThread || hasComment
79+
}
80+
5081
mapFeed(feed, showAll = false) {
5182
const { allMembers } = this.props
5283
const item = _.pick(feed, ['id', 'date', 'read', 'tag', 'title', 'totalPosts', 'userId', 'reference', 'referenceId', 'postIds', 'isAddingComment', 'isLoadingComments', 'error'])
@@ -99,6 +130,12 @@ class FeedView extends React.Component {
99130
})
100131
}
101132

133+
onNewPostChange(title, content) {
134+
this.setState({
135+
newPost: {title, content}
136+
})
137+
}
138+
102139
onNewPost({title, content}) {
103140
const { project } = this.props
104141
const newFeed = {
@@ -194,6 +231,7 @@ class FeedView extends React.Component {
194231
isCreating={ isCreatingFeed }
195232
hasError={ error }
196233
heading="NEW STATUS POST"
234+
onNewPostChange={this.onNewPostChange}
197235
titlePlaceholder="Share the latest project updates with the team"
198236
/>
199237
}
@@ -203,7 +241,7 @@ class FeedView extends React.Component {
203241
}
204242
}
205243
const enhance = spinnerWhileLoading(props => !props.isLoading)
206-
const EnhancedFeedView = enhance(FeedView)
244+
const EnhancedFeedView = withRouter(enhance(FeedView))
207245

208246

209247
class FeedContainer extends React.Component {

src/projects/detail/containers/MessagesContainer.js

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,11 +47,14 @@ class MessagesView extends React.Component {
4747
this.isChanged = this.isChanged.bind(this)
4848
this.onNewPostChange = this.onNewPostChange.bind(this)
4949
this.changeThread = this.changeThread.bind(this)
50+
this.onNewThreadClick = this.onNewThreadClick.bind(this)
51+
this.showNewThreadForm = this.showNewThreadForm.bind(this)
5052
}
5153

5254
componentDidMount() {
53-
this.props.router.setRouteLeaveHook(this.props.route, this.onLeave)
55+
const routeLeaveHook = this.props.router.setRouteLeaveHook(this.props.route, this.onLeave)
5456
window.addEventListener('beforeunload', this.onLeave)
57+
this.setState({ routeLeaveHook })
5558
}
5659

5760
componentWillMount() {
@@ -64,6 +67,9 @@ class MessagesView extends React.Component {
6467

6568
componentWillUnmount() {
6669
window.removeEventListener('beforeunload', this.onLeave)
70+
if (this.state.routeLeaveHook) {
71+
this.state.routeLeaveHook()
72+
}
6773
}
6874

6975
// Notify user if they navigate away while the form is modified.
@@ -208,6 +214,30 @@ class MessagesView extends React.Component {
208214
})
209215
}
210216

217+
onNewThreadClick() {
218+
const unsavedContentMsg = this.onLeave({})
219+
if (unsavedContentMsg) {
220+
const changeConfirmed = confirm(unsavedContentMsg)
221+
if (changeConfirmed) {
222+
this.showNewThreadForm()
223+
}
224+
} else {
225+
this.showNewThreadForm()
226+
}
227+
}
228+
229+
showNewThreadForm() {
230+
this.setState({
231+
isCreateNewMessage: true,
232+
threads: this.state.threads.map((item) => {
233+
if (item.isActive) {
234+
return {...item, newMessage: ''}
235+
}
236+
return item
237+
})
238+
})
239+
}
240+
211241
onNewMessageChange(content) {
212242
this.setState({
213243
threads: this.state.threads.map((item) => {
@@ -281,7 +311,7 @@ class MessagesView extends React.Component {
281311
<div className="messages-container">
282312
<div className="left-area">
283313
<MessageList
284-
onAdd={() => this.setState({isCreateNewMessage: true})}
314+
onAdd={ this.onNewThreadClick }
285315
threads={threads}
286316
onSelect={this.onThreadSelect}
287317
showAddButton={ !!currentMemberRole }

0 commit comments

Comments
 (0)