From f59b7330a15d62bc5c69c5eb43af0292bf004b03 Mon Sep 17 00:00:00 2001 From: Ammar Ahmed Date: Mon, 10 Mar 2025 09:40:17 +0500 Subject: [PATCH] mobile: fix logout button not working in user sheet --- .../app/components/sheets/user/index.tsx | 3 + apps/mobile/app/screens/settings/logout.ts | 92 +++++++++++++++++++ .../app/screens/settings/settings-data.tsx | 85 +---------------- 3 files changed, 97 insertions(+), 83 deletions(-) create mode 100644 apps/mobile/app/screens/settings/logout.ts diff --git a/apps/mobile/app/components/sheets/user/index.tsx b/apps/mobile/app/components/sheets/user/index.tsx index ed1c81e7d..125046beb 100644 --- a/apps/mobile/app/components/sheets/user/index.tsx +++ b/apps/mobile/app/components/sheets/user/index.tsx @@ -39,6 +39,7 @@ import Paragraph from "../../ui/typography/paragraph"; import Sync from "../../../services/sync"; import Clipboard from "@react-native-clipboard/clipboard"; +import { logoutUser } from "../../../screens/settings/logout"; export const UserSheet = () => { const ref = useSheetRef(); const { colors } = useThemeColors(); @@ -313,6 +314,8 @@ export const UserSheet = () => { icon: "logout", title: strings.logout(), onPress: () => { + console.log("logout"); + logoutUser(); ref.current?.hide(); }, hidden: !user diff --git a/apps/mobile/app/screens/settings/logout.ts b/apps/mobile/app/screens/settings/logout.ts new file mode 100644 index 000000000..9b0b63051 --- /dev/null +++ b/apps/mobile/app/screens/settings/logout.ts @@ -0,0 +1,92 @@ +import { strings } from "@notesnook/intl"; +import { presentDialog } from "../../components/dialog/functions"; +import { DatabaseLogger, db } from "../../common/database"; +import { eSendEvent, ToastManager } from "../../services/event-manager"; +import { eCloseSimpleDialog } from "../../utils/events"; +import { + endProgress, + startProgress, + updateProgress +} from "../../components/dialogs/progress"; +import Navigation from "../../services/navigation"; +import BackupService from "../../services/backup"; + +export async function logoutUser() { + const hasUnsyncedChanges = await db.hasUnsyncedChanges(); + presentDialog({ + title: strings.logout(), + paragraph: strings.logoutConfirmation(), + positiveText: strings.logout(), + check: { + info: strings.backupDataBeforeLogout(), + defaultValue: true + }, + notice: hasUnsyncedChanges + ? { + text: strings.unsyncedChangesWarning(), + type: "alert" + } + : undefined, + positivePress: async (_, takeBackup) => { + eSendEvent(eCloseSimpleDialog); + setTimeout(async () => { + try { + startProgress({ + fillBackground: true, + title: strings.loggingOut(), + canHideProgress: true, + paragraph: strings.loggingOutDesc() + }); + + Navigation.navigate("Notes"); + + if (takeBackup) { + updateProgress({ + progress: strings.backingUpData() + }); + + try { + const result = await BackupService.run(false, "local", "partial"); + if (result?.error) throw result.error as Error; + } catch (e) { + DatabaseLogger.error(e); + const error = e; + const canLogout = await new Promise((resolve) => { + presentDialog({ + context: "local", + title: strings.failedToTakeBackup(), + paragraph: `${ + (error as Error).message + }. ${strings.failedToTakeBackupMessage()}?`, + positiveText: strings.yes(), + negativeText: strings.no(), + positivePress: () => { + resolve(true); + }, + onClose: () => { + resolve(false); + } + }); + }); + if (!canLogout) { + endProgress(); + return; + } + } + } + + updateProgress({ + progress: strings.loggingOut() + }); + + await db.user?.logout(); + endProgress(); + } catch (e) { + DatabaseLogger.error(e); + ToastManager.error(e as Error, strings.logoutError()); + endProgress(); + } + }, 300); + } + }); +} diff --git a/apps/mobile/app/screens/settings/settings-data.tsx b/apps/mobile/app/screens/settings/settings-data.tsx index 3c529db59..5d5c67a07 100644 --- a/apps/mobile/app/screens/settings/settings-data.tsx +++ b/apps/mobile/app/screens/settings/settings-data.tsx @@ -76,6 +76,7 @@ import { useDragState } from "./editor/state"; import { verifyUser, verifyUserWithApplock } from "./functions"; import { SettingSection } from "./types"; import { getTimeLeft } from "./user-section"; +import { logoutUser } from "./logout"; export const settingsGroups: SettingSection[] = [ { @@ -412,89 +413,7 @@ export const settingsGroups: SettingSection[] = [ name: strings.logout(), description: strings.logoutWarnin(), icon: "logout", - modifer: async () => { - const hasUnsyncedChanges = await db.hasUnsyncedChanges(); - presentDialog({ - title: strings.logout(), - paragraph: strings.logoutConfirmation(), - positiveText: strings.logout(), - check: { - info: strings.backupDataBeforeLogout(), - defaultValue: true - }, - notice: hasUnsyncedChanges - ? { - text: strings.unsyncedChangesWarning(), - type: "alert" - } - : undefined, - positivePress: async (_, takeBackup) => { - eSendEvent(eCloseSimpleDialog); - setTimeout(async () => { - try { - startProgress({ - fillBackground: true, - title: strings.loggingOut(), - canHideProgress: true, - paragraph: strings.loggingOutDesc() - }); - - Navigation.navigate("Notes"); - - if (takeBackup) { - updateProgress({ - progress: strings.backingUpData() - }); - - try { - const result = await BackupService.run( - false, - "local", - "partial" - ); - if (result?.error) throw result.error as Error; - } catch (e) { - DatabaseLogger.error(e); - const error = e; - const canLogout = await new Promise((resolve) => { - presentDialog({ - context: "local", - title: strings.failedToTakeBackup(), - paragraph: `${ - (error as Error).message - }. ${strings.failedToTakeBackupMessage()}?`, - positiveText: strings.yes(), - negativeText: strings.no(), - positivePress: () => { - resolve(true); - }, - onClose: () => { - resolve(false); - } - }); - }); - if (!canLogout) { - endProgress(); - return; - } - } - } - - updateProgress({ - progress: strings.loggingOut() - }); - - await db.user?.logout(); - endProgress(); - } catch (e) { - DatabaseLogger.error(e); - ToastManager.error(e as Error, strings.logoutError()); - endProgress(); - } - }, 300); - } - }); - } + modifer: logoutUser }, { id: "delete-account",