import { Toast } from "antd-mobile" ; import { ItemCreateData , ItemDetail , ListItem , ListSearch , ListSummary } from "entity/comment" ; import { RootState } from "modules" ; import { ModuleNames } from "modules/names" ; import { Actions , BaseModuleHandlers , BaseModuleState , effect , exportModel , reducer } from "react-coat" ; import api from "./api" ; import { defaultListSearch } from "./facade" ; export interface State extends BaseModuleState { listSearch ? : ListSearch ; listItems ? : ListItem [ ] ; listSummary ? : ListSummary ; itemDetail ? : ItemDetail ; } const initState = { } ; class ModuleHandlers extends BaseModuleHandlers < State , RootState , ModuleNames > { @ reducer public putItemDetail ( itemDetail ? ) { return { ... this . state , itemDetail } ; } @ effect ( ) public async createItem ( data ) { const response = await api . createItem ( data ) ; if ( ! response . error ) { Toast . info ( "操作成功" ) ; if ( this . state . itemDetail ) { await this . getItemDetail ( this . state . itemDetail . id ) ; } else { await this . searchList ( { isNewest : true , page : 1 } ) ; } } else { Toast . info ( response . error . message ) ; } return response ; } @ effect ( ) public async searchList ( options = { } ) { const listSearch = { ... ( this . state . listSearch || defaultListSearch ) , ... options } ; const { listItems , listSummary } = await api . searchList ( listSearch ) ; this . updateState ( { listSearch , listItems , listSummary , itemDetail : undefined } ) ; } @ effect ( ) public async getItemDetail ( itemDetailId ) { const itemDetail = await api . getItemDetail ( itemDetailId ) ; this . updateState ( { itemDetail } ) ; } } export type ModuleActions = Actions < ModuleHandlers > ; export default exportModel ( ModuleNames . comments , ModuleHandlers , initState ) ;