Option to save draft as new

This commit is contained in:
noumantahir 2022-01-08 21:32:54 +05:00
parent 986e351c89
commit 18d4e37ac5
4 changed files with 41 additions and 18 deletions
src
config/locales
realm
screens/editor

View File

@ -337,7 +337,10 @@
"scheduled_immediate":"Immediate",
"scheduled_later":"Later",
"settings_title":"Post Options",
"done":"DONE"
"done":"DONE",
"draft_save_title":"Save Existing Draft?",
"draft_overwrite":"Overwrite Draft",
"draft_save_new":"Save As New"
},
"snippets":{
"label_no_snippets":"No Snippets Found",

View File

@ -127,7 +127,7 @@ export const setDraftPost = async (fields, username, draftId) => {
const data = {
username,
draftId,
timestamp,
timestamp: fields.timestamp === 0 ? 0 : timestamp,
title: fields.title,
tags: fields.tags,
body: fields.body,

View File

@ -470,7 +470,7 @@ class EditorContainer extends Component {
}
};
_saveDraftToDB = async (fields, silent = false) => {
_saveDraftToDB = async (fields, saveAsNew = false) => {
const { isDraftSaved, draftId, thumbIndex } = this.state;
const { currentAccount, dispatch, intl } = this.props;
@ -480,7 +480,7 @@ class EditorContainer extends Component {
if (!isDraftSaved) {
let draftField;
if (this._isMounted && !silent) {
if (this._isMounted) {
this.setState({
isDraftSaving: true,
});
@ -494,7 +494,7 @@ class EditorContainer extends Component {
}
//update draft is draftId is present
if (draftId && draftField) {
if (draftId && draftField && !saveAsNew) {
await updateDraft(draftId, draftField.title, draftField.body, draftField.tags, thumbIndex);
if (this._isMounted) {
@ -520,7 +520,7 @@ class EditorContainer extends Component {
dispatch(setBeneficiaries(response._id, beneficiaries));
dispatch(removeBeneficiaries(TEMP_BENEFICIARIES_ID));
//clear local copy is darft save is successful
//clear local copy if darft save is successful
const username = get(currentAccount, 'name', '');
setDraftPost(
{
@ -530,18 +530,19 @@ class EditorContainer extends Component {
timestamp: 0,
},
username,
saveAsNew ? draftId : undefined
);
}
if (!silent) {
dispatch(
toastNotification(
intl.formatMessage({
id: 'editor.draft_save_success',
}),
),
);
}
dispatch(
toastNotification(
intl.formatMessage({
id: 'editor.draft_save_success',
}),
),
);
//call fetch post to drafts screen
this._navigationBackFetchDrafts();

View File

@ -1,5 +1,5 @@
import React, { Component } from 'react';
import { View } from 'react-native';
import { Alert, View } from 'react-native';
import { injectIntl } from 'react-intl';
import { get, isNull } from 'lodash';
@ -151,6 +151,25 @@ class EditorScreen extends Component {
};
_handleOnSaveButtonPress = () => {
const {draftId, intl} = this.props;
if(draftId){
Alert.alert(
intl.formatMessage({id:'editor.draft_save_title'}),
"",
[{
text:intl.formatMessage({id:'editor.draft_overwrite'}),
onPress:()=>this._saveDraftToDB(),
},{
text:intl.formatMessage({id:'editor.draft_save_new'}),
onPress:()=>this._saveDraftToDB(true)
},{
text:intl.formatMessage({id:'alert.cancel'}),
onPress:()=>{},
style:'cancel'
}]
)
return;
}
this._saveDraftToDB();
};
@ -317,13 +336,13 @@ class EditorScreen extends Component {
});
};
_saveDraftToDB() {
_saveDraftToDB(saveAsNew?:boolean) {
const { saveDraftToDB } = this.props;
const { fields } = this.state;
//save draft only if any of field is valid
if (fields.body || fields.title) {
saveDraftToDB(fields);
saveDraftToDB(fields, saveAsNew);
}
}