Skip to content

Commit

Permalink
feat(systemSettings): load systemsettings on appload
Browse files Browse the repository at this point in the history
  • Loading branch information
Birkbjo committed Aug 2, 2023
1 parent 83c6a47 commit cda58ae
Show file tree
Hide file tree
Showing 6 changed files with 154 additions and 1 deletion.
1 change: 1 addition & 0 deletions src/lib/systemSettings/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './systemSettingsStore'
37 changes: 37 additions & 0 deletions src/lib/systemSettings/systemSettingsStore.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import { create } from 'zustand'
import type { SystemSettings, SystemSettingsKey } from '../../types'

export interface SchemasStore {
systemSettings: SystemSettings | undefined
getSystemSettings: () => SystemSettings
setSystemSettings: (systemSettings: SystemSettings) => void
getSystemSetting: (
settingsKey: SystemSettingsKey
) => SystemSettings[typeof settingsKey]
}

export const useSchemaStore = create<SchemasStore>()((set, get) => ({
systemSettings: undefined,
getSystemSettings: () => {
const systemSettings = get().systemSettings

if (systemSettings === undefined) {
throw new Error('SystemSettings not loaded')
}

return systemSettings
},
setSystemSettings: (systemSettings: SystemSettings) =>
set({ systemSettings }),
getSystemSetting: (settingsKey: SystemSettingsKey) =>
get().getSystemSettings()[settingsKey],
}))

export const useSetSystemSettings = () =>
useSchemaStore((state) => state.setSystemSettings)

export const useSystemSettings = () =>
useSchemaStore((state) => state.getSystemSettings())

export const useSystemSetting = (settingsKey: SystemSettingsKey) =>
useSchemaStore((state) => state.getSystemSetting(settingsKey))
12 changes: 11 additions & 1 deletion src/lib/useLoadApp.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
import { useDataQuery } from '@dhis2/app-runtime'
import type { ModelSchemasBase, PickSchemaProperties } from '../types'
import type { CurrentUser as CurrentUserBase } from '../types/models'
import type {
CurrentUser as CurrentUserBase,
SystemSettings,
} from '../types/models'
import { useSetSchemas } from './schemas'
import { useSetSystemSettings } from './systemSettings'
import { useSetCurrentUser } from './user'

export const schemaFields = [
Expand Down Expand Up @@ -54,6 +58,9 @@ const query = {
fields: userFieldsFilter,
},
},
systemSettings: {
resource: 'systemSettings',
},
}

// properties are returned as an array, but we map them by fieldName
Expand All @@ -66,6 +73,7 @@ interface QueryResponse {
schemas: SchemaResponse[]
}
currentUser: CurrentUserResponse
systemSettings: SystemSettings
}

