refactor(frontend): migrate GarminConnection.tsx to the banner system
Removes the local error state and its inline paragraph; every failure (connect, MFA, sync trigger, reset-all) now calls showError directly.
This commit is contained in:
@@ -1,5 +1,6 @@
|
|||||||
import { useEffect, useState } from "react";
|
import { useEffect, useState } from "react";
|
||||||
import { api } from "../api/client";
|
import { api } from "../api/client";
|
||||||
|
import { showError } from "../banner";
|
||||||
import type { AuthResponse } from "../types/api";
|
import type { AuthResponse } from "../types/api";
|
||||||
import { SyncModal } from "./SyncModal";
|
import { SyncModal } from "./SyncModal";
|
||||||
|
|
||||||
@@ -7,7 +8,6 @@ export function GarminConnection({ onBeforeConnect }: { onBeforeConnect?: () =>
|
|||||||
const [auth, setAuth] = useState<AuthResponse | null>(null);
|
const [auth, setAuth] = useState<AuthResponse | null>(null);
|
||||||
const [code, setCode] = useState("");
|
const [code, setCode] = useState("");
|
||||||
const [busy, setBusy] = useState(false);
|
const [busy, setBusy] = useState(false);
|
||||||
const [error, setError] = useState<string | null>(null);
|
|
||||||
// There's no real Garmin logout -- the backend session just sits idle.
|
// There's no real Garmin logout -- the backend session just sits idle.
|
||||||
// "Disconnect" only hides that and shows Connect again locally; a real
|
// "Disconnect" only hides that and shows Connect again locally; a real
|
||||||
// login (connect()) clears it.
|
// login (connect()) clears it.
|
||||||
@@ -18,7 +18,7 @@ export function GarminConnection({ onBeforeConnect }: { onBeforeConnect?: () =>
|
|||||||
// it's open, so this no longer needs the "poll faster while syncing"
|
// it's open, so this no longer needs the "poll faster while syncing"
|
||||||
// dynamic interval it used to have.
|
// dynamic interval it used to have.
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
api.authStatus().then(setAuth).catch((e) => setError(String(e)));
|
api.authStatus().then(setAuth).catch((e) => showError(String(e)));
|
||||||
const interval = setInterval(() => {
|
const interval = setInterval(() => {
|
||||||
api.authStatus().then(setAuth).catch(() => {});
|
api.authStatus().then(setAuth).catch(() => {});
|
||||||
}, 6000);
|
}, 6000);
|
||||||
@@ -27,13 +27,12 @@ export function GarminConnection({ onBeforeConnect }: { onBeforeConnect?: () =>
|
|||||||
|
|
||||||
async function connect() {
|
async function connect() {
|
||||||
setBusy(true);
|
setBusy(true);
|
||||||
setError(null);
|
|
||||||
try {
|
try {
|
||||||
await onBeforeConnect?.();
|
await onBeforeConnect?.();
|
||||||
setAuth(await api.login());
|
setAuth(await api.login());
|
||||||
setDisconnected(false);
|
setDisconnected(false);
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
setError(String(e));
|
showError(String(e));
|
||||||
} finally {
|
} finally {
|
||||||
setBusy(false);
|
setBusy(false);
|
||||||
}
|
}
|
||||||
@@ -46,12 +45,11 @@ export function GarminConnection({ onBeforeConnect }: { onBeforeConnect?: () =>
|
|||||||
async function submitMFA() {
|
async function submitMFA() {
|
||||||
if (!code.trim()) return;
|
if (!code.trim()) return;
|
||||||
setBusy(true);
|
setBusy(true);
|
||||||
setError(null);
|
|
||||||
try {
|
try {
|
||||||
setAuth(await api.submitMFA(code.trim()));
|
setAuth(await api.submitMFA(code.trim()));
|
||||||
setCode("");
|
setCode("");
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
setError(String(e));
|
showError(String(e));
|
||||||
} finally {
|
} finally {
|
||||||
setBusy(false);
|
setBusy(false);
|
||||||
}
|
}
|
||||||
@@ -59,12 +57,11 @@ export function GarminConnection({ onBeforeConnect }: { onBeforeConnect?: () =>
|
|||||||
|
|
||||||
async function sync() {
|
async function sync() {
|
||||||
setBusy(true);
|
setBusy(true);
|
||||||
setError(null);
|
|
||||||
try {
|
try {
|
||||||
await api.syncRun();
|
await api.syncRun();
|
||||||
setShowSyncModal(true);
|
setShowSyncModal(true);
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
setError(String(e));
|
showError(String(e));
|
||||||
} finally {
|
} finally {
|
||||||
setBusy(false);
|
setBusy(false);
|
||||||
}
|
}
|
||||||
@@ -75,7 +72,6 @@ export function GarminConnection({ onBeforeConnect }: { onBeforeConnect?: () =>
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
setBusy(true);
|
setBusy(true);
|
||||||
setError(null);
|
|
||||||
try {
|
try {
|
||||||
await api.resetSync();
|
await api.resetSync();
|
||||||
// Reset runs as a background sync job; wait for it to actually finish
|
// Reset runs as a background sync job; wait for it to actually finish
|
||||||
@@ -88,7 +84,7 @@ export function GarminConnection({ onBeforeConnect }: { onBeforeConnect?: () =>
|
|||||||
}
|
}
|
||||||
window.location.reload();
|
window.location.reload();
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
setError(String(e));
|
showError(String(e));
|
||||||
setBusy(false);
|
setBusy(false);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -153,8 +149,6 @@ export function GarminConnection({ onBeforeConnect }: { onBeforeConnect?: () =>
|
|||||||
|
|
||||||
{!disconnected && auth?.message && <p className="garmin-connection-message">{auth.message}</p>}
|
{!disconnected && auth?.message && <p className="garmin-connection-message">{auth.message}</p>}
|
||||||
|
|
||||||
{error && <p className="error">{error}</p>}
|
|
||||||
|
|
||||||
{showSyncModal && <SyncModal onClose={() => setShowSyncModal(false)} />}
|
{showSyncModal && <SyncModal onClose={() => setShowSyncModal(false)} />}
|
||||||
</div>
|
</div>
|
||||||
);
|
);
|
||||||
|
|||||||
Reference in New Issue
Block a user