import { inject , injectable } from 'inversify' ; import * as apid from '../../../../../api' ; import DateUtil from '../../../util/DateUtil' ; import IReservesApiModel from '../../api/reserves/IReservesApiModel' ; import IReservesState from './IReservesState' ; import IReserveStateUtil , { ReserveStateData , SelectedIndex } from './IReserveStateUtil' ; @ injectable ( ) export default class ReservesState implements IReservesState { private reserveApiModel : IReservesApiModel ; private reserveStateUtil : IReserveStateUtil ; private reserves : ReserveStateData [ ] | null = null ; private total : number = 0 ; constructor ( @ inject ( 'IReservesApiModel' ) reserveApiModel , @ inject ( 'IReserveStateUtil' ) reserveStateUtil ) { this . reserveApiModel = reserveApiModel ; this . reserveStateUtil = reserveStateUtil ; } /**
     * 取得した予約情報をクリア
     */ public clearDate ( ) { this . reserves = null ; this . total = 0 ; } param option public async fetchData ( option ) { const reserves = await this . reserveApiModel . gets ( option ) ; this . total = reserves . total ; const oldSelectedIndex = { } ; if ( this . reserves !== null ) { for ( const r of this . reserves ) { oldSelectedIndex [ r . reserveItem . id ] = r . isSelected ; } } this . reserves = this . reserveStateUtil . convertReserveItemsToStateDatas ( reserves . reserves , option . isHalfWidth , oldSelectedIndex ) ; } return public getReserves ( ) { return this . reserves === null ? [ ] : this . reserves ; } return public getTotal ( ) { return this . total ; } return public getSelectedCnt ( ) { if ( this . reserves === null ) { return 0 ; } let selectedCnt = 0 ; for ( const r of this . reserves ) { if ( r . isSelected === true ) { selectedCnt ++ ; } } return selectedCnt ; } param reserveId public select ( reserveId ) { if ( this . reserves === null ) { return ; } for ( const r of this . reserves ) { if ( r . reserveItem . id === reserveId ) { r . isSelected = ! r . isSelected ; return ; } } } /**
     * 全て選択 (削除時の複数選択)
     */ public selectAll ( ) { if ( this . reserves === null ) { return ; } let isUnselectAll = true ; for ( const r of this . reserves ) { if ( r . isSelected === false ) { isUnselectAll = false ; } r . isSelected = true ; } if ( isUnselectAll === true ) { for ( const r of this . reserves ) { r . isSelected = false ; } } } /**
     * 全ての選択解除 (削除時の複数選択)
     */ public clearSelect ( ) { if ( this . reserves === null ) { return ; } for ( const r of this . reserves ) { r . isSelected = false ; } } return public async multiplueDeletion ( ) { if ( this . reserves === null ) { return ; } const reserveIds = [ ] ; for ( const r of this . reserves ) { if ( r . isSelected === true ) { reserveIds . push ( r . reserveItem . id ) ; } } this . clearSelect ( ) ; let hasError = false ; for ( const id of reserveIds ) { try { await this . reserveApiModel . cancel ( id ) ; } catch ( err ) { console . error ( err ) ; hasError = true ; } } if ( hasError === true ) { throw new Error ( ) ; } } }