import { inject , injectable } from 'inversify' ; import * as apid from '../../../../../api' ; import GenreUtil from '../../../util/GenreUtil' ; import IRuleApiModel from '../../api/rule/IRuleApiModel' ; import IChannelModel from '../../channels/IChannelModel' ; import IRuleState , { RuleFetchOption , RuleStateData } from './IRuleState' ; interface SelectedIndex { [ ruleId ] : boolean ; } @ injectable ( ) export default class RuleState implements IRuleState { private channelModel : IChannelModel ; private ruleApiModel : IRuleApiModel ; private rules : RuleStateData [ ] | null = null ; private total : number = 0 ; constructor ( @ inject ( 'IChannelModel' ) channelModel , @ inject ( 'IRuleApiModel' ) ruleApiModel ) { this . channelModel = channelModel ; this . ruleApiModel = ruleApiModel ; } /**
     * 取得したルール情報をクリア
     */ public clearData ( ) { this . rules = null ; } param option return public async fetchData ( option ) { const result = await this . ruleApiModel . gets ( option ) ; const isSelectedIndex = { } ; if ( this . rules !== null ) { for ( const r of this . rules ) { isSelectedIndex [ r . item . id ] = r . isSelected ; } } this . total = result . total ; this . rules = result . rules . map ( r => { return this . convertRuleItemToStateData ( r , option . isHalfWidth , isSelectedIndex ) ; } ) ; } param rule param isHalfWidth param isSelectedIndex return private convertRuleItemToStateData ( r , isHalfWidth , isSelectedIndex ) { let channelStr = '' ; if ( typeof r . searchOption . channelIds === 'undefined' || r . searchOption . channelIds . length === 0 ) { channelStr = '-' ; } else { const channel = this . channelModel . findChannel ( r . searchOption . channelIds [ 0 ] , isHalfWidth ) ; channelStr = channel === null ? r . searchOption . channelIds [ 0 ] . toString ( 10 ) : channel . name ; if ( r . searchOption . channelIds . length > 1 ) { channelStr += ` 他${ r . searchOption . channelIds . length - 1 }` ; } } let genreStr = '' ; if ( typeof r . searchOption . genres === 'undefined' || r . searchOption . genres . length === 0 ) { genreStr = '-' ; } else { const genre = typeof r . searchOption . genres [ 0 ] . subGenre === 'undefined' ? GenreUtil . getGenre ( r . searchOption . genres [ 0 ] . genre ) : GenreUtil . getSubGenre ( r . searchOption . genres [ 0 ] . genre , r . searchOption . genres [ 0 ] . subGenre ) ; if ( genre === null ) { genreStr = '-' ; } else { genreStr = genre ; if ( r . searchOption . genres . length > 1 ) { genreStr += ` 他${ r . searchOption . genres . length - 1 }` ; } } } return { display : { id : r . id , isEnable : r . reserveOption . enable , keyword : typeof r . searchOption . keyword === 'undefined' ? '-' : r . searchOption . keyword , ignoreKeyword : typeof r . searchOption . ignoreKeyword === 'undefined' ? '-' : r . searchOption . ignoreKeyword , channels : channelStr , genres : genreStr , reservationsCnt : typeof r . reservesCnt === 'undefined' ? 0 : r . reservesCnt , } , item : r , isSelected : typeof isSelectedIndex [ r . id ] === 'undefined' ? false : isSelectedIndex [ r . id ] , } ; } return public getRules ( ) { return this . rules === null ? [ ] : this . rules ; } return public getTotal ( ) { return this . total ; } return public getSelectedCnt ( ) { if ( this . rules === null ) { return 0 ; } let selectedCnt = 0 ; for ( const r of this . rules ) { if ( r . isSelected === true ) { selectedCnt ++ ; } } return selectedCnt ; } param reserveId public select ( reserveId ) { if ( this . rules === null ) { return ; } for ( const r of this . rules ) { if ( r . item . id === reserveId ) { r . isSelected = ! r . isSelected ; return ; } } } /**
     * 全て選択 (削除時の複数選択)
     */ public selectAll ( ) { if ( this . rules === null ) { return ; } let isUnselectAll = true ; for ( const r of this . rules ) { if ( r . isSelected === false ) { isUnselectAll = false ; } r . isSelected = true ; } if ( isUnselectAll === true ) { for ( const r of this . rules ) { r . isSelected = false ; } } } /**
     * 全ての選択解除 (削除時の複数選択)
     */ public clearSelect ( ) { if ( this . rules === null ) { return ; } for ( const r of this . rules ) { r . isSelected = false ; } } return public async multiplueDeletion ( ) { if ( this . rules === null ) { return ; } const reserveIds = [ ] ; for ( const r of this . rules ) { if ( r . isSelected === true ) { reserveIds . push ( r . item . id ) ; } } this . clearSelect ( ) ; let hasError = false ; for ( const id of reserveIds ) { try { await this . ruleApiModel . delete ( id ) ; } catch ( err ) { console . error ( err ) ; hasError = true ; } } if ( hasError === true ) { throw new Error ( ) ; } } }