freshyo/apps/admin-web/src/components/GlobalProductForm.tsx
2026-10-10 15:00:50 +05:30

158 lines
6.2 KiB
TypeScript

import { useMemo, useState } from 'react'
import { p as P } from 'web-components'
import type { AdminProductWithRelations } from '@packages/shared'
/**
* Global products are owned by a superadmin and shared across every region. A
* regional admin may only edit the market data for their own region — the
* product structure (name, descriptions, images, features, combos, store) is
* read-only here.
*/
interface GlobalProductFormProps {
product: AdminProductWithRelations
onSubmit: (skus: any[]) => void
isLoading: boolean
}
interface VariantState {
price: string
marketPrice: string
flashPrice: string
isFlashAvailable: boolean
isOutOfStock: boolean
isOffer: boolean
isSuspended: boolean
}
const toNumOrUndef = (v: string): number | undefined => {
const n = parseFloat(v)
return Number.isFinite(n) ? n : undefined
}
function GlobalProductForm({ product, onSubmit, isLoading }: GlobalProductFormProps) {
const skus = product.skus || []
const [variants, setVariants] = useState<VariantState[]>(
useMemo(
() =>
skus.map((sku) => ({
price: sku.price != null ? String(sku.price) : '',
marketPrice: sku.marketPrice != null ? String(sku.marketPrice) : '',
flashPrice: sku.flashPrice != null ? String(sku.flashPrice) : '',
isFlashAvailable: sku.isFlashAvailable ?? false,
isOutOfStock: sku.isOutOfStock ?? false,
isOffer: sku.isOffer ?? false,
isSuspended: sku.isSuspended ?? false,
})),
[product]
)
)
const setVariant = (index: number, patch: Partial<VariantState>) =>
setVariants((prev) => prev.map((v, i) => (i === index ? { ...v, ...patch } : v)))
const handleSubmit = () => {
const payload = skus.map((sku, i) => {
const v = variants[i]
return {
id: sku.id,
name: sku.name ?? null,
price: toNumOrUndef(v.price) ?? 0,
marketPrice: toNumOrUndef(v.marketPrice) ?? null,
images: sku.images ?? [],
isFlashAvailable: v.isFlashAvailable,
flashPrice: v.isFlashAvailable ? (toNumOrUndef(v.flashPrice) ?? null) : null,
isOutOfStock: v.isOutOfStock,
isOffer: v.isOffer,
isComboOnly: sku.isComboOnly ?? false,
isDeleted: sku.isDeleted ?? false,
isSuspended: v.isSuspended,
features: (sku.features || []).map((f) => ({
featureName: f.featureName,
featureValue: f.featureValue,
})),
comboItems: (sku.comboItems || []).map((ci: any) => ({ skuId: ci.skuId })),
}
})
onSubmit(payload)
}
const numberInput = (label: string, value: string, onChange: (t: string) => void) => (
<label className="mb-3 block">
<span className="mb-1 block text-sm font-medium text-gray-500">{label}</span>
<input
type="number"
inputMode="decimal"
value={value}
onChange={(e) => onChange(e.target.value)}
className="w-full rounded-lg border border-gray-200 px-3 py-2 text-gray-900 focus:border-blue-400 focus:outline-none"
/>
</label>
)
const toggle = (label: string, checked: boolean, onToggle: () => void) => (
<label className="mb-3 flex items-center gap-3">
<input type="checkbox" checked={checked} onChange={onToggle} className="h-4 w-4" />
<span className="text-gray-700">{label}</span>
</label>
)
return (
<div className="mx-auto max-w-3xl p-4 pb-16">
<div className="mb-4 rounded-2xl border border-blue-100 bg-blue-50 p-4">
<P className="mb-1 font-bold text-blue-800">Global product</P>
<P className="text-sm text-blue-700">
Shared across all regions. You can edit the price &amp; stock for your region only.
</P>
</div>
<div className="mb-4 rounded-2xl border border-gray-100 bg-white p-4">
<P className="mb-2 text-xs uppercase tracking-wider text-gray-400">Product details (read-only)</P>
<P className="text-lg font-bold text-gray-900">{product.name}</P>
{product.shortDescription ? <P className="mt-1 text-sm text-gray-500">{product.shortDescription}</P> : null}
<P className="mt-2 text-sm text-gray-500">
Store: {product.store?.name ?? '—'} · Type: {product.productType}
</P>
</div>
{skus.map((sku, index) => {
const v = variants[index]
return (
<div key={sku.id} className="mb-4 rounded-2xl border border-gray-100 bg-white p-4">
<div className="mb-3 flex items-center gap-3">
{sku.images && sku.images[0] ? (
<img src={sku.images[0]} alt="" className="h-12 w-12 rounded-lg object-cover" />
) : null}
<div className="flex-1">
<P className="font-bold text-gray-900">{sku.name || product.name}</P>
<P className="text-xs text-gray-400">
{(sku.features || []).map((f) => f.featureValue).join(' · ') || '—'}
</P>
</div>
</div>
<P className="mb-2 text-xs uppercase tracking-wider text-gray-400">Market data (your region)</P>
{numberInput('Price (₹)', v.price, (t) => setVariant(index, { price: t }))}
{numberInput('Market price (₹)', v.marketPrice, (t) => setVariant(index, { marketPrice: t }))}
{toggle('Flash available', v.isFlashAvailable, () => setVariant(index, { isFlashAvailable: !v.isFlashAvailable }))}
{v.isFlashAvailable && numberInput('Flash price (₹)', v.flashPrice, (t) => setVariant(index, { flashPrice: t }))}
{toggle('Out of stock', v.isOutOfStock, () => setVariant(index, { isOutOfStock: !v.isOutOfStock }))}
{toggle('Offer SKU', v.isOffer, () => setVariant(index, { isOffer: !v.isOffer }))}
{toggle('Suspend in my region', v.isSuspended, () => setVariant(index, { isSuspended: !v.isSuspended }))}
</div>
)
})}
<button
type="button"
onClick={handleSubmit}
disabled={isLoading}
className="w-full rounded-lg bg-blue-500 px-4 py-2 text-lg font-bold text-white shadow-lg disabled:opacity-70"
>
{isLoading ? 'Saving...' : 'Save market data'}
</button>
</div>
)
}
export default GlobalProductForm