Skip to content

Commit 0b011a1

Browse files
fix: create fork in login
1 parent 30a4410 commit 0b011a1

3 files changed

Lines changed: 32 additions & 41 deletions

File tree

app/lib/methods/getAppTranslations.ts

Lines changed: 21 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -6,49 +6,35 @@ import log from './helpers/log';
66
import protectedFunction from './helpers/protectedFunction';
77
import sdk from '../services/sdk';
88

9-
async function fetchAndSaveTranslations(language: string, db: any): Promise<void> {
10-
const result = await sdk.get('apps.translations', { language });
11-
12-
if (!result?.success || !result.translations) {
13-
return;
14-
}
15-
16-
await db.write(async () => {
17-
const collection = db.get('app_translations');
18-
19-
const existing = await collection.query(Q.where('language', result.language)).fetch();
20-
const toDelete = existing.map((r: any) => r.prepareDestroyPermanently());
21-
22-
const toCreate = Object.entries(result.translations).map(([key, value]) =>
23-
collection.prepareCreate(
24-
protectedFunction((r: any) => {
25-
r._raw = sanitizedRaw({ id: `${result.language}_${key}` }, collection.schema);
26-
r.key = key;
27-
r.value = value as string;
28-
r.language = result.language;
29-
})
30-
)
31-
);
32-
33-
await db.batch(...toDelete, ...toCreate);
34-
});
35-
}
36-
379
export async function getAppTranslations(language = 'en'): Promise<void> {
3810
try {
3911
const db = database.active;
40-
const collection = db.get('app_translations');
4112

42-
// check if translations already exist in DB for this language
43-
const existing = await collection.query(Q.where('language', language)).fetchCount();
13+
const result = await sdk.get('apps.translations', { language });
4414

45-
if (existing > 0) {
46-
// already have translations — skip fetch
15+
if (!result?.success || !result.translations) {
4716
return;
4817
}
4918

50-
// nothing in DB — fetch from server and save
51-
await fetchAndSaveTranslations(language, db);
19+
await db.write(async () => {
20+
const collection = db.get('app_translations');
21+
22+
const existing = await collection.query(Q.where('language', result.language)).fetch();
23+
const toDelete = existing.map((r: any) => r.prepareDestroyPermanently());
24+
25+
const toCreate = Object.entries(result.translations).map(([key, value]) =>
26+
collection.prepareCreate(
27+
protectedFunction((r: any) => {
28+
r._raw = sanitizedRaw({ id: `${result.language}_${key}` }, collection.schema);
29+
r.key = key;
30+
r.value = value as string;
31+
r.language = result.language;
32+
})
33+
)
34+
);
35+
36+
await db.batch(...toDelete, ...toCreate);
37+
});
5238
} catch (e) {
5339
log(e);
5440
}

app/lib/methods/getSlashCommands.ts

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@ import log from './helpers/log';
55
import protectedFunction from './helpers/protectedFunction';
66
import { type ISlashCommandResult, type TSlashCommandModel } from '../../definitions';
77
import sdk from '../services/sdk';
8-
import { getAppTranslations } from './getAppTranslations';
9-
import I18n from '../../i18n';
108

119
export function getSlashCommands() {
1210
const db = database.active;
@@ -20,10 +18,6 @@ export function getSlashCommands() {
2018
return resolve();
2119
}
2220

23-
// fetch and save app translations if not already in DB
24-
const appLang = I18n.currentLocale();
25-
await getAppTranslations(appLang);
26-
2721
// @ts-ignore
2822
const { commands } = result;
2923
if (commands && commands.length) {

app/sagas/login.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ import appNavigation from '../lib/navigation/appNavigation';
3939
import { showActionSheetRef } from '../containers/ActionSheet';
4040
import { SupportedVersionsWarning } from '../containers/SupportedVersions';
4141
import { isIOS } from '../lib/methods/helpers';
42+
import { getAppTranslations } from '../lib/methods/getAppTranslations';
4243

4344
const getServer = state => state.server.server;
4445
const loginWithPasswordCall = args => loginWithPassword(args);
@@ -184,6 +185,15 @@ const fetchSlashCommandsFork = function* fetchSlashCommandsFork() {
184185
}
185186
};
186187

188+
const fetchAppTranslationsFork = function* fetchAppTranslationsFork() {
189+
try {
190+
const appLang = I18n.currentLocale().split('-')[0];
191+
yield getAppTranslations(appLang);
192+
} catch (e) {
193+
log(e);
194+
}
195+
};
196+
187197
const registerPushTokenFork = function* registerPushTokenFork() {
188198
try {
189199
yield registerPushToken();
@@ -251,6 +261,7 @@ const handleLoginSuccess = function* handleLoginSuccess({ user }) {
251261
yield fork(fetchCustomEmojisFork);
252262
yield fork(fetchRolesFork);
253263
yield fork(fetchSlashCommandsFork);
264+
yield fork(fetchAppTranslationsFork);
254265
yield fork(registerPushTokenFork);
255266
yield fork(fetchUsersPresenceFork);
256267
yield fork(fetchEnterpriseModulesFork, { user });

0 commit comments

Comments
 (0)