Post draft persist (#271)
This commit is contained in:
parent
3b7a6df617
commit
230cbf7530
3 changed files with 52 additions and 3 deletions
|
|
@ -3,6 +3,8 @@
|
|||
|
||||
import {AsyncStorage} from 'react-native';
|
||||
import {batchActions} from 'redux-batched-actions';
|
||||
|
||||
import {ViewTypes} from 'app/constants';
|
||||
import {ChannelTypes, GeneralTypes, TeamsTypes, UsersTypes} from 'service/constants';
|
||||
|
||||
export function loadStorage() {
|
||||
|
|
@ -15,11 +17,35 @@ export function loadStorage() {
|
|||
|
||||
const currentChannelId = otherStorage[currentTeamId] ? otherStorage[currentTeamId].currentChannelId : '';
|
||||
|
||||
dispatch(batchActions([
|
||||
const actions = [
|
||||
{type: GeneralTypes.RECEIVED_APP_CREDENTIALS, data: credentials},
|
||||
{type: TeamsTypes.SELECT_TEAM, data: currentTeamId},
|
||||
{type: ChannelTypes.SELECT_CHANNEL, data: currentChannelId}
|
||||
]), getState);
|
||||
];
|
||||
|
||||
// Load post drafts if there are any
|
||||
if (otherStorage.postDrafts) {
|
||||
Object.keys(otherStorage.postDrafts).forEach((d) => {
|
||||
actions.push({
|
||||
type: ViewTypes.POST_DRAFT_CHANGED,
|
||||
channelId: d,
|
||||
postDraft: otherStorage.postDrafts[d]
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
// Load thread drafts if there are any
|
||||
if (otherStorage.threadDrafts) {
|
||||
Object.keys(otherStorage.threadDrafts).forEach((d) => {
|
||||
actions.push({
|
||||
type: ViewTypes.COMMENT_DRAFT_CHANGED,
|
||||
rootId: d,
|
||||
draft: otherStorage.threadDrafts[d]
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
dispatch(batchActions(actions), getState);
|
||||
} catch (error) {
|
||||
// Error loading data
|
||||
dispatch({type: GeneralTypes.REMOVED_APP_CREDENTIALS, error}, getState);
|
||||
|
|
@ -27,6 +53,18 @@ export function loadStorage() {
|
|||
};
|
||||
}
|
||||
|
||||
export function flushToStorage() {
|
||||
return async (dispatch, getState) => {
|
||||
const state = getState();
|
||||
|
||||
// Can add other important items here.
|
||||
const postDrafts = state.views.channel.drafts;
|
||||
const threadDrafts = state.views.thread.draft;
|
||||
|
||||
await updateStorage(null, {postDrafts, threadDrafts});
|
||||
};
|
||||
}
|
||||
|
||||
// Passing in a blank key of null or '' merges the data into the current storage.
|
||||
// Could maybe use some rework
|
||||
export async function updateStorage(key, data) {
|
||||
|
|
|
|||
|
|
@ -2,6 +2,7 @@
|
|||
// See License.txt for license information.
|
||||
|
||||
import React from 'react';
|
||||
import {AppState} from 'react-native';
|
||||
import Loading from 'app/components/loading';
|
||||
|
||||
import {RequestStatus} from 'service/constants';
|
||||
|
|
@ -14,6 +15,7 @@ export default class Root extends React.Component {
|
|||
actions: React.PropTypes.shape({
|
||||
goToLoadTeam: React.PropTypes.func,
|
||||
goToSelectServer: React.PropTypes.func,
|
||||
flushToStorage: React.PropTypes.func,
|
||||
loadStorage: React.PropTypes.func,
|
||||
removeStorage: React.PropTypes.func,
|
||||
setStoreFromLocalData: React.PropTypes.func,
|
||||
|
|
@ -28,6 +30,7 @@ export default class Root extends React.Component {
|
|||
componentDidMount() {
|
||||
// Any initialization logic for navigation, setting up the client, etc should go here
|
||||
this.init();
|
||||
AppState.addEventListener('change', this.handleAppStateChange);
|
||||
}
|
||||
|
||||
componentWillReceiveProps(nextProps) {
|
||||
|
|
@ -40,6 +43,13 @@ export default class Root extends React.Component {
|
|||
}
|
||||
}
|
||||
|
||||
handleAppStateChange = (event) => {
|
||||
// App gets killed, phone call comes in, app is pushed to the background, etc...
|
||||
if (event === 'inactive') {
|
||||
this.props.actions.flushToStorage();
|
||||
}
|
||||
}
|
||||
|
||||
init = () => {
|
||||
if (this.props.logoutRequest.status === RequestStatus.SUCCESS) {
|
||||
this.props.actions.removeStorage().then(() => {
|
||||
|
|
|
|||
|
|
@ -5,7 +5,7 @@ import {bindActionCreators} from 'redux';
|
|||
|
||||
import navigationSceneConnect from '../navigationSceneConnect';
|
||||
|
||||
import {loadStorage, removeStorage} from 'app/actions/storage';
|
||||
import {flushToStorage, loadStorage, removeStorage} from 'app/actions/storage';
|
||||
import {goToSelectServer, setStoreFromLocalData} from 'app/actions/views/root';
|
||||
import {goToLoadTeam} from 'app/actions/navigation';
|
||||
import {resetLogout} from 'service/actions/users';
|
||||
|
|
@ -26,6 +26,7 @@ function mapDispatchToProps(dispatch) {
|
|||
actions: bindActionCreators({
|
||||
goToLoadTeam,
|
||||
goToSelectServer,
|
||||
flushToStorage,
|
||||
loadStorage,
|
||||
removeStorage,
|
||||
setStoreFromLocalData,
|
||||
|
|
|
|||
Loading…
Reference in a new issue