import { AnyAction , combineReducers , Reducer } from 'redux' ; import { connectRoutes , Location } from 'redux-first-router' ; import { ThunkDispatch } from 'redux-thunk' ; import { LocationState , routesMap } from './locationReducer' ; import fileIndexReducer , { FileIndexReducerType } from './fileIndexReducer' ; import undoableReducers , { UndoableReducerType } from './undoableReducer' ; import loggedInUserReducer , { LoggedInUserReducerType } from './loggedInUserReducer' ; import connectedUserReducer , { ConnectedUserReducerType } from './connectedUserReducer' ; import { ScenarioType , TabletopType } from '../util/scenarioUtils' ; import { TabletopValidationType } from './tabletopValidationReducer' ; import myPeerIdReducer , { MyPeerIdReducerType } from './myPeerIdReducer' ; import tabletopReducer from './tabletopReducer' ; import bundleReducer , { BundleReducerType } from './bundleReducer' ; import createInitialStructureReducer , { CreateInitialStructureReducerType } from './createInitialStructureReducer' ; import deviceLayoutReducer , { DeviceLayoutReducerType } from './deviceLayoutReducer' ; import windowTitleReducer , { WindowTitleReducerType } from './windowTitleReducer' ; import diceReducer , { DiceReducerType } from './diceReducer' ; import pingReducer , { PingReducerType } from './pingReducer' ; import serviceWorkerReducer , { ServiceWorkerReducerType } from './serviceWorkerReducer' ; import { movableWindowReducer , MovableWindowReducerType } from './movableWindowReducer' ; import folderStacksReducer , { FolderStacksReducerType } from './folderStacksReducer' ; import { DiceBagReducerType } from './diceBagReducer' ; import uploadPlaceholderReducer , { UploadPlaceholderReducerType } from './uploadPlaceholderReducer' ; const DISCARD_STORE = 'discard_store' ; export function discardStoreAction ( ) { return { type : DISCARD_STORE } ; } export interface ReduxStoreType { location : Location ; windowTitle : WindowTitleReducerType ; fileIndex : FileIndexReducerType ; undoableState : UndoableReducerType ; tabletop : TabletopType ; loggedInUser : LoggedInUserReducerType ; connectedUsers : ConnectedUserReducerType ; myPeerId : MyPeerIdReducerType ; bundleId : BundleReducerType ; createInitialStructure : CreateInitialStructureReducerType ; deviceLayout : DeviceLayoutReducerType ; dice : DiceReducerType ; pings : PingReducerType ; serviceWorker : ServiceWorkerReducerType ; movableWindows : MovableWindowReducerType ; folderStacks : FolderStacksReducerType ; uploadPlaceholders : UploadPlaceholderReducerType ; } export interface GtoveDispatchProp { dispatch : ThunkDispatch < ReduxStoreType , { } , AnyAction > ; } const { reducer : locationReducer , middleware , enhancer } = connectRoutes < { } , LocationState > ( routesMap , { basename : '/gtove' , title : 'windowTitle' } ) ; export const reduxFirstMiddleware = middleware ; export const reduxFirstEnhancer = enhancer ; const topLevelReducers = combineReducers < ReduxStoreType > ( { location : locationReducer as any , windowTitle : windowTitleReducer , fileIndex : fileIndexReducer , undoableState : undoableReducers , tabletop : tabletopReducer , loggedInUser : loggedInUserReducer , connectedUsers : connectedUserReducer , myPeerId : myPeerIdReducer , bundleId : bundleReducer , createInitialStructure : createInitialStructureReducer , deviceLayout : deviceLayoutReducer , dice : diceReducer , pings : pingReducer , serviceWorker : serviceWorkerReducer , movableWindows : movableWindowReducer , folderStacks : folderStacksReducer , uploadPlaceholders : uploadPlaceholderReducer } ) ; const mainReducer = ( state , action ) => { switch ( action . type ) { case DISCARD_STORE : return topLevelReducers ( { location : state ? state . location : '' } as ReduxStoreType , action ) ; default : if ( state ) { const loggedInUser = getLoggedInUserFromStore ( state ) ; const tabletop = getTabletopFromStore ( state ) ; const isGMReduxStore = loggedInUser ? tabletop . gm === loggedInUser . emailAddress : false ; const connectedUsers = getConnectedUsersFromStore ( state ) ; const isGMAction = state && action . fromPeerId && connectedUsers . users [ action . fromPeerId ] ? tabletop . gm === connectedUsers . users [ action . fromPeerId ] . user . emailAddress : isGMReduxStore ; return topLevelReducers ( state , { ... action , isGMReduxStore , isGMAction } ) ; } else { return topLevelReducers ( state , { ... action , isGMReduxStore : false , isGMAction : false } ) ; } } } ; export default mainReducer ; export function getTabletopIdFromStore ( store ) { return store . location . payload [ 'tabletopId' ] ; } export function getTabletopResourceKeyFromStore ( store ) { return store . location . payload [ 'resourceKey' ] ; } export function getWindowTitleFromStore ( store ) { return store . windowTitle ; } export function getLoggedInUserFromStore ( store ) { return store . loggedInUser ; } export function getAllFilesFromStore ( store ) { return store . fileIndex ; } export function getConnectedUsersFromStore ( store ) { return store . connectedUsers ; } export function getMyPeerIdFromStore ( store ) { return store . myPeerId ; } export function getUndoableHistoryFromStore ( store ) { return store . undoableState ; } export function getScenarioFromStore ( store ) { return store . undoableState . present . scenario ; } export function getTabletopValidationFromStore ( store ) { return store . undoableState . present . tabletopValidation ; } export function getTabletopFromStore ( store ) { return store . tabletop ; } export function getBundleIdFromStore ( store ) { return store . bundleId ; } export function getCreateInitialStructureFromStore ( store ) { return store . createInitialStructure ; } export function getDeviceLayoutFromStore ( store ) { return store . deviceLayout ; } export function getDiceFromStore ( store ) { return store . dice ; } export function getPingsFromStore ( store ) { return store . pings ; } export function getServiceWorkerFromStore ( store ) { return store . serviceWorker ; } export function getMovableWindowsFromStore ( store ) { return store . movableWindows ; } export function getFolderStacksFromStore ( store ) { return store . folderStacks ; } export function getDiceBagFromStore ( store ) { return store . dice . diceBag ; } export function getUploadPlaceholdersFromStore ( store ) { return store . uploadPlaceholders ; }