Created
November 2, 2016 16:48
-
-
Save binzailani3136/ba58fa4839ae495a7a791f17f2a96798 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'use strict'; | |
import type {Action} from './types' | |
export const PUSH_NEW_ROUTE = "PUSH_NEW_ROUTE"; | |
export const REPLACE_ROUTE = "REPLACE_ROUTE"; | |
export const REPLACE_OR_PUSH_ROUTE = "REPLACE_OR_PUSH_ROUTE"; | |
export const POP_ROUTE = "POP_ROUTE"; | |
export const POP_TO_ROUTE = "POP_TO_ROUTE"; | |
export function replaceRoute(route:string):Action { | |
return { | |
type: REPLACE_ROUTE, | |
route: route | |
} | |
} | |
export function pushNewRoute(route:string):Action { | |
return { | |
type: PUSH_NEW_ROUTE, | |
route: route | |
} | |
} | |
export function replaceOrPushRoute(route:string):Action { | |
return { | |
type: REPLACE_OR_PUSH_ROUTE, | |
route: route | |
} | |
} | |
export function popRoute():Action { | |
return { | |
type: POP_ROUTE | |
} | |
} | |
export function popToRoute(route:string):Action { | |
return { | |
type: POP_TO_ROUTE, | |
route: route | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'use strict'; | |
import type from '../AppNavigator'; | |
export type Action = | |
{ type: 'PUSH_NEW_ROUTE', route: string } | |
| { type: 'POP_ROUTE' } | |
| { type: 'POP_TO_ROUTE', route: string } | |
| { type: 'REPLACE_ROUTE', route: string } | |
| { type: 'REPLACE_OR_PUSH_ROUTE', route: string } | |
| { type: 'OPEN_DRAWER'} | |
| { type: 'CLOSE_DRAWER'} | |
| { type: 'SET_CHECK_STATE', checkState: boolean } | |
| { type: 'STORE_TOPIC', topic: object } | |
export type Dispatch = (action:Action | ThunkAction | PromiseAction | Array<Action>) => any; | |
export type GetState = () => Object; | |
export type ThunkAction = (dispatch:Dispatch, getState:GetState) => any; | |
export type PromiseAction = Promise<Action>; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'use strict'; | |
import type {Action} from '../actions/types'; | |
import {globalNav} from '../AppNavigator'; | |
import { PUSH_NEW_ROUTE, POP_ROUTE, POP_TO_ROUTE, REPLACE_ROUTE, REPLACE_OR_PUSH_ROUTE } from '../actions/route'; | |
import { REHYDRATE } from 'redux-persist/constants' | |
export type State = { | |
routes: Array<string> | |
} | |
const initialState = { | |
routes: ['login'] | |
}; | |
export default function (state:State = initialState, action:Action): State { | |
if (action.type === PUSH_NEW_ROUTE) { | |
globalNav.navigator.push({id: action.route}); | |
return { | |
routes: [...state.routes, action.route] | |
}; | |
} | |
if (action.type === REPLACE_ROUTE) { | |
globalNav.navigator.replaceWithAnimation({id: action.route}); | |
let routes = state.routes; | |
routes.pop(); | |
return { | |
routes: [...routes, action.route] | |
}; | |
} | |
// For sidebar navigation | |
if (action.type === REPLACE_OR_PUSH_ROUTE) { | |
let routes = state.routes; | |
if(routes[routes.length - 1] == 'home') { | |
// If top route is home and user navigates to a route other than home, then push | |
if(action.route != 'home') | |
globalNav.navigator.push({id: action.route}); | |
// If top route is home and user navigates to home, do nothing | |
else | |
routes = []; | |
} | |
else { | |
if(action.route == 'home') { | |
globalNav.navigator.resetTo({id: 'home'}); | |
routes = []; | |
} | |
else { | |
globalNav.navigator.replaceWithAnimation({id: action.route}); | |
routes.pop(); | |
} | |
} | |
return { | |
routes: [...routes, action.route] | |
}; | |
} | |
if (action.type === POP_ROUTE) { | |
globalNav.navigator.pop(); | |
let routes = state.routes; | |
routes.pop(); | |
return { | |
routes: routes | |
} | |
} | |
if (action.type === POP_TO_ROUTE) { | |
globalNav.navigator.popToRoute({id: action.route}); | |
let routes = state.routes; | |
while (routes.pop() !== action.route) {} | |
return { | |
routes: [...routes, action.route] | |
} | |
} | |
if (action.type === REHYDRATE) { | |
const savedData = action['payload']['route'] || state; | |
return { | |
...savedData | |
} | |
} | |
return state; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment