Reset store after disconnect

This commit is contained in:
Thomas Nordquist
2019-04-15 14:14:43 +02:00
parent 6d6b35d5f8
commit 47d1e74852
9 changed files with 63 additions and 39 deletions

View File

@@ -1,22 +1,26 @@
import * as q from '../../../backend/src/Model'
import { Action } from 'redux'
import { createReducer } from './lib'
import { Record } from 'immutable'
import { TopicViewModel } from '../model/TopicViewModel'
export interface TreeState {
interface TreeStateModel {
tree?: q.Tree<TopicViewModel>
selectedTopic?: q.TreeNode<TopicViewModel>
filter?: string
paused: boolean
}
export type Action = ShowTree | SelectTopic
export type TreeState = Record<TreeStateModel>
export type Action = ShowTree | SelectTopic | ResetStore
export enum ActionTypes {
TREE_SHOW_TREE = 'TREE_SHOW_TREE',
TREE_SELECT_TOPIC = 'TREE_SELECT_TOPIC',
TREE_RESUME_UPDATES = 'TREE_RESUME_UPDATES',
TREE_PAUSE_UPDATES = 'TREE_PAUSE_UPDATES',
TREE_RESET_STORE = 'TREE_RESET_STORE',
}
export interface ShowTree {
@@ -34,35 +38,42 @@ export interface SetPause {
type: ActionTypes.TREE_PAUSE_UPDATES | ActionTypes.TREE_RESUME_UPDATES
}
const initialState: TreeState = {
const initialStateFactory = Record<TreeStateModel>({
paused: false,
tree: undefined,
selectedTopic: undefined,
filter: undefined,
})
const setPaused = (pause: boolean) => (state: TreeState, action: ShowTree): TreeState => {
return state.set('paused', pause)
}
const setPaused = (pause: boolean) => (state: TreeState, action: ShowTree) => {
return {
...state,
paused: pause,
}
}
export const treeReducer = createReducer(initialState, {
const actions: {[s: string]: (state: TreeState, action: Action) => TreeState} = {
TREE_SHOW_TREE: showTree,
TREE_SELECT_TOPIC: selectTopic,
TREE_PAUSE_UPDATES: setPaused(true),
TREE_RESUME_UPDATES: setPaused(false),
})
TREE_RESET_STORE: resetStore,
}
function showTree(state: TreeState, action: ShowTree) {
return {
...state,
export const treeReducer = createReducer(initialStateFactory(), actions)
function showTree(state: TreeState, action: ShowTree): TreeState {
return state.merge({
tree: action.tree,
filter: action.filter,
}
})
}
function selectTopic(state: TreeState, action: SelectTopic) {
return {
...state,
selectedTopic: action.selectedTopic,
}
function selectTopic(state: TreeState, action: SelectTopic): TreeState {
return state.set('selectedTopic', action.selectedTopic)
}
export interface ResetStore {
type: ActionTypes.TREE_RESET_STORE
}
function resetStore(state: TreeState, action: ResetStore): TreeState {
return initialStateFactory()
}