2026-09-18 12:54:45 +03:30
|
|
|
// Adapted from legacy singleProduct.vue's own fetch (GET /product/:id).
|
2026-10-12 15:19:50 +03:30
|
|
|
//
|
|
|
|
|
// Reads ONE product from GET /catalog/products/:id.
|
|
|
|
|
//
|
|
|
|
|
// It used to fetch the shared catalogue and `find()` the row in it, because the
|
|
|
|
|
// API had no single-product route. With the real catalogue reaching ~16k
|
|
|
|
|
// products that meant opening any product could download all of them, so the
|
|
|
|
|
// route now exists (plan step 2) and this is the first caller moved onto it.
|
2026-10-04 16:18:56 +03:30
|
|
|
//
|
|
|
|
|
// `initial` seeds the page with the ProductSummary the tapped card already
|
|
|
|
|
// holds — the same shape, from the same endpoint — so the detail view renders
|
|
|
|
|
// complete on its first frame instead of mounting blank and jumping when the
|
2026-10-12 15:19:50 +03:30
|
|
|
// fetch lands. The read still runs, silently, and only replaces the product if
|
|
|
|
|
// the server's copy actually differs (stock moved since the list loaded).
|
2026-10-04 16:18:56 +03:30
|
|
|
//
|
|
|
|
|
// The silent refresh waits until the opening morph (ProductMorph.vue, ≤420ms)
|
2026-10-12 15:19:50 +03:30
|
|
|
// is over: swapping the product and re-rendering mid-animation drops frames.
|
2026-09-18 12:54:45 +03:30
|
|
|
import { ref } from 'vue'
|
2026-10-12 15:19:50 +03:30
|
|
|
import { api, UnauthorizedError, type ProductSummary } from '../api/client'
|
2026-09-18 12:54:45 +03:30
|
|
|
|
2026-10-04 16:18:56 +03:30
|
|
|
const SEEDED_REFRESH_DELAY_MS = 600
|
|
|
|
|
|
2026-10-12 15:19:50 +03:30
|
|
|
/** A 404 from the API client, which throws `API <path> failed (404): …`. */
|
|
|
|
|
function isNotFound(e: unknown): boolean {
|
|
|
|
|
return e instanceof Error && !(e instanceof UnauthorizedError) && /\(404\)/.test(e.message)
|
|
|
|
|
}
|
|
|
|
|
|
2026-10-04 16:18:56 +03:30
|
|
|
export function useProductDetail(id: string, initial?: ProductSummary | null) {
|
2026-09-18 12:54:45 +03:30
|
|
|
const loading = ref(false)
|
2026-10-04 16:18:56 +03:30
|
|
|
const flag = ref(Boolean(initial))
|
2026-09-18 12:54:45 +03:30
|
|
|
const notFound = ref(false)
|
2026-10-04 16:18:56 +03:30
|
|
|
const productRef = ref<ProductSummary | null>(initial ?? null)
|
2026-09-18 12:54:45 +03:30
|
|
|
|
|
|
|
|
const load = async () => {
|
2026-10-04 16:18:56 +03:30
|
|
|
const seeded = productRef.value !== null
|
|
|
|
|
if (seeded) await new Promise((resolve) => setTimeout(resolve, SEEDED_REFRESH_DELAY_MS))
|
|
|
|
|
else loading.value = true
|
2026-09-18 12:54:45 +03:30
|
|
|
try {
|
2026-10-12 15:19:50 +03:30
|
|
|
const found = await api.getProduct(id)
|
2026-10-04 16:18:56 +03:30
|
|
|
if (seeded) {
|
2026-10-12 15:19:50 +03:30
|
|
|
if (JSON.stringify(found) !== JSON.stringify(productRef.value)) productRef.value = found
|
2026-10-04 16:18:56 +03:30
|
|
|
} else {
|
|
|
|
|
productRef.value = found
|
|
|
|
|
}
|
2026-09-18 12:54:45 +03:30
|
|
|
flag.value = true
|
|
|
|
|
} catch (e) {
|
2026-10-12 15:19:50 +03:30
|
|
|
// Only a real 404 means "no such product". Anything else — offline, a
|
|
|
|
|
// 500 — must not render the "not found" page: before this route existed
|
|
|
|
|
// a failed catalogue fetch left `notFound` false, and that is still the
|
|
|
|
|
// right answer, because the product may well exist.
|
|
|
|
|
if (isNotFound(e)) {
|
|
|
|
|
if (!seeded) {
|
|
|
|
|
productRef.value = null
|
|
|
|
|
notFound.value = true
|
|
|
|
|
}
|
|
|
|
|
// Seeded and now 404: the product was pulled from the storefront while
|
|
|
|
|
// the page was open. The card's copy stays on screen rather than
|
|
|
|
|
// blanking under the person mid-read; the cart refuses it at checkout.
|
|
|
|
|
} else {
|
|
|
|
|
console.log(e)
|
|
|
|
|
}
|
|
|
|
|
flag.value = true
|
2026-09-18 12:54:45 +03:30
|
|
|
} finally {
|
|
|
|
|
loading.value = false
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return { loading, flag, notFound, productRef, load }
|
|
|
|
|
}
|