@ -10,6 +10,7 @@ import {
import { getSubscribers } from 'src/api/subscriber'
import { getNumbers } from 'src/api/user'
import numberFilter from 'src/filters/number'
import { showGlobalError } from 'src/helpers/ui'
import { RequestState } from 'src/store/common'
export default {
@ -26,7 +27,8 @@ export default {
seatMapById : { } ,
soundSetList : [ ] ,
soundSetMapByName : { } ,
deviceProfilesListState : RequestState . initiated ,
deviceProfileListState : RequestState . initiated ,
deviceProfileListError : null ,
deviceProfileList : [ ] ,
deviceProfileMap : { } ,
deviceModelList : [ ] ,
@ -36,11 +38,9 @@ export default {
deviceModelImageSmallMap : { } ,
subscriberList : [ ] ,
subscriberListState : RequestState . initiated ,
sub criberListError: null ,
sub s criberListError: null ,
subscriberMap : { } ,
ncosMapByName : { } ,
deviceProfileListState : RequestState . initiated ,
deviceProfileListError : null
ncosMapByName : { }
} ,
getters : {
pilot ( state ) {
@ -160,6 +160,9 @@ export default {
return state . deviceModelMap [ deviceId ] !== undefined
}
} ,
isDeviceModelListStateRequesting ( state ) {
return state . deviceModelListState === RequestState . requesting
} ,
isSubscribersRequesting ( state ) {
return state . subscriberListState === RequestState . requesting
} ,
@ -234,30 +237,39 @@ export default {
state . soundSetMapByName [ soundSet . name ] = soundSet
} )
} ,
deviceProfile s ListStateRequesting ( state ) {
state . deviceProfile s ListState = RequestState . requesting
deviceProfile ListStateRequesting ( state ) {
state . deviceProfile ListState = RequestState . requesting
} ,
deviceProfilesListSucceeded ( state , deviceProfileList ) {
state . deviceProfilesListState = RequestState . succeeded
state . deviceProfileList = _ . get ( deviceProfileList , 'items' , [ ] )
deviceProfileListSucceeded ( state , deviceProfileList ) {
const newList = _ . get ( deviceProfileList , 'items' , [ ] )
// First remove existing items that we're about to replace with newer versions
// and keep only items whoseIds are not in the updated list
const existingIds = newList . map ( ( item ) => item . id )
const filteredExistingList = state . deviceProfileList . filter (
( existingItem ) => ! existingIds . includes ( existingItem . id )
)
state . deviceProfileList = [ ... filteredExistingList , ... newList ]
state . deviceProfileListState = RequestState . succeeded
state . deviceProfileMap = { }
state . deviceProfileList . forEach ( ( deviceProfile ) => {
state . deviceProfileMap [ deviceProfile . id ] = deviceProfile
} )
} ,
deviceProfilesListFailed ( state ) {
state . deviceProfilesListState = RequestState . failed
deviceProfile ListFailed ( state ) {
state . deviceProfile ListState = RequestState . failed
} ,
deviceProfileRequesting ( state ) {
state . deviceProfile s ListState = RequestState . requesting
state . deviceProfile ListState = RequestState . requesting
} ,
deviceProfileSucceeded ( state , deviceProfile ) {
state . deviceProfile s ListState = RequestState . succeeded
state . deviceProfile ListState = RequestState . succeeded
state . deviceProfileList = [ ... state . deviceProfileList , deviceProfile ]
state . deviceProfileMap [ deviceProfile . id ] = deviceProfile
} ,
deviceProfileFailed ( state ) {
state . deviceProfile s ListState = RequestState . failed
state . deviceProfile ListState = RequestState . failed
} ,
deviceModelSucceeded ( state , deviceModel ) {
state . deviceModelListState = RequestState . succeeded
@ -285,7 +297,7 @@ export default {
state . deviceModelError = options . error
} ,
subscribersRequesting ( state ) {
state . sub criberListState = RequestState . requesting
state . sub s criberListState = RequestState . requesting
state . subscriberList = [ ]
} ,
subscribersSucceeded ( state , subscribers ) {
@ -296,20 +308,18 @@ export default {
} )
} ,
subscribersFailed ( state , err ) {
state . sub criberListState = RequestState . failed
state . sub criberListError = err
state . sub s criberListState = RequestState . failed
state . sub s criberListError = err
}
} ,
actions : {
async loadProfiles ( context ) {
if ( context . state . deviceProfileList . length === 0 ) {
context . commit ( 'deviceProfilesListStateRequesting' )
try {
const profiles = await getAllProfiles ( )
context . commit ( 'deviceProfilesListSucceeded' , profiles )
} catch ( err ) {
context . commit ( 'deviceProfilesListFailed' , err . message )
}
context . commit ( 'deviceProfileListStateRequesting' )
try {
const profiles = await getAllProfiles ( )
context . commit ( 'deviceProfileListSucceeded' , profiles )
} catch ( err ) {
context . commit ( 'deviceProfileListFailed' , err . message )
}
} ,
async loadProfileById ( context , deviceId ) {
@ -321,6 +331,47 @@ export default {
context . commit ( 'deviceProfileFailed' )
}
} ,
async loadProfileThumbnails ( context ) {
const requests = [ ]
context . state . deviceProfileList . forEach ( ( deviceProfile ) => {
const isFrontThumbnailCached = context . state . deviceModelImageSmallMap [ deviceProfile . device _id ] !== undefined
if ( ! isFrontThumbnailCached ) {
requests . push (
getModelFrontThumbnailImage ( deviceProfile . device _id )
. then ( ( thumbnail ) => ( {
deviceId : deviceProfile . device _id ,
thumbnail
} ) )
. catch ( ( error ) => ( {
deviceId : deviceProfile . device _id ,
error
} ) )
)
}
} )
if ( requests . length > 0 ) {
try {
const results = await Promise . all ( requests )
results . forEach ( ( result ) => {
if ( result . thumbnail ) {
context . commit ( 'deviceModelSucceeded' , {
modelImageThumbnail : result . thumbnail
} )
} else if ( result . error ) {
// Silent warning as it's not a critical error
// and we can still use the device profile without the thumbnail
// eslint-disable-next-line no-console
console . warn ( ` Failed to load thumbnail for device ${ result . deviceId } : ` , result . error )
}
} )
} catch ( error ) {
showGlobalError ( 'Failed to load profile thumbnails:' , error )
}
}
} ,
async loadDeviceModel ( context , payload ) {
try {
const isFrontCached = context . state . deviceModelImageMap [ payload . deviceId ] !== undefined
@ -373,6 +424,10 @@ export default {
}
context . commit ( 'deviceModelSucceeded' , deviceModel )
} catch ( err ) {
// Note: the way it is implemented at the moment
// if any of the promises fails, the whole action fails
// and only the first error is reported. This needs refactoring
// to handle each request separately and report errors accordingly.
context . commit ( 'deviceModelFailed' , {
deviceModelId : payload . deviceId ,
error : err . message