mirror of
https://github.com/ecency/ecency-mobile.git
synced 2024-12-24 13:53:23 +03:00
replaced setDraftPost, getDraftPost with redux based drafts
This commit is contained in:
parent
6b859aaaea
commit
12a4f1e7af
@ -113,6 +113,7 @@ export const UPDATE_COMMENT_CACHE = 'UPDATE_COMMENT_CACHE';
|
|||||||
export const DELETE_COMMENT_CACHE_ENTRY = 'DELETE_COMMENT_CACHE_ENTRY';
|
export const DELETE_COMMENT_CACHE_ENTRY = 'DELETE_COMMENT_CACHE_ENTRY';
|
||||||
export const UPDATE_DRAFT_CACHE = 'UPDATE_DRAFT_CACHE';
|
export const UPDATE_DRAFT_CACHE = 'UPDATE_DRAFT_CACHE';
|
||||||
export const DELETE_DRAFT_CACHE_ENTRY = 'DELETE_DRAFT_CACHE_ENTRY';
|
export const DELETE_DRAFT_CACHE_ENTRY = 'DELETE_DRAFT_CACHE_ENTRY';
|
||||||
|
export const DEFAULT_USER_DRAFT_ID = 'DEFAULT_USER_DRAFT_ID_';
|
||||||
|
|
||||||
// TOOLTIPS
|
// TOOLTIPS
|
||||||
export const REGISTER_TOOLTIP = 'REGISTER_TOOLTIP';
|
export const REGISTER_TOOLTIP = 'REGISTER_TOOLTIP';
|
||||||
|
@ -1,7 +1,7 @@
|
|||||||
import React, { Component } from 'react';
|
import React, { Component } from 'react';
|
||||||
import { connect } from 'react-redux';
|
import { connect } from 'react-redux';
|
||||||
import { injectIntl } from 'react-intl';
|
import { injectIntl } from 'react-intl';
|
||||||
import { Alert, Platform } from 'react-native';
|
import { Alert } from 'react-native';
|
||||||
import ImagePicker from 'react-native-image-crop-picker';
|
import ImagePicker from 'react-native-image-crop-picker';
|
||||||
import get from 'lodash/get';
|
import get from 'lodash/get';
|
||||||
import AsyncStorage from '@react-native-community/async-storage';
|
import AsyncStorage from '@react-native-community/async-storage';
|
||||||
@ -25,7 +25,6 @@ import {
|
|||||||
reblog,
|
reblog,
|
||||||
postComment,
|
postComment,
|
||||||
} from '../../../providers/hive/dhive';
|
} from '../../../providers/hive/dhive';
|
||||||
import { setDraftPost, getDraftPost } from '../../../realm/realm';
|
|
||||||
|
|
||||||
// Constants
|
// Constants
|
||||||
import { default as ROUTES } from '../../../constants/routeNames';
|
import { default as ROUTES } from '../../../constants/routeNames';
|
||||||
@ -45,8 +44,8 @@ import {
|
|||||||
import EditorScreen from '../screen/editorScreen';
|
import EditorScreen from '../screen/editorScreen';
|
||||||
import bugsnapInstance from '../../../config/bugsnag';
|
import bugsnapInstance from '../../../config/bugsnag';
|
||||||
import { removeBeneficiaries, setBeneficiaries } from '../../../redux/actions/editorActions';
|
import { removeBeneficiaries, setBeneficiaries } from '../../../redux/actions/editorActions';
|
||||||
import { TEMP_BENEFICIARIES_ID } from '../../../redux/constants/constants';
|
import { DEFAULT_USER_DRAFT_ID, TEMP_BENEFICIARIES_ID } from '../../../redux/constants/constants';
|
||||||
import { updateCommentCache, updateDraftCache } from '../../../redux/actions/cacheActions';
|
import { deleteDraftCacheEntry, updateCommentCache, updateDraftCache } from '../../../redux/actions/cacheActions';
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Props Name Description Value
|
* Props Name Description Value
|
||||||
@ -139,8 +138,8 @@ class EditorContainer extends Component<any, any> {
|
|||||||
draftId,
|
draftId,
|
||||||
autoFocusText: true,
|
autoFocusText: true,
|
||||||
});
|
});
|
||||||
if(draftId){
|
if (draftId) {
|
||||||
this._getStorageDraft(username, isReply, {_id:draftId});
|
this._getStorageDraft(username, isReply, { _id: draftId });
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -197,51 +196,57 @@ class EditorContainer extends Component<any, any> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
_getStorageDraft = async (username, isReply, paramDraft) => {
|
_getStorageDraft = async (username, isReply, paramDraft) => {
|
||||||
|
const { drafts } = this.props;
|
||||||
|
|
||||||
if (isReply) {
|
if (isReply) {
|
||||||
//TODO: get draft reply from redux based on draft passed in param;
|
const _draft = drafts.get(paramDraft._id);
|
||||||
const {drafts} = this.props;
|
if (_draft && _draft.body) {
|
||||||
const draft = drafts.get(paramDraft._id);
|
|
||||||
if (draft && draft.body) {
|
|
||||||
this.setState({
|
this.setState({
|
||||||
draftPost: {
|
draftPost: {
|
||||||
body: draft.body,
|
body: _draft.body,
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
//TOOD: get draft from redux after reply side is complete
|
//TOOD: get draft from redux after reply side is complete
|
||||||
getDraftPost(username, paramDraft && paramDraft._id).then((result) => {
|
const _draftId = paramDraft ? paramDraft._id : DEFAULT_USER_DRAFT_ID + username;
|
||||||
//if result is return and param draft available, compare timestamp, use latest
|
const _localDraft = drafts.get(_draftId);
|
||||||
//if no draft, use result anayways
|
if (!_localDraft) {
|
||||||
if (result && (!paramDraft || paramDraft.timestamp < result.timestamp)) {
|
return;
|
||||||
this.setState({
|
}
|
||||||
draftPost: {
|
|
||||||
body: get(result, 'body', ''),
|
|
||||||
title: get(result, 'title', ''),
|
|
||||||
tags: get(result, 'tags', '').split(','),
|
|
||||||
isDraft: paramDraft ? true : false,
|
|
||||||
draftId: paramDraft ? paramDraft._id : null,
|
|
||||||
},
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
//if above fails with either no result returned or timestamp is old,
|
//if _draft is returned and param draft is available, compare timestamp, use latest
|
||||||
// and use draft form nav param if available.
|
//if no draft, use result anayways
|
||||||
else if (paramDraft) {
|
|
||||||
const _tags = paramDraft.tags.includes(' ')
|
if (_localDraft && (!paramDraft || paramDraft.timestamp < _localDraft.updated)) {
|
||||||
? paramDraft.tags.split(' ')
|
this.setState({
|
||||||
: paramDraft.tags.split(',');
|
draftPost: {
|
||||||
this.setState({
|
body: get(_localDraft, 'body', ''),
|
||||||
draftPost: {
|
title: get(_localDraft, 'title', ''),
|
||||||
title: paramDraft.title,
|
tags: get(_localDraft, 'tags', '').split(','),
|
||||||
body: paramDraft.body,
|
isDraft: paramDraft ? true : false,
|
||||||
tags: _tags,
|
draftId: paramDraft ? paramDraft._id : null,
|
||||||
},
|
},
|
||||||
isDraft: true,
|
});
|
||||||
draftId: paramDraft._id,
|
}
|
||||||
});
|
|
||||||
}
|
//if above fails with either no result returned or timestamp is old,
|
||||||
});
|
// and use draft form nav param if available.
|
||||||
|
else if (paramDraft) {
|
||||||
|
const _tags = paramDraft.tags.includes(' ')
|
||||||
|
? paramDraft.tags.split(' ')
|
||||||
|
: paramDraft.tags.split(',');
|
||||||
|
this.setState({
|
||||||
|
draftPost: {
|
||||||
|
title: paramDraft.title,
|
||||||
|
body: paramDraft.body,
|
||||||
|
tags: _tags,
|
||||||
|
},
|
||||||
|
isDraft: true,
|
||||||
|
draftId: paramDraft._id,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -256,14 +261,14 @@ class EditorContainer extends Component<any, any> {
|
|||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* this fucntion is run if editor is access used mid tab or reply section
|
* this fucntion is run if editor is access fused mid tab or reply section
|
||||||
* it fetches fresh drafts and run some comparions to load one of following
|
* it fetches fresh drafts and run some comparions to load one of following
|
||||||
* empty editor, load non-remote draft or most recent remote draft based on timestamps
|
* empty editor, load non-remote draft or most recent remote draft based on timestamps
|
||||||
* prompts user as well
|
* prompts user as well
|
||||||
* @param isReply
|
* @param isReply
|
||||||
**/
|
**/
|
||||||
_fetchDraftsForComparison = async (isReply) => {
|
_fetchDraftsForComparison = async (isReply) => {
|
||||||
const { currentAccount, isLoggedIn, intl, dispatch } = this.props;
|
const { currentAccount, isLoggedIn, intl, dispatch, drafts } = this.props;
|
||||||
const username = get(currentAccount, 'name', '');
|
const username = get(currentAccount, 'name', '');
|
||||||
|
|
||||||
//initilizes editor with reply or non remote id less draft
|
//initilizes editor with reply or non remote id less draft
|
||||||
@ -287,28 +292,29 @@ class EditorContainer extends Component<any, any> {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
const drafts = await getDrafts(username);
|
const remoteDrafts = await getDrafts(username);
|
||||||
const idLessDraft = await getDraftPost(username);
|
|
||||||
|
const idLessDraft = drafts.get(DEFAULT_USER_DRAFT_ID + username)
|
||||||
|
|
||||||
const loadRecentDraft = () => {
|
const loadRecentDraft = () => {
|
||||||
//if no draft available means local draft is recent
|
//if no draft available means local draft is recent
|
||||||
if (drafts.length == 0) {
|
if (remoteDrafts.length == 0) {
|
||||||
_getStorageDraftGeneral(false);
|
_getStorageDraftGeneral(false);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
//sort darts based on timestamps
|
//sort darts based on timestamps
|
||||||
drafts.sort((d1, d2) =>
|
remoteDrafts.sort((d1, d2) =>
|
||||||
new Date(d1.modified).getTime() < new Date(d2.modified).getTime() ? 1 : -1,
|
new Date(d1.modified).getTime() < new Date(d2.modified).getTime() ? 1 : -1,
|
||||||
);
|
);
|
||||||
const _draft = drafts[0];
|
const _draft = remoteDrafts[0];
|
||||||
|
|
||||||
//if unsaved local draft is more latest then remote draft, use that instead
|
//if unsaved local draft is more latest then remote draft, use that instead
|
||||||
//if editor was opened from draft screens, this code will be skipped anyways.
|
//if editor was opened from draft screens, this code will be skipped anyways.
|
||||||
if (
|
if (
|
||||||
idLessDraft &&
|
idLessDraft &&
|
||||||
(idLessDraft.title !== '' || idLessDraft.tags !== '' || idLessDraft.body !== '') &&
|
(idLessDraft.title !== '' || idLessDraft.tags !== '' || idLessDraft.body !== '') &&
|
||||||
new Date(_draft.modified).getTime() < idLessDraft.timestamp
|
new Date(_draft.modified).getTime() < idLessDraft.updated
|
||||||
) {
|
) {
|
||||||
_getStorageDraftGeneral(false);
|
_getStorageDraftGeneral(false);
|
||||||
return;
|
return;
|
||||||
@ -322,7 +328,7 @@ class EditorContainer extends Component<any, any> {
|
|||||||
this._getStorageDraft(username, isReply, _draft);
|
this._getStorageDraft(username, isReply, _draft);
|
||||||
};
|
};
|
||||||
|
|
||||||
if (drafts.length > 0 || (idLessDraft && idLessDraft.timestamp > 0)) {
|
if (remoteDrafts.length > 0 || (idLessDraft && idLessDraft.updated > 0)) {
|
||||||
this.setState({
|
this.setState({
|
||||||
onLoadDraftPress: loadRecentDraft,
|
onLoadDraftPress: loadRecentDraft,
|
||||||
});
|
});
|
||||||
@ -531,7 +537,7 @@ class EditorContainer extends Component<any, any> {
|
|||||||
const { isDraftSaved, draftId, thumbIndex, isReply } = this.state;
|
const { isDraftSaved, draftId, thumbIndex, isReply } = this.state;
|
||||||
const { currentAccount, dispatch, intl } = this.props;
|
const { currentAccount, dispatch, intl } = this.props;
|
||||||
|
|
||||||
if(isReply){
|
if (isReply) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -584,16 +590,8 @@ class EditorContainer extends Component<any, any> {
|
|||||||
|
|
||||||
//clear local copy if darft save is successful
|
//clear local copy if darft save is successful
|
||||||
const username = get(currentAccount, 'name', '');
|
const username = get(currentAccount, 'name', '');
|
||||||
setDraftPost(
|
|
||||||
{
|
dispatch(deleteDraftCacheEntry(draftId || (DEFAULT_USER_DRAFT_ID + username)))
|
||||||
title: '',
|
|
||||||
body: '',
|
|
||||||
tags: '',
|
|
||||||
timestamp: 0,
|
|
||||||
},
|
|
||||||
username,
|
|
||||||
saveAsNew ? draftId : undefined
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@ -636,39 +634,28 @@ class EditorContainer extends Component<any, any> {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
const { currentAccount } = this.props;
|
const { currentAccount, dispatch } = this.props;
|
||||||
const username = currentAccount && currentAccount.name ? currentAccount.name : '';
|
const username = currentAccount && currentAccount.name ? currentAccount.name : '';
|
||||||
|
|
||||||
const draftField = {
|
const draftField = {
|
||||||
...fields,
|
title: fields.title,
|
||||||
|
body: fields.body,
|
||||||
tags: fields.tags && fields.tags.length > 0 ? fields.tags.toString() : '',
|
tags: fields.tags && fields.tags.length > 0 ? fields.tags.toString() : '',
|
||||||
};
|
author: username,
|
||||||
|
}
|
||||||
|
|
||||||
//save reply data
|
//save reply data
|
||||||
if (isReply && draftField.body !== null) {
|
if (isReply && draftField.body !== null) {
|
||||||
//TODO: update draft item in redux using draftId passed in params
|
dispatch(updateDraftCache(draftId, draftField))
|
||||||
const {dispatch} = this.props;
|
|
||||||
const {draftId} = this.state;
|
|
||||||
|
|
||||||
const replyDraft = {
|
|
||||||
author:currentAccount.name,
|
|
||||||
body:fields.body,
|
|
||||||
updated: new Date().toISOString().substring(0, 19),
|
|
||||||
expiresAt: new Date().getTime() + 604800000, // 7 days expiry time
|
|
||||||
}
|
|
||||||
|
|
||||||
dispatch(updateDraftCache(draftId, replyDraft))
|
|
||||||
//await AsyncStorage.setItem('temp-reply', draftField.body);
|
|
||||||
|
|
||||||
|
|
||||||
//save existing draft data locally
|
//save existing draft data locally
|
||||||
} else if (draftId) {
|
} else if (draftId) {
|
||||||
setDraftPost(draftField, username, draftId);
|
dispatch(updateDraftCache(draftId, draftField))
|
||||||
}
|
}
|
||||||
|
|
||||||
//update editor data locally
|
//update editor data locally
|
||||||
else if (!isReply) {
|
else if (!isReply) {
|
||||||
setDraftPost(draftField, username);
|
dispatch(updateDraftCache(DEFAULT_USER_DRAFT_ID + username, draftField));
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -767,15 +754,7 @@ class EditorContainer extends Component<any, any> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
//post publish updates
|
//post publish updates
|
||||||
setDraftPost(
|
dispatch(deleteDraftCacheEntry(DEFAULT_USER_DRAFT_ID + currentAccount.name))
|
||||||
{
|
|
||||||
title: '',
|
|
||||||
body: '',
|
|
||||||
tags: '',
|
|
||||||
timestamp: 0,
|
|
||||||
},
|
|
||||||
currentAccount.name,
|
|
||||||
);
|
|
||||||
|
|
||||||
dispatch(removeBeneficiaries(TEMP_BENEFICIARIES_ID))
|
dispatch(removeBeneficiaries(TEMP_BENEFICIARIES_ID))
|
||||||
if (draftId) {
|
if (draftId) {
|
||||||
@ -1162,15 +1141,8 @@ class EditorContainer extends Component<any, any> {
|
|||||||
}),
|
}),
|
||||||
),
|
),
|
||||||
);
|
);
|
||||||
setDraftPost(
|
|
||||||
{
|
dispatch(deleteDraftCacheEntry(DEFAULT_USER_DRAFT_ID + currentAccount.name))
|
||||||
title: '',
|
|
||||||
body: '',
|
|
||||||
tags: '',
|
|
||||||
timestamp: 0,
|
|
||||||
},
|
|
||||||
currentAccount.name,
|
|
||||||
);
|
|
||||||
|
|
||||||
setTimeout(() => {
|
setTimeout(() => {
|
||||||
navigation.replace(ROUTES.SCREENS.DRAFTS,
|
navigation.replace(ROUTES.SCREENS.DRAFTS,
|
||||||
@ -1190,17 +1162,10 @@ class EditorContainer extends Component<any, any> {
|
|||||||
_initialEditor = () => {
|
_initialEditor = () => {
|
||||||
const {
|
const {
|
||||||
currentAccount: { name },
|
currentAccount: { name },
|
||||||
|
dispatch
|
||||||
} = this.props;
|
} = this.props;
|
||||||
|
|
||||||
setDraftPost(
|
dispatch(deleteDraftCacheEntry(DEFAULT_USER_DRAFT_ID + name))
|
||||||
{
|
|
||||||
title: '',
|
|
||||||
body: '',
|
|
||||||
tags: '',
|
|
||||||
timestamp: 0,
|
|
||||||
},
|
|
||||||
name,
|
|
||||||
);
|
|
||||||
|
|
||||||
this.setState({
|
this.setState({
|
||||||
uploadedImage: null,
|
uploadedImage: null,
|
||||||
|
Loading…
Reference in New Issue
Block a user