import { getNextVersions } from '@/api/versions' ; import { ActionContext , Module } from 'vuex' ; import { IRootState , IVersion , IVersionsState , } from '../Interface' ; const module = { namespaced : true , state : { versionNotificationSettings : { enabled : false , endpoint : '' , infoUrl : '' , } , nextVersions : [ ] , currentVersion : undefined , } , getters : { hasVersionUpdates ( state ) { return state . nextVersions . length > 0 ; } , nextVersions ( state ) { return state . nextVersions ; } , currentVersion ( state ) { return state . currentVersion ; } , areNotificationsEnabled ( state ) { return state . versionNotificationSettings . enabled ; } , infoUrl ( state ) { return state . versionNotificationSettings . infoUrl ; } , } , mutations : { setVersions ( state , { versions , currentVersion } ) { state . nextVersions = versions . filter ( ( version ) => version . name !== currentVersion ) ; state . currentVersion = versions . find ( ( version ) => version . name === currentVersion ) ; } , setVersionNotificationSettings ( state , settings ) { state . versionNotificationSettings = settings ; } , } , actions : { async fetchVersions ( context ) { try { const { enabled , endpoint } = context . state . versionNotificationSettings ; if ( enabled && endpoint ) { const currentVersion = context . rootState . versionCli ; const instanceId = context . rootState . instanceId ; const versions = await getNextVersions ( endpoint , currentVersion , instanceId ) ; context . commit ( 'setVersions' , { versions , currentVersion } ) ; } } catch ( e ) { } } , } , } ; export default module ;