diff --git a/src/providers/esteem/esteem.js b/src/providers/esteem/esteem.js index fa6de80ff..e171a6efd 100644 --- a/src/providers/esteem/esteem.js +++ b/src/providers/esteem/esteem.js @@ -38,7 +38,11 @@ export const addDraft = data => new Promise((resolve, reject) => { export const updateDraft = data => new Promise((resolve, reject) => { api - .put(`/drafts/${data.user}/${data.draftId}`, data) + .put(`/drafts/${data.username}/${data.draftId}`, { + title: data.title, + body: data.body, + tags: data.tags, + }) .then((res) => { resolve(res.data); }) diff --git a/src/screens/editor/container/editorContainer.js b/src/screens/editor/container/editorContainer.js index e19e7c03e..3c58c916d 100644 --- a/src/screens/editor/container/editorContainer.js +++ b/src/screens/editor/container/editorContainer.js @@ -5,7 +5,7 @@ import ImagePicker from 'react-native-image-crop-picker'; // Services and Actions import { Buffer } from 'buffer'; -import { uploadImage } from '../../../providers/esteem/esteem'; +import { uploadImage, addDraft, updateDraft } from '../../../providers/esteem/esteem'; import { postContent, getPurePost } from '../../../providers/steem/dsteem'; import { setDraftPost, getDraftPost } from '../../../realm/realm'; @@ -212,33 +212,8 @@ class EditorContainer extends Component { // Media select functions <- END -> - _handleOnSaveButtonPress = (fields) => { - // const { isDraftSaved } = this.state; - // if (!isDraftSaved) { - // const { currentAccount } = this.props; - // const username = currentAccount && currentAccount.name ? currentAccount.name : ''; - - // this.setState({ isDraftSaving: true }); - // const draftField = { - // ...fields, - // tags: fields.tags.toString(), - // }; - - // setDraftPost(draftField, username) - // .then(() => { - // this.setState({ - // isDraftSaving: false, - // isDraftSaved: true, - // }); - // }) - // .catch((error) => { - // alert(error); - // }); - // } - }; - - _saveCurrentDraft = (fields) => { - const { isDraftSaved } = this.state; + _saveDraftToDB = (fields) => { + const { isDraftSaved, draftId } = this.state; if (!isDraftSaved) { const { currentAccount } = this.props; const username = currentAccount && currentAccount.name ? currentAccount.name : ''; @@ -247,21 +222,42 @@ class EditorContainer extends Component { const draftField = { ...fields, tags: fields.tags.toString(), + username, }; - setDraftPost(draftField, username) - .then(() => { + if (draftId) { + updateDraft({ ...draftField, draftId }).then(() => { this.setState({ - isDraftSaving: false, isDraftSaved: true, }); - }) - .catch((error) => { - alert(error); }); + } else { + addDraft(draftField).then((response) => { + this.setState({ + isDraftSaved: true, + draftId: response._id, + }); + }); + } + + this.setState({ + isDraftSaving: false, + }); } }; + _saveCurrentDraft = (fields) => { + const { currentAccount } = this.props; + const username = currentAccount && currentAccount.name ? currentAccount.name : ''; + + const draftField = { + ...fields, + tags: fields.tags.toString(), + }; + + setDraftPost(draftField, username); + }; + _submitPost = async (fields) => { const { navigation, currentAccount, pinCode } = this.props; @@ -416,10 +412,6 @@ class EditorContainer extends Component { } }; - _handleOnPressBackButton= () => { - alert("pressed"); - } - render() { const { isLoggedIn, isDarkTheme } = this.props; const { @@ -443,8 +435,7 @@ class EditorContainer extends Component { draftPost={draftPost} handleFormChanged={this._handleFormChanged} handleOnImagePicker={this._handleRoutingAction} - handleOnPressBackButton={this._handleOnPressBackButton} - handleOnSaveButtonPress={this._handleOnSaveButtonPress} + saveDraftToDB={this._saveDraftToDB} handleOnSubmit={this._handleSubmit} isCameraOrPickerOpen={isCameraOrPickerOpen} isDarkTheme={isDarkTheme} diff --git a/src/screens/editor/screen/editorScreen.js b/src/screens/editor/screen/editorScreen.js index 7b1cde41a..7e63cfb0a 100644 --- a/src/screens/editor/screen/editorScreen.js +++ b/src/screens/editor/screen/editorScreen.js @@ -68,10 +68,10 @@ class EditorScreen extends Component { }; _handleOnSaveButtonPress = () => { - const { handleOnSaveButtonPress } = this.props; + const { saveDraftToDB } = this.props; const { fields } = this.state; - handleOnSaveButtonPress(fields); + saveDraftToDB(fields); }; _saveCurrentDraft = () => { @@ -158,7 +158,6 @@ class EditorScreen extends Component { isUploading, post, uploadedImage, - handleOnPressBackButton, } = this.props; const rightButtonText = intl.formatMessage({ id: isEdit ? 'basic_header.update' : isReply ? 'basic_header.reply' : 'basic_header.publish', @@ -170,7 +169,6 @@ class EditorScreen extends Component { handleOnPressPreviewButton={this._handleOnPressPreviewButton} handleOnSaveButtonPress={this._handleOnSaveButtonPress} handleOnSubmit={this._handleOnSubmit} - handleOnPressBackButton={handleOnPressBackButton} isDraftSaved={isDraftSaved} isDraftSaving={isDraftSaving} isFormValid={isFormValid}