const formatSchema = (schema: SchemaResponse): Schema => {
Expand All @@ -80,6 +88,7 @@ const formatSchema = (schema: SchemaResponse): Schema => {
export const useLoadApp = () => {
const setSchemas = useSetSchemas()
const setCurrentUser = useSetCurrentUser()
const setSystemSettings = useSetSystemSettings()

const queryResponse = useDataQuery<QueryResponse>(query, {
onComplete: (queryData) => {
Expand All @@ -100,6 +109,7 @@ export const useLoadApp = () => {
}
setSchemas(modelSchemas)
setCurrentUser(currentUser)
setSystemSettings(queryData.systemSettings)
} catch (e) {
console.log('Failed to load app', e)
}
Expand Down
1 change: 1 addition & 0 deletions src/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ export * from './schemaBase'
export * from './section'
export type * from './query'
export * from './ui'
export * from './systemSettings'
1 change: 1 addition & 0 deletions src/types/models.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
// generated by https://github.com/Birkbjo/dhis2-open-api-ts
export type * from './generated'
export * from './systemSettings'
103 changes: 103 additions & 0 deletions src/types/systemSettings.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
import { SmsConfiguration } from './models'

export type SystemSettings = {
keyDateFormat: string
keyHideBiWeeklyPeriods: boolean
keyGoogleMapsApiKey: string
keyRemoteInstanceUsername: string
keyFileResourceRetentionStrategy: string
keyEmailSender: string
keyAnalysisDigitGroupSeparator: string
keyCustomLoginPageLogo: boolean
KeyTrackedEntityInstanceMaxLimit: number
keyUseCustomLogoBanner: boolean
keyDashboardContextMenuItemOpenInRelevantApp: boolean
keyEmailTls: boolean
keyRemoteInstanceUrl: string
keyLastSuccessfulSystemMonitoringPush: string
keyHideDailyPeriods: boolean
keyDataImportStrictDataSetLocking: boolean
keyFlag: string
minPasswordLength: number
analyticsFinancialYearStart: string
keyBingMapsApiKey: string
keyUseCustomLogoFront: boolean
credentialsExpiresReminderInDays: number
keySkipDataTypeValidationInAnalyticsTableExport: boolean
keyHideMonthlyPeriods: boolean
startModule: string
ruleEngineAssignOverwrite: boolean
keyCacheStrategy: string
keyDataImportRequireCategoryOptionCombo: boolean
multiOrganisationUnitForms: boolean
syncMaxAttempts: number
keyDataImportStrictAttributeOptionCombos: boolean
keyHideWeeklyPeriods: boolean
keyGatherAnalyticalObjectStatisticsInDashboardViews: boolean
credentialsExpiryAlert: boolean
keyCustomTopMenuLogo: boolean
keySmsMaxLength: number
keyLastMonitoringRun: string
keyAnalyticsMaxLimit: number
keyLockMultipleFailedLogins: boolean
lastSuccessfulDataStatistics: string
keyCountPassiveDashboardViewsInUsageAnalytics: boolean
keyDashboardContextMenuItemViewFullscreen: boolean
keyDatabaseServerCpus: number
keyIncludeZeroValuesInAnalytics: boolean
keyApplicationFooter: string
keyDataImportStrictPeriods: boolean
syncDelayBetweenRemoteServerAvailabilityCheckAttempts: number
keyIgnoreAnalyticsApprovalYearThreshold: number
keyDataImportStrictDataSetInputPeriods: boolean
keySelfRegistrationNoRecaptcha: boolean
keyCustomCss: string
applicationTitle: string
syncSkipSyncForDataChangedBefore: string
keyAnalysisDisplayProperty: string
maxPasswordLength: number
keyDashboardContextMenuItemSwitchViewType: boolean
keyLastCompleteDataSetRegistrationSyncSuccess: string
keyMetaDataRepoUrl: string
keyApplicationNotification: string
keyLastSuccessfulAnalyticsTablesUpdate: string
keyUiLocale: string
keyEmailPort: number
keyRespectMetaDataStartEndDatesInAnalyticsTableExport: boolean
keyCalendar: string
keyHideBiMonthlyPeriods: boolean
keyCacheability: string
keyDataImportRequireAttributeOptionCombo: boolean
keyLastSuccessfulAnalyticsTablesRuntime: string
keyStyle: string
keyLastSuccessfulResourceTablesUpdate: string
keyDataImportStrictDataSetApproval: boolean
keyVersionEnabled: boolean
factorDeviation: number
keySqlViewMaxLimit: number
accountExpiryAlert: boolean
keyAnalysisRelativePeriod: string
keyDashboardContextMenuItemShowInterpretationsAndDetails: boolean
accountExpiresInDays: number
keyAnalyticsCacheProgressiveTtlFactor: number
keyLastSuccessfulEventsDataSynch: string
syncMaxRemoteServerAvailabilityCheckAttempts: number
keyAllowObjectAssignment: boolean
keyStopMetadataSync: boolean
keySmsSetting: SmsConfiguration
keyDataImportStrictDataElements: boolean
keyLastSuccessfulDataSynch: string
keyApplicationIntro: string
keyDataImportStrictOrganisationUnits: boolean
helpPageLink: string
keyDataImportStrictCategoryOptionCombos: boolean
credentialsExpires: number
keyAccountRecovery: boolean
keyCanGrantOwnUserAuthorityGroups: boolean
keyAcceptanceRequiredForApproval: boolean
keyRequireAddToView: boolean
keyAnalyticsCacheTtlMode: string
startModuleEnableLightweight: boolean
}

export type SystemSettingsKey = keyof SystemSettings

0 comments on commit cda58ae

Please sign in to comment.