From 2ba17081ea4444f96fbdd0fa7d26cab7e62072c8 Mon Sep 17 00:00:00 2001 From: Guilherme Gazzo Date: Mon, 21 Aug 2017 00:00:41 -0300 Subject: [PATCH] app opens now at last selectioned server --- app/actions/actionsTypes.js | 1 + app/actions/index.js | 6 ++++++ app/sagas/index.js | 6 +++++- app/sagas/init.js | 15 +++++++++++++++ app/sagas/selectServer.js | 8 +++++--- 5 files changed, 32 insertions(+), 4 deletions(-) create mode 100644 app/sagas/init.js diff --git a/app/actions/actionsTypes.js b/app/actions/actionsTypes.js index efacd963b..a1b6ed78f 100644 --- a/app/actions/actionsTypes.js +++ b/app/actions/actionsTypes.js @@ -12,6 +12,7 @@ function createRequestTypes(base, types = defaultTypes) { // Login events export const LOGIN = createRequestTypes('LOGIN', [...defaultTypes, 'SET_TOKEN', 'SUBMIT']); export const ROOMS = createRequestTypes('ROOMS'); +export const APP = createRequestTypes('APP', ['READY']); export const MESSAGES = createRequestTypes('MESSAGES'); export const NAVIGATION = createRequestTypes('NAVIGATION', ['SET']); export const SERVER = createRequestTypes('SERVER', ['SELECT', 'CHANGED']); diff --git a/app/actions/index.js b/app/actions/index.js index c49b92bd0..75a05545f 100644 --- a/app/actions/index.js +++ b/app/actions/index.js @@ -1,5 +1,11 @@ import * as types from '../constants/types'; +import { APP } from './actionsTypes'; +export function appReady() { + return { + type: APP.READY + }; +} export function setCurrentServer(server) { return { type: types.SET_CURRENT_SERVER, diff --git a/app/sagas/index.js b/app/sagas/index.js index 8bc3d026a..fe68aad7f 100644 --- a/app/sagas/index.js +++ b/app/sagas/index.js @@ -1,12 +1,16 @@ -import { fork } from 'redux-saga/effects'; +import { fork, take } from 'redux-saga/effects'; +import * as types from '../actions/actionsTypes'; import hello from './hello'; import login from './login'; import connect from './connect'; import rooms from './rooms'; import messages from './messages'; import selectServer from './selectServer'; +import init from './init'; const root = function* root() { + yield fork(init); + yield take(types.APP.READY); yield fork(hello); yield fork(rooms); yield fork(login); diff --git a/app/sagas/init.js b/app/sagas/init.js new file mode 100644 index 000000000..b06120211 --- /dev/null +++ b/app/sagas/init.js @@ -0,0 +1,15 @@ +import { AsyncStorage } from 'react-native'; +import { call, put } from 'redux-saga/effects'; +import * as actions from '../actions'; +import { setServer } from '../actions/server'; + +const restore = function* restore() { + try { + const currentServer = yield call([AsyncStorage, 'getItem'], 'currentServer'); + yield put(actions.appReady({})); + if (currentServer) { yield put(setServer(currentServer)); } + } catch (e) { + console.log(e); + } +}; +export default restore; diff --git a/app/sagas/selectServer.js b/app/sagas/selectServer.js index f8df5c07f..de45cd34c 100644 --- a/app/sagas/selectServer.js +++ b/app/sagas/selectServer.js @@ -1,12 +1,14 @@ -import { put, takeEvery } from 'redux-saga/effects'; - +import { put, takeEvery, call } from 'redux-saga/effects'; +import { AsyncStorage } from 'react-native'; import { SERVER } from '../actions/actionsTypes'; import { connectRequest, disconnect } from '../actions/connect'; import { changedServer } from '../actions/server'; -const selectServer = function* selectServer(server) { +const selectServer = function* selectServer({ server }) { yield put(disconnect()); yield put(changedServer(server)); + yield console.log('SERVER->', server); + yield call([AsyncStorage, 'setItem'], 'currentServer', server); yield put(connectRequest(server)); }; const root = function* root() {