import DataParser from '@/Parser/data' ; import AppModel from '@/Model/app' ; import KnockoutModel from '@/Model/knockout' ; import MatchModel from '@/Model/match' ; import GroupParser from '@/Parser/group' ; import GroupModel from '@/Model/group' ; import KnockoutParser from '@/Parser/knockout' ; const DATAURL = 'https://raw.githubusercontent.com/lsv/fifa-worldcup-2018/master/data.json' ; const state = { loading : true as boolean , data : null as AppModel , } ; const getters = { } ; const wait = ( ms ) => new Promise ( ( r ) => setTimeout ( r , ms ) ) ; const actions = { loadData ( { commit } ) { commit ( 'LOADING' , true ) ; fetch ( DATAURL ) . then ( ( response ) => { return response . json ( ) ; } ) . then ( ( json ) => { return DataParser . parse ( json ) ; } ) . then ( ( data ) => { wait ( 0 ) . then ( ( ) => { commit ( 'LOAD_DATA' , data ) ; commit ( 'LOADING' , false ) ; } ) ; } ) ; } , } ; const mutations = { [ 'LOADING' ] ( state , payload ) { state . loading = payload ; } , [ 'LOAD_DATA' ] ( state , payload ) { state . data = payload ; } , [ 'SET_KNOCKOUT_MATCH_RESULT' ] ( state , payload ) { const knockout = state . data . getKnockouts ( ) . find ( ( k ) => { return k . getName ( ) === payload . knockoutid ; } ) ; if ( knockout ) { const match = knockout . getMatches ( ) . find ( ( m ) => { return m . getId ( ) === payload . matchid ; } ) ; if ( match ) { match . setAwayResult ( payload . awayscore ) ; match . setHomeResult ( payload . homescore ) ; KnockoutParser . updateKnockouts ( state . data ) ; } } } , [ 'SET_GROUP_MATCH_RESULT' ] ( state , payload ) { const group = state . data . getGroups ( ) . find ( ( g ) => { return g . getName ( ) === payload . groupid ; } ) ; if ( group ) { const match = group . getMatches ( ) . find ( ( m ) => { return m . getId ( ) === payload . matchid ; } ) ; if ( match ) { match . setAwayResult ( payload . awayscore ) ; match . setHomeResult ( payload . homescore ) ; GroupParser . updateStandings ( group ) ; KnockoutParser . updateKnockouts ( state . data ) ; } } } , } ; export default { state , getters , actions , mutations , } ;