mirror of
https://github.com/roleypoly/roleypoly-v1.git
synced 2025-06-17 02:29:10 +00:00
first bits of redux
This commit is contained in:
parent
9c7f7fda73
commit
def2b0d2a3
13 changed files with 170 additions and 16 deletions
53
ui/stores/currentServer.js
Normal file
53
ui/stores/currentServer.js
Normal file
|
@ -0,0 +1,53 @@
|
|||
// @flow
|
||||
import { dynamicPropertyConfig } from 'fast-redux'
|
||||
// import { Map } from 'immutable'
|
||||
import type { PresentableServer } from '../../services/presentation'
|
||||
import RPC from '../config/rpc'
|
||||
import { action } from './servers'
|
||||
|
||||
const DEFAULT_STATE: $Shape<PresentableServer> | { id: ?string } = {
|
||||
id: null,
|
||||
server: {
|
||||
name: 'PLACEHOLDER',
|
||||
id: '386659935687147521',
|
||||
icon: '4fa0c1063649a739f3fe1a0589aa2c03',
|
||||
ownerID: '62601275618889728'
|
||||
},
|
||||
gm: {
|
||||
nickname: 'person',
|
||||
color: '#ff00ff'
|
||||
},
|
||||
categories: {},
|
||||
perms: {
|
||||
isAdmin: false,
|
||||
canManageRoles: false
|
||||
},
|
||||
roles: []
|
||||
}
|
||||
|
||||
export type ServerState = PresentableServer
|
||||
|
||||
// export const { action, getState: getCurrentServerState } = namespaceConfig('currentServer', DEFAULT_STATE)
|
||||
|
||||
export const { propertyAction: currentServerAction, getPropertyState: getCurrentServerState } = dynamicPropertyConfig(action, DEFAULT_STATE)
|
||||
|
||||
export const updateCurrentServer = currentServerAction('updateCurrentServer', (state, newState) => ({ ...state, ...newState }))
|
||||
|
||||
export const fetchServerIfNeed = (id: string, rpc?: typeof RPC) => async (dispatch: *, getState: *) => {
|
||||
if (rpc == null) {
|
||||
rpc = RPC
|
||||
}
|
||||
|
||||
if (id == null) {
|
||||
console.warn({ id })
|
||||
}
|
||||
|
||||
const state: ServerState = getCurrentServerState(getState(), id)
|
||||
if (state.id == null || state.id !== id) {
|
||||
const server = await rpc.getServer(id)
|
||||
dispatch(updateCurrentServer(id, server))
|
||||
console.log({ state, server, fullStore: getState() })
|
||||
} else {
|
||||
console.log('did not update')
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue