import { isForceRefresh , parseQuery } from "common/routers" ; import { equal } from "common/utils" ; import { ListItem , ListSearch , ListSummary } from "entity/message" ; import { RootState } from "modules" ; import { ModuleNames } from "modules/names" ; import { Actions , BaseModuleHandlers , BaseModuleState , effect , exportModel , VIEW_INVALID } from "react-coat" ; import api from "./api" ; import { defaultListSearch } from "./facade" ; export interface State extends BaseModuleState { listSearch ? : ListSearch ; listItems ? : ListItem [ ] ; listSummary ? : ListSummary ; } const initState = { } ; class ModuleHandlers extends BaseModuleHandlers < State , RootState , ModuleNames > { @ effect ( ) public async searchList ( options = { } ) { const listSearch = { ... ( this . state . listSearch || defaultListSearch ) , ... options } ; const { listItems , listSummary } = await api . searchList ( listSearch ) ; this . updateState ( { listSearch , listItems , listSummary } ) ; } @ effect ( null ) protected async [ VIEW_INVALID ] ( ) { const views = this . rootState . views ; if ( views . messages && views . messages . List ) { const { search , hash } = this . rootState . router . location ; const forceRefresh = isForceRefresh ( hash ) ; const listSearch = parseQuery ( "search" , search , defaultListSearch ) ; if ( forceRefresh || ( forceRefresh === null && ! equal ( this . state . listSearch , listSearch ) ) ) { await this . dispatch ( this . actions . searchList ( listSearch ) ) ; } } } } export type ModuleActions = Actions < ModuleHandlers > ; export default exportModel ( ModuleNames . messages , ModuleHandlers , initState ) ;