fix(services): resolve owner shop from backend
This commit is contained in:
@@ -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 }
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user