import { IOfflineDataState , IOfflineData } from '@client/offline/reducer' ; import { IStoreState } from '@client/store' ; import { IUserDetails } from '@client/utils/userUtils' ; import { SYS_ADMIN_ROLES } from '@client/utils/constants' ; export const getOfflineState = ( store ) : IOfflineDataState => store . offline ; function getKey < K extends keyof IOfflineDataState > ( store , key ) { return getOfflineState ( store ) [ key ] ; } export function isOfflineDataLoaded ( state ) { const hasAllRequiredData = state . locations && state . facilities && state . forms && state . forms . registerForm && state . forms . certificateCollectorDefinition && state . forms . userForm && state . languages && state . assets && state . templates ; return Boolean ( hasAllRequiredData ) ; } export function isSystemAdmin ( userDetails ) { return ( userDetails && userDetails . role && SYS_ADMIN_ROLES . includes ( userDetails . role ) ) ; } export const getOfflineDataLoaded = ( store ) : IOfflineDataState [ 'offlineDataLoaded' ] => getKey ( store , 'offlineDataLoaded' ) ; export const getOfflineData = ( store ) : IOfflineData => { const data = getKey ( store , 'offlineData' ) ; if ( ! isOfflineDataLoaded ( data ) ) { throw new Error ( 'Offline data is not yet loaded. This should never happen' ) ; } return data ; } ; export const getOfflineLoadingError = ( store ) : IOfflineDataState [ 'loadingError' ] => getKey ( store , 'loadingError' ) ;