Cleaned up code for ensuring post drafts are non-null (#4382)

Этот коммит содержится в:
Harrison Healey
2016-10-31 10:52:00 -04:00
коммит произвёл Christopher Speller
родитель b3044ba4ea
Коммит a8e772fa48
3 изменённых файлов: 23 добавлений и 28 удалений

Просмотреть файл

@@ -40,7 +40,6 @@ export default class CreatePost extends React.Component {
this.lastTime = 0; this.lastTime = 0;
this.getCurrentDraft = this.getCurrentDraft.bind(this);
this.handleSubmit = this.handleSubmit.bind(this); this.handleSubmit = this.handleSubmit.bind(this);
this.postMsgKeyPress = this.postMsgKeyPress.bind(this); this.postMsgKeyPress = this.postMsgKeyPress.bind(this);
this.handleChange = this.handleChange.bind(this); this.handleChange = this.handleChange.bind(this);
@@ -61,7 +60,7 @@ export default class CreatePost extends React.Component {
PostStore.clearDraftUploads(); PostStore.clearDraftUploads();
const draft = this.getCurrentDraft(); const draft = PostStore.getCurrentDraft();
this.state = { this.state = {
channelId: ChannelStore.getCurrentId(), channelId: ChannelStore.getCurrentId(),
@@ -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) { handleSubmit(e) {
e.preventDefault(); e.preventDefault();
@@ -358,7 +338,7 @@ export default class CreatePost extends React.Component {
onChange() { onChange() {
const channelId = ChannelStore.getCurrentId(); const channelId = ChannelStore.getCurrentId();
if (this.state.channelId !== channelId) { 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}); this.setState({channelId, messageText: draft.messageText, initialText: draft.messageText, submitting: false, serverError: null, postError: null, fileInfos: draft.fileInfos, uploadsInProgress: draft.uploadsInProgress});
} }

Просмотреть файл

@@ -52,7 +52,7 @@ class BrowserStoreClass {
} }
} }
getGlobalItem(name, defaultValue) { getGlobalItem(name, defaultValue = null) {
var result = null; var result = null;
try { try {
if (this.isLocalStorageSupported()) { if (this.isLocalStorageSupported()) {
@@ -64,7 +64,7 @@ class BrowserStoreClass {
result = null; result = null;
} }
if (result === null && typeof defaultValue !== 'undefined') { if (!result) {
result = defaultValue; result = defaultValue;
} }

Просмотреть файл

@@ -513,8 +513,23 @@ class PostStoreClass extends EventEmitter {
return lastPost; return lastPost;
} }
getEmptyDraft() { normalizeDraft(originalDraft) {
return {message: '', uploadsInProgress: [], fileInfos: []}; 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) { storeCurrentDraft(draft) {
@@ -532,7 +547,7 @@ class PostStoreClass extends EventEmitter {
} }
getDraft(channelId) { getDraft(channelId) {
return BrowserStore.getGlobalItem('draft_' + channelId, this.getEmptyDraft()); return this.normalizeDraft(BrowserStore.getGlobalItem('draft_' + channelId));
} }
storeCommentDraft(parentPostId, draft) { storeCommentDraft(parentPostId, draft) {
@@ -540,7 +555,7 @@ class PostStoreClass extends EventEmitter {
} }
getCommentDraft(parentPostId) { getCommentDraft(parentPostId) {
return BrowserStore.getGlobalItem('comment_draft_' + parentPostId, this.getEmptyDraft()); return this.normalizeDraft(BrowserStore.getGlobalItem('comment_draft_' + parentPostId));
} }
clearDraftUploads() { clearDraftUploads() {