Skip to content

Commit

Permalink
Cleaned up code for ensuring post drafts are non-null (#4382)
Browse files Browse the repository at this point in the history
  • Loading branch information
hmhealey authored and crspeller committed Oct 31, 2016
1 parent b3044ba commit a8e772f
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 28 deletions.
24 changes: 2 additions & 22 deletions webapp/components/create_post.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ export default class CreatePost extends React.Component {

this.lastTime = 0;

this.getCurrentDraft = this.getCurrentDraft.bind(this);
this.handleSubmit = this.handleSubmit.bind(this);
this.postMsgKeyPress = this.postMsgKeyPress.bind(this);
this.handleChange = this.handleChange.bind(this);
Expand All @@ -61,7 +60,7 @@ export default class CreatePost extends React.Component {

PostStore.clearDraftUploads();

const draft = this.getCurrentDraft();
const draft = PostStore.getCurrentDraft();

this.state = {
channelId: ChannelStore.getCurrentId(),
Expand All @@ -77,25 +76,6 @@ export default class CreatePost extends React.Component {
};
}

getCurrentDraft() {
const draft = PostStore.getCurrentDraft();
const safeDraft = {fileInfos: [], messageText: '', uploadsInProgress: []};

if (draft) {
if (draft.message) {
safeDraft.messageText = draft.message;
}
if (draft.fileInfos) {
safeDraft.fileInfos = draft.fileInfos;
}
if (draft.uploadsInProgress) {
safeDraft.uploadsInProgress = draft.uploadsInProgress;
}
}

return safeDraft;
}

handleSubmit(e) {
e.preventDefault();

Expand Down Expand Up @@ -358,7 +338,7 @@ export default class CreatePost extends React.Component {
onChange() {
const channelId = ChannelStore.getCurrentId();
if (this.state.channelId !== channelId) {
const draft = this.getCurrentDraft();
const draft = PostStore.getCurrentDraft();

this.setState({channelId, messageText: draft.messageText, initialText: draft.messageText, submitting: false, serverError: null, postError: null, fileInfos: draft.fileInfos, uploadsInProgress: draft.uploadsInProgress});
}
Expand Down
4 changes: 2 additions & 2 deletions webapp/stores/browser_store.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ class BrowserStoreClass {
}
}

getGlobalItem(name, defaultValue) {
getGlobalItem(name, defaultValue = null) {
var result = null;
try {
if (this.isLocalStorageSupported()) {
Expand All @@ -64,7 +64,7 @@ class BrowserStoreClass {
result = null;
}

if (result === null && typeof defaultValue !== 'undefined') {
if (!result) {
result = defaultValue;
}

Expand Down
23 changes: 19 additions & 4 deletions webapp/stores/post_store.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -513,8 +513,23 @@ class PostStoreClass extends EventEmitter {
return lastPost;
}

getEmptyDraft() {
return {message: '', uploadsInProgress: [], fileInfos: []};
normalizeDraft(originalDraft) {
let draft = {
messageText: '',
uploadsInProgress: [],
fileInfos: []
};

// Make sure that the post draft is non-null and has all the required fields
if (originalDraft) {
draft = {
messageText: originalDraft.messageText || draft.messageText,
uploadsInProgress: originalDraft.uploadsInProgress || draft.uploadsInProgress,
fileInfos: originalDraft.fileInfos || draft.fileInfos
};
}

return draft;
}

storeCurrentDraft(draft) {
Expand All @@ -532,15 +547,15 @@ class PostStoreClass extends EventEmitter {
}

getDraft(channelId) {
return BrowserStore.getGlobalItem('draft_' + channelId, this.getEmptyDraft());
return this.normalizeDraft(BrowserStore.getGlobalItem('draft_' + channelId));
}

storeCommentDraft(parentPostId, draft) {
BrowserStore.setGlobalItem('comment_draft_' + parentPostId, draft);
}

getCommentDraft(parentPostId) {
return BrowserStore.getGlobalItem('comment_draft_' + parentPostId, this.getEmptyDraft());
return this.normalizeDraft(BrowserStore.getGlobalItem('comment_draft_' + parentPostId));
}

clearDraftUploads() {
Expand Down

0 comments on commit a8e772f

Please sign in to comment.