saving local draft only when editor exits or remote draft save fails

This commit is contained in:
Nouman Tahir 2022-07-21 15:54:24 +05:00
parent fb973b9a46
commit 6ab79306c7
2 changed files with 16 additions and 13 deletions

View File

@ -55,6 +55,7 @@ import { deleteDraftCacheEntry, updateCommentCache, updateDraftCache } from '../
class EditorContainer extends Component<any, any> { class EditorContainer extends Component<any, any> {
_isMounted = false; _isMounted = false;
_updatedDraftFields = null;
constructor(props) { constructor(props) {
super(props); super(props);
@ -571,10 +572,9 @@ class EditorContainer extends Component<any, any> {
const { isDraftSaved, draftId, thumbIndex, isReply, rewardType } = this.state; const { isDraftSaved, draftId, thumbIndex, isReply, rewardType } = this.state;
const { currentAccount, dispatch, intl } = this.props; const { currentAccount, dispatch, intl } = this.props;
//Saves draft locally for both reply and post
this._saveCurrentDraft(fields)
if (isReply) { if (isReply) {
this._saveCurrentDraft(this._updatedDraftFields)
return; return;
} }
@ -658,6 +658,9 @@ class EditorContainer extends Component<any, any> {
isDraftSaving: false, isDraftSaving: false,
isDraftSaved: false, isDraftSaved: false,
}); });
//saves draft locally if remote draft save fails
this._saveCurrentDraft(this._updatedDraftFields)
} }
dispatch( dispatch(
@ -671,6 +674,10 @@ class EditorContainer extends Component<any, any> {
}; };
_updateDraftFields = (fields) => {
this._updatedDraftFields = fields;
}
_saveCurrentDraft = async (fields) => { _saveCurrentDraft = async (fields) => {
const { draftId, isReply, isEdit, isPostSending } = this.state; const { draftId, isReply, isEdit, isPostSending } = this.state;
@ -1294,6 +1301,7 @@ class EditorContainer extends Component<any, any> {
quickReplyText={quickReplyText} quickReplyText={quickReplyText}
isUploading={isUploading} isUploading={isUploading}
post={post} post={post}
updateDraftFields = {this._updateDraftFields}
saveCurrentDraft={this._saveCurrentDraft} saveCurrentDraft={this._saveCurrentDraft}
saveDraftToDB={this._saveDraftToDB} saveDraftToDB={this._saveDraftToDB}
uploadedImage={uploadedImage} uploadedImage={uploadedImage}
@ -1309,6 +1317,7 @@ class EditorContainer extends Component<any, any> {
rewardType={rewardType} rewardType={rewardType}
scheduledForDate={scheduledForDate} scheduledForDate={scheduledForDate}
getBeneficiaries={this._extractBeneficiaries} getBeneficiaries={this._extractBeneficiaries}
/> />
); );
} }

View File

@ -9,17 +9,11 @@ import { extractMetadata, getWordsCount, makeJsonMetadata } from '../../../utils
// Components // Components
import { import {
BasicHeader, BasicHeader,
TitleArea,
TagArea,
TagInput,
SummaryArea,
PostForm, PostForm,
MarkdownEditor, MarkdownEditor,
SelectCommunityAreaView, SelectCommunityAreaView,
SelectCommunityModalContainer, SelectCommunityModalContainer,
Modal, Modal,
UserAvatar,
MainButton,
} from '../../../components'; } from '../../../components';
// dhive // dhive
@ -31,7 +25,6 @@ import globalStyles from '../../../globalStyles';
import { isCommunity } from '../../../utils/communityValidation'; import { isCommunity } from '../../../utils/communityValidation';
import styles from './editorScreenStyles'; import styles from './editorScreenStyles';
import ThumbSelectionModal from '../children/thumbSelectionModal';
import PostOptionsModal from '../children/postOptionsModal'; import PostOptionsModal from '../children/postOptionsModal';
class EditorScreen extends Component { class EditorScreen extends Component {
@ -176,14 +169,15 @@ class EditorScreen extends Component {
}; };
_saveCurrentDraft = (fields) => { _saveCurrentDraft = (fields) => {
const { saveCurrentDraft } = this.props; const { saveCurrentDraft, updateDraftFields } = this.props;
if (this.changeTimer) { if (this.changeTimer) {
clearTimeout(this.changeTimer); clearTimeout(this.changeTimer);
} }
this.changeTimer = setTimeout(() => { this.changeTimer = setTimeout(() => {
saveCurrentDraft(fields); // saveCurrentDraft(fields);
updateDraftFields(fields)
}, 300); }, 300);
}; };
@ -276,8 +270,8 @@ class EditorScreen extends Component {
) { ) {
console.log('jsonMeta : ', jsonMeta); console.log('jsonMeta : ', jsonMeta);
handleFormChanged(); handleFormChanged();
//TODO: REMOVED FOR TESTING, CAN BE PUT BACK IF APP STILL CRASHES
// this._saveCurrentDraft(fields); this._saveCurrentDraft(fields);
} }
this.setState({ fields }, () => { this.setState({ fields }, () => {