first bits of redux

This commit is contained in:
41666 2019-03-20 09:25:28 -05:00
parent 9c7f7fda73
commit def2b0d2a3
13 changed files with 170 additions and 16 deletions

View 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')
}
}

10
ui/stores/roles.js Normal file
View file

@ -0,0 +1,10 @@
import { namespaceConfig } from 'fast-redux'
import { getCurrentServerState } from './currentServer'
export const { action, getState: getCurrentRoles } = namespaceConfig('roles', {})
export const updateCurrentRoles = action('updateCurrentRoles', (state, data) => data)
export const renderRoles = (dispatch, getState) => {
const server = getCurrentServerState(getState())
}

16
ui/stores/servers.js Normal file
View file

@ -0,0 +1,16 @@
// @flow
import { namespaceConfig } from 'fast-redux'
// import { Map } from 'immutable'
const DEFAULT_STATE = {}
export type ServersState = typeof DEFAULT_STATE
export const { action, getState: getServerState } = namespaceConfig('servers', DEFAULT_STATE)
export const updateServers = action('updateServers', (state: ServersState, serverData) => ({
...state,
servers: serverData
}))
export const updateSingleServer = action('updateSingleServer', (state, data, server) => ({ ...state, [server]: data }))

32
ui/stores/user.js Normal file
View file

@ -0,0 +1,32 @@
// @flow
import { namespaceConfig } from 'fast-redux'
export type User = {
id: string,
username: string,
discriminator: string,
avatar: string,
nicknameCache: {
[server: string]: string
}
}
export type UserState = {
currentUser: User | null,
userCache: {
[id: string]: User
}
}
const DEFAULT_STATE: UserState = {
currentUser: null,
userCache: {}
}
export const {
action, getState: getUserStore
} = namespaceConfig('userStore', DEFAULT_STATE)
export const getCurrentUser = () => async (dispatch: Function) => {
}