feat(mobile): refresh entitlements after scan + route QUOTA_EXCEEDED to paywall
This commit is contained in:
@@ -2,8 +2,8 @@ import { useEffect, useState } from "react";
|
||||
import { Alert, View } from "react-native";
|
||||
import { router } from "expo-router";
|
||||
import * as ImagePicker from "expo-image-picker";
|
||||
import { useQuery } from "@tanstack/react-query";
|
||||
import { api, uploadImage } from "@/lib/api";
|
||||
import { useQuery, useQueryClient } from "@tanstack/react-query";
|
||||
import { api, uploadImage, ApiError } from "@/lib/api";
|
||||
import { t } from "@/lib/i18n";
|
||||
import {
|
||||
Body,
|
||||
@@ -48,7 +48,8 @@ export default function ScanScreen() {
|
||||
const [busyType, setBusyType] = useState<string | null>(null);
|
||||
const coach = useFirstScanCoach();
|
||||
const { track } = useAnalytics();
|
||||
const entitlements = useQuery({
|
||||
const queryClient = useQueryClient();
|
||||
const entitlements = useQuery({
|
||||
queryKey: ["entitlements"],
|
||||
queryFn: () => api<Entitlements>("/v1/me/entitlements"),
|
||||
});
|
||||
@@ -82,7 +83,9 @@ export default function ScanScreen() {
|
||||
body: { scanType, imageCount: 1, contentType: "image/jpeg" },
|
||||
});
|
||||
|
||||
// 3. Ladda upp + starta analysen
|
||||
queryClient.invalidateQueries({ queryKey: ["entitlements"] });
|
||||
|
||||
// 3. Ladda upp + starta analysen
|
||||
const upload = created.uploads[0];
|
||||
if (!upload) throw new Error(t("common.error"));
|
||||
await uploadImage(upload, asset.uri);
|
||||
@@ -91,7 +94,11 @@ export default function ScanScreen() {
|
||||
// 4. Vidare till granskningsvyn som pollar tills resultatet kommer
|
||||
router.push(`${scanType === "plate" ? "/meal-review" : "/scan-review"}/${created.scan.id}`);
|
||||
} catch (err) {
|
||||
Alert.alert(t("common.oops"), err instanceof Error ? err.message : t("common.error"));
|
||||
if (err instanceof ApiError && err.code === "QUOTA_EXCEEDED") {
|
||||
router.push("/paywall");
|
||||
} else {
|
||||
Alert.alert(t("common.oops"), err instanceof Error ? err.message : t("common.error"));
|
||||
}
|
||||
} finally {
|
||||
setBusyType(null);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user