fix(services): resolve owner shop from backend

This commit is contained in:
zetaloop
2026-04-25 15:04:48 +08:00
parent 9c7e207dfe
commit 9cff134cf2
3 changed files with 39 additions and 15 deletions
+15 -5
View File
@@ -5,12 +5,14 @@ import { toApiError } from "@/lib/errors"
import type { Shop } from "@/lib/types"
import { useCallback, useEffect, useState } from "react"
export function useMyShop() {
export function useMyShop(enabled = true) {
const [shop, setShop] = useState<Shop | null>(null)
const [loading, setLoading] = useState(true)
const [loading, setLoading] = useState(enabled)
const [error, setError] = useState<string | null>(null)
const refreshShop = useCallback(async () => {
if (!enabled) return null
setLoading(true)
setError(null)
@@ -29,12 +31,20 @@ export function useMyShop() {
} finally {
setLoading(false)
}
}, [])
}, [enabled])
useEffect(() => {
if (!enabled) return
let cancelled = false
getMyShop()
Promise.resolve()
.then(() => {
if (cancelled) return undefined
setLoading(true)
setError(null)
return getMyShop()
})
.then((nextShop) => {
if (cancelled) return
setShop(nextShop ?? null)
@@ -56,7 +66,7 @@ export function useMyShop() {
return () => {
cancelled = true
}
}, [])
}, [enabled])
return { shop, setShop, loading, error, refreshShop }
}