1- // @flow
2- /* eslint-disable import/prefer-default-export */
31import log from 'loglevel' ;
4- import type { ServerVersion } from '@dhis2/app-runtime' ;
52import { environments } from 'capture-core/constants/environments' ;
63import moment from 'moment' ;
74import { CurrentLocaleData } from 'capture-core/utils/localeData/CurrentLocaleData' ;
85import i18n from '@dhis2/d2-i18n' ;
9- import type { LocaleDataType } from 'capture-core/utils/localeData/CurrentLocaleData' ;
10-
116import { loadMetaData , cacheSystemSettings } from 'capture-core/metaDataStoreLoaders' ;
127import { buildMetaDataAsync , buildSystemSettingsAsync } from 'capture-core/metaDataMemoryStoreBuilders' ;
138import { initStorageControllers } from 'capture-core/storageControllers' ;
149import { DisplayException } from 'capture-core/utils/exceptions' ;
1510import { initRulesEngine } from '../../core_modules/capture-core/rules/rulesEngine' ;
1611
12+ type LocaleDataType = {
13+ dateFnsLocale : any ;
14+ weekDays : any ;
15+ weekDaysShort : any ;
16+ calendarFormatHeaderLong : string ;
17+ calendarFormatHeaderShort : string ;
18+ selectDatesText : string ;
19+ selectDateText : string ;
20+ todayLabelShort : string ;
21+ todayLabelLong : string ;
22+ weekStartsOn : number ;
23+ } ;
24+
1725function setLogLevel ( ) {
1826 const levels = {
1927 [ environments . dev ] : log . levels . DEBUG ,
@@ -22,7 +30,7 @@ function setLogLevel() {
2230 [ environments . prod ] : log . levels . ERROR ,
2331 } ;
2432
25- let level = levels [ process . env . NODE_ENV ] ;
33+ let level = levels [ process . env . NODE_ENV as keyof typeof levels ] ;
2634 if ( ! level && level !== 0 ) {
2735 level = log . levels . ERROR ;
2836 }
@@ -36,16 +44,14 @@ function setMomentLocaleAsync(locale: string) {
3644 return Promise . resolve ( ) ;
3745 }
3846
39- return new Promise ( ( resolve ) => {
40- // $FlowFixMe[unsupported-syntax] automated comment
47+ return new Promise < void > ( ( resolve ) => {
4148 import ( `moment/locale/${ locale } ` )
4249 . then ( ( ) => {
4350 moment . locale ( locale ) ;
4451 log . info ( `got moment locale config for ${ locale } ` ) ;
4552 resolve ( ) ;
4653 } )
4754 . catch ( ( ) => {
48- // fallback to english
4955 moment . locale ( 'en' ) ;
5056 log . error ( `could not get moment locale config for ${ locale } ` ) ;
5157 resolve ( ) ;
@@ -54,8 +60,7 @@ function setMomentLocaleAsync(locale: string) {
5460}
5561
5662function setDateFnLocaleAsync ( locale : string , weekdays : any , weekdaysShort : any , firstDayOfWeek : number ) {
57- return new Promise ( ( resolve , reject ) => {
58- // $FlowFixMe[unsupported-syntax] automated comment
63+ return new Promise < void > ( ( resolve , reject ) => {
5964 import ( `date-fns/locale/${ locale } /index.js` )
6065 . then ( ( dateFnLocale ) => {
6166 const localeData : LocaleDataType = {
@@ -77,7 +82,7 @@ function setDateFnLocaleAsync(locale: string, weekdays: any, weekdaysShort: any,
7782 } ) . catch ( ( ) => {
7883 log . error ( `could not get date-fns locale config for ${ locale } , fallback to en` ) ;
7984
80- import ( 'date-fns/locale/en/index.js' ) // eslint-disable-line
85+ import ( 'date-fns/locale/en' )
8186 . then ( ( dateFnLocale ) => {
8287 const localeData : LocaleDataType = {
8388 dateFnsLocale : dateFnLocale ,
@@ -103,29 +108,28 @@ function setDateFnLocaleAsync(locale: string, weekdays: any, weekdaysShort: any,
103108 } ) ;
104109}
105110
106- function changeI18nLocale ( locale ) {
107- i18n . changeLanguage ( locale ) ;
111+ function changeI18nLocale ( locale : string ) {
112+ ( i18n as any ) . changeLanguage ( locale ) ;
108113}
109114
110- function initI18n ( locale ) {
115+ function initI18n ( locale : string ) {
111116 changeI18nLocale ( locale ) ;
112- i18n . setDefaultNamespace ( 'default' ) ;
117+ ( i18n as any ) . setDefaultNamespace ( 'default' ) ;
113118}
114119
115- async function setLocaleDataAsync ( uiLocale : string ) { //eslint-disable-line
120+ async function setLocaleDataAsync ( uiLocale : string ) {
116121 const locale = uiLocale ;
117122 await setMomentLocaleAsync ( locale ) ;
118123 const weekdays = moment . weekdays ( ) ;
119124 const weekdaysShort = moment . weekdaysShort ( ) ;
120125
121- // $FlowFixMe[prop-missing] automated comment
122- const firstDayOfWeek = moment . localeData ( ) . _week . dow ; //eslint-disable-line
126+ const firstDayOfWeek = ( moment . localeData ( ) as any ) . week . dow ;
123127
124128 await setDateFnLocaleAsync ( locale , weekdays , weekdaysShort , firstDayOfWeek ) ;
125129 initI18n ( locale ) ;
126130}
127131
128- async function initializeMetaDataAsync ( dbLocale : string , onQueryApi : Function , minorServerVersion : number ) {
132+ async function initializeMetaDataAsync ( dbLocale : string , onQueryApi : any , minorServerVersion : number ) {
129133 await loadMetaData ( onQueryApi ) ;
130134 await buildMetaDataAsync ( dbLocale , minorServerVersion ) ;
131135}
@@ -144,9 +148,9 @@ export async function initializeAsync({
144148 serverVersion,
145149 baseUrl,
146150} : {
147- onCacheExpired : Function ,
148- querySingleResource : Function ,
149- serverVersion : ServerVersion ,
151+ onCacheExpired : ( ) => void ,
152+ querySingleResource : any ,
153+ serverVersion : any ,
150154 baseUrl : string ,
151155} ) {
152156 setLogLevel ( ) ;
@@ -171,7 +175,6 @@ export async function initializeAsync({
171175 } ,
172176 } ) ;
173177
174- // initialize rule engine
175178 let ruleEngineSettings ;
176179 try {
177180 ruleEngineSettings = await querySingleResource ( {
@@ -182,7 +185,6 @@ export async function initializeAsync({
182185 }
183186 initRulesEngine ( ruleEngineSettings . version , userRoles ) ;
184187
185- // initialize storage controllers
186188 try {
187189 await initStorageControllers ( {
188190 onCacheExpired,
@@ -196,13 +198,10 @@ export async function initializeAsync({
196198 ) , error ) ;
197199 }
198200
199- // set locale data
200201 const uiLocale = userSettings . keyUiLocale ;
201202 const dbLocale = userSettings . keyDbLocale ;
202203 await setLocaleDataAsync ( uiLocale ) ;
203- // initialize system settings
204204 await initializeSystemSettingsAsync ( { ...systemSettings , baseUrl } , { uiLocale, captureScope, searchScope } ) ;
205205
206- // initialize metadata
207206 await initializeMetaDataAsync ( dbLocale , querySingleResource , serverVersion . minor ) ;
208207}
0 commit comments