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