Skip to content

Commit 8a253ea

Browse files
refactor: convert AppLoader and AppStart folders from Flow to TypeScript
- Convert AppLoader.component.js to AppLoader.component.tsx with proper TypeScript types - Convert AppLoader/index.js to index.ts - Convert AppLoader/init.js to init.ts with type annotations for functions and parameters - Convert AppStart/appStart.actions.js to appStart.actions.ts with proper parameter types - Convert AppStart/appStart.epics.js to appStart.epics.ts with InputObservable type - Remove original .js files after creating TypeScript equivalents - Follow minimalistic conversion approach - only add types, no code restructuring - All TypeScript compilation and linting checks pass Co-Authored-By: [email protected] <[email protected]>
1 parent 116183f commit 8a253ea

File tree

5 files changed

+41
-39
lines changed

5 files changed

+41
-39
lines changed

src/components/AppLoader/AppLoader.component.js renamed to src/components/AppLoader/AppLoader.component.tsx

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
// @flow
21
import React, { useCallback, useMemo, useEffect } from 'react';
32
import log from 'loglevel';
43
import { useDataEngine, useConfig, useTimeZoneConversion } from '@dhis2/app-runtime';
@@ -12,9 +11,14 @@ import { initFeatureAvailability } from 'capture-core-utils/featuresSupport';
1211
import { initializeAsync } from './init';
1312
import { getStore } from '../../store/getStore';
1413

14+
type PlainReduxStore = {
15+
dispatch: (action: any) => void;
16+
getState: () => any;
17+
};
18+
1519
type Props = {
16-
onRunApp: (store: PlainReduxStore) => void,
17-
onCacheExpired: Function,
20+
onRunApp: (store: PlainReduxStore) => void;
21+
onCacheExpired: () => void;
1822
};
1923

2024
const useApiUtils = () => {
@@ -24,7 +28,7 @@ const useApiUtils = () => {
2428
return useMemo(() => ({
2529
querySingleResource: makeQuerySingleResource(dataEngine.query.bind(dataEngine)),
2630
mutate: dataEngine.mutate.bind(dataEngine),
27-
absoluteApiPath: buildUrl(dataEngine.link.config.baseUrl, dataEngine.link.versionedApiPath),
31+
absoluteApiPath: buildUrl((dataEngine as any).link.config.baseUrl, (dataEngine as any).link.versionedApiPath),
2832
serverVersion,
2933
baseUrl,
3034
fromClientDate,
@@ -33,7 +37,7 @@ const useApiUtils = () => {
3337

3438
export const AppLoader = (props: Props) => {
3539
const { onRunApp, onCacheExpired } = props;
36-
const [loadError, setLoadError] = React.useState(null);
40+
const [loadError, setLoadError] = React.useState<string | null>(null);
3741
const { querySingleResource, mutate, absoluteApiPath, serverVersion, baseUrl, fromClientDate } = useApiUtils();
3842

3943
const { navigate } = useNavigate();
@@ -52,7 +56,7 @@ export const AppLoader = (props: Props) => {
5256
await initializeAsync({
5357
onCacheExpired,
5458
querySingleResource,
55-
serverVersion,
59+
serverVersion: serverVersion as any,
5660
baseUrl,
5761
});
5862
const store = await getStore(
@@ -67,7 +71,7 @@ export const AppLoader = (props: Props) => {
6771
} catch (error) {
6872
let message = 'The application could not be loaded.';
6973
if (error && error instanceof DisplayException) {
70-
logError(error.innerError);
74+
logError((error as any).innerError);
7175
message += ` ${error.toString()}`;
7276
} else {
7377
logError(error);
Original file line numberDiff line numberDiff line change
@@ -1,2 +1 @@
1-
// @flow
21
export { AppLoader } from './AppLoader.component';
Lines changed: 27 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,27 @@
1-
// @flow
2-
/* eslint-disable import/prefer-default-export */
31
import log from 'loglevel';
4-
import type { ServerVersion } from '@dhis2/app-runtime';
52
import { environments } from 'capture-core/constants/environments';
63
import moment from 'moment';
74
import { CurrentLocaleData } from 'capture-core/utils/localeData/CurrentLocaleData';
85
import i18n from '@dhis2/d2-i18n';
9-
import type { LocaleDataType } from 'capture-core/utils/localeData/CurrentLocaleData';
10-
116
import { loadMetaData, cacheSystemSettings } from 'capture-core/metaDataStoreLoaders';
127
import { buildMetaDataAsync, buildSystemSettingsAsync } from 'capture-core/metaDataMemoryStoreBuilders';
138
import { initStorageControllers } from 'capture-core/storageControllers';
149
import { DisplayException } from 'capture-core/utils/exceptions';
1510
import { 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+
1725
function 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

5662
function 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
}
Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
// @flow
21
import { actionCreator } from 'capture-core/actions/actions.utils';
32

43
export const appStartActionTypes = {
@@ -8,5 +7,5 @@ export const appStartActionTypes = {
87
};
98

109
export const loadApp = () => actionCreator(appStartActionTypes.APP_LOAD)();
11-
export const loadAppSuccess = (payload: mixed) => actionCreator(appStartActionTypes.APP_LOAD_SUCESS)(payload);
10+
export const loadAppSuccess = (payload: any) => actionCreator(appStartActionTypes.APP_LOAD_SUCESS)(payload);
1211
export const loadAppFailed = (message: string) => actionCreator(appStartActionTypes.APP_LOAD_FAILED)({ message });
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
1-
// @flow
21
import { ofType } from 'redux-observable';
32
import { map } from 'rxjs/operators';
43
import { loadCore, actionTypes as coreActionTypes } from 'capture-core/init';
54
import { loadAppSuccess, appStartActionTypes } from './appStart.actions';
65

6+
type InputObservable = any;
7+
78
export const triggerLoadCoreEpic = (action$: InputObservable) =>
89
action$.pipe(
910
ofType(appStartActionTypes.APP_LOAD),

0 commit comments

Comments
 (0)