feat: one piece per variant and optional admin rial wage

A customer cart line is a single piece, the home reel opens on tap, and a variant can be saved without a rial wage.

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
mahmood
2026-11-26 19:11:09 +03:30
co-authored by Cursor
parent 3e9f532c6c
commit 3800d2d50e
13 changed files with 158 additions and 250 deletions
@@ -7,7 +7,7 @@
:aria-label="mode === 'create' ? 'افزودن تنوع جدید' : 'ویرایش تنوع'"
@keydown="onKeydown"
>
<!-- required: what every weight variant needs -->
<!-- weight and percent wage are required; rial wage may be left empty -->
<div class="vre__primary">
<a-form-item
class="vre__item"
@@ -51,7 +51,6 @@
<a-form-item
class="vre__item"
label="اجرت ریالی"
required
:validate-status="errors.rial_pay ? 'error' : ''"
:help="errors.rial_pay"
>
@@ -61,7 +60,7 @@
:min="0"
:step="50000"
addon-after="ریال"
placeholder="نداشت: صفر"
placeholder="اختیاری"
:formatter="formatThousands"
:parser="parseThousands"
/>
@@ -111,7 +111,7 @@ export function validateVariant(v: VariantRow): VariantErrors {
else if (Number(v.grams) <= 0) e.grams = 'وزن باید بیشتر از صفر باشد';
if (!isBlank(v.qty) && Number(v.qty) < 0) e.qty = 'تعداد منفی نمی‌شود';
if (isBlank(v.percentage_pay)) e.percentage_pay = 'اجرت درصدی را وارد کنید';
if (isBlank(v.rial_pay)) e.rial_pay = 'اجرت ریالی را وارد کنید؛ اگر ندارد صفر بزنید';
if (!isBlank(v.rial_pay) && Number(v.rial_pay) < 0) e.rial_pay = 'اجرت ریالی منفی نمی‌شود';
return e;
}
@@ -1,3 +0,0 @@
<svg width="12" height="9" viewBox="0 0 12 9" fill="none" xmlns="http://www.w3.org/2000/svg">
<path d="M6 9C5.91177 5.77359 3.66177 4.75472 0 4.5C3.57353 4.37264 5.91177 3.18396 6 0C6.08824 3.22642 8.38235 4.4151 12 4.5C8.38235 4.75472 6.08824 5.77359 6 9Z" fill="#8D6A46"/>
</svg>

Before

Width:  |  Height:  |  Size: 282 B

@@ -69,6 +69,13 @@ const dragging = ref(false)
const drag = ref(0)
let startX = 0
let pointerId: number | null = null
// The cell under the finger at pointerdown. Pointer capture retargets
// pointerup (and therefore click) onto the stage, so the card's own click
// never runs — a tap has to be resolved from this index instead.
let pressedIndex: number | null = null
// Set when pointerup already handled the tap, so a click that does arrive
// afterwards does not open the product a second time.
let tapHandled = false
const SWIPE_THRESHOLD = 46
@@ -153,12 +160,23 @@ function prev() {
go(index.value - 1)
}
function indexFromEvent(e: Event): number | null {
const target = e.target
if (!(target instanceof Element) || !stage.value) return null
const cell = target.closest('.reel__cell')
if (!cell) return null
const i = [...stage.value.querySelectorAll('.reel__cell')].indexOf(cell)
return i >= 0 ? i : null
}
function onDown(e: PointerEvent) {
if (count.value < 2) return
dragging.value = true
startX = e.clientX
drag.value = 0
pointerId = e.pointerId
pressedIndex = indexFromEvent(e)
tapHandled = false
;(e.currentTarget as HTMLElement).setPointerCapture?.(e.pointerId)
}
@@ -171,33 +189,57 @@ function onMove(e: PointerEvent) {
function onUp(e: PointerEvent) {
if (!dragging.value) return
const moved = e.clientX - startX
const pressed = pressedIndex
dragging.value = false
drag.value = 0
pointerId = null
pressedIndex = null
// RTL: dragging right pushes the stack toward the previous card.
if (moved > SWIPE_THRESHOLD) prev()
else if (moved < -SWIPE_THRESHOLD) next()
if (moved > SWIPE_THRESHOLD) {
prev()
return
}
if (moved < -SWIPE_THRESHOLD) {
next()
return
}
// A tap. Pointer capture delivers this pointerup to the stage, so the
// card's click listener never runs; the cell recorded on pointerdown is
// what was actually tapped.
tapHandled = true
activate(pressed)
}
// A tap on a side card centres it instead of opening it, and a tap that ended
// a drag does neither.
function onOpen(i: number, product: ProductSummary) {
if (Math.abs(drag.value) > 1) return
// A tap on a side card centres it instead of opening it. The centred card
// opens, and the trailing tile goes to the full newest-products list.
function activate(i: number | null) {
if (i == null) return
const slot = slots.value[i]
if (!slot) return
if (i !== index.value) {
go(i)
return
}
emit('open', product)
if (slot.kind === 'product' && slot.product) emit('open', slot.product)
else emit('all')
}
function onOpen(i: number, _product: ProductSummary) {
if (tapHandled) {
tapHandled = false
return
}
activate(i)
}
function onAll(i: number) {
if (Math.abs(drag.value) > 1) return
if (i !== index.value) {
go(i)
if (tapHandled) {
tapHandled = false
return
}
emit('all')
activate(i)
}
</script>
@@ -82,14 +82,30 @@ function format(v: number) {
display: block;
}
// The stand-in is the brand mark, not a photo, so it stays a mark in the
// middle of the frame instead of filling the slot the piece will occupy.
.panel__art img[lazy='loading'],
.panel__art img[lazy='error'] {
max-width: 62%;
max-height: 52%;
}
// Dark mode: product photos are shot on white, so they sit on a light mat
// instead of showing as white boxes on a dark card (@photoMat — transparent,
// i.e. no change, in light mode).
// i.e. no change, in light mode). The brand mark that stands in while a photo
// loads is black line art, so it stays on the card and is drawn light, the
// same way the header logo is.
html.theme-dark .panel__art img {
background: @photoMat;
border-radius: 12px;
}
html.theme-dark .panel__art img[lazy='loading'],
html.theme-dark .panel__art img[lazy='error'] {
background: transparent;
filter: invert(1);
}
.panel__title {
margin: 14px 0 0;
font-size: 14px;
@@ -11,8 +11,8 @@
field existed; that was wrong, and the customer was charged a figure the cart
never showed.
All cart behaviour is unchanged: quantity increment/decrement, remove at
qty 1, and the note affordance that opens the parent's drawer.
All cart behaviour for a line: it is one piece, so the row can be removed,
noted, or moved to buy-later. There is no quantity.
-->
<template>
<article class="ci">
@@ -32,41 +32,11 @@
<span v-if="rialWage > 0" class="ci__wage">اجرت ریالی {{ price(rialWage) }} ریال</span>
</div>
<p v-if="rialWage > 0 && value.qty > 1" class="ci__rialTotal">
جمع اجرت {{ price(rialWage * value.qty) }} ریال
</p>
<div class="ci__row">
<!-- App-wide stepper order rule (also followed by SingleProduct.vue's
.pd__qty): plus on the visual right, minus (or trash at qty 1) on
the visual left. The page is RTL, so a plain flex row lays its
DOM-first child on the right — plus has to come FIRST in markup,
not last, to land there. Keep both steppers' DOM order in sync if
either one changes. -->
<div class="ci__qty">
<button
type="button"
class="ci__qtyBtn"
aria-label="افزودن"
:disabled="!canIncrement"
@click="increment"
>
<Icon icon="vuesax-linear:add" size="16" />
</button>
<span class="ci__qtyValue">{{ value.qty }}</span>
<button
v-if="value.qty === 1"
type="button"
class="ci__qtyBtn ci__qtyBtn--danger"
aria-label="حذف"
@click="removeLine"
>
<button type="button" class="ci__remove" aria-label="حذف" @click="removeLine">
<Icon icon="vuesax-linear:trash" size="16" />
حذف
</button>
<button v-else type="button" class="ci__qtyBtn" aria-label="کاهش" @click="decrement">
<Icon icon="vuesax-linear:minus" size="16" />
</button>
</div>
<button type="button" class="ci__note" @click="emit('note', listIndex)">
<Icon icon="vuesax-linear:note-text" size="15" />
@@ -124,25 +94,6 @@ const listIndex = computed(() =>
props.mode === 'buyLater' ? buyLater.find_index(props.value) : cart.find_index(props.value),
)
// Active cart: stock ceiling. Buy-later: uncapped (desired qty for later).
const maxQty = computed(() => (props.mode === 'cart' ? cart.maxQty(props.value) : null))
const canIncrement = computed(() => maxQty.value === null || props.value.qty < maxQty.value)
const increment = () => {
if (!canIncrement.value) return
if (props.mode === 'buyLater') {
buyLater.updateQty(props.value, props.value.qty + 1)
return
}
cart.updateFeild(props.value.qty + 1, listIndex.value, props.value, 'qty')
}
const decrement = () => {
if (props.mode === 'buyLater') {
buyLater.updateQty(props.value, props.value.qty - 1)
return
}
cart.updateFeild(props.value.qty - 1, listIndex.value, props.value, 'qty')
}
const removeLine = () => {
if (props.mode === 'buyLater') buyLater.remove(props.value)
else cart.remove(props.value)
@@ -195,14 +146,27 @@ const wageLabel = computed(() => {
display: block;
}
.ci__art img[lazy='loading'],
.ci__art img[lazy='error'] {
width: 64px;
height: auto;
}
// Dark mode: product photos are shot on white, so they sit on a light mat
// instead of showing as white boxes on a dark card (@photoMat — transparent,
// i.e. no change, in light mode).
// i.e. no change, in light mode). The brand mark that stands in while a photo
// loads is black line art, so it stays on the card and is drawn light.
html.theme-dark .ci__art img {
background: @photoMat;
border-radius: 12px;
}
html.theme-dark .ci__art img[lazy='loading'],
html.theme-dark .ci__art img[lazy='error'] {
background: transparent;
filter: invert(1);
}
.ci__artEmpty {
width: 100%;
height: 100%;
@@ -247,13 +211,6 @@ html.theme-dark .ci__art img {
border-inline-start: 1px solid @hairline;
}
.ci__rialTotal {
margin: 5px 0 0;
font-size: 11.5px;
font-weight: 700;
color: @wine;
}
.ci__row {
display: flex;
align-items: center;
@@ -262,43 +219,24 @@ html.theme-dark .ci__art img {
padding-top: 10px;
}
.ci__qty {
.ci__remove {
display: inline-flex;
align-items: center;
gap: 4px;
padding: 3px;
border-radius: 999px;
background: @hairlineSoft;
border: 1px solid @hairline;
}
.ci__qtyBtn {
width: 27px;
height: 27px;
gap: 5px;
height: 32px;
padding: 0 12px;
border: 0;
border-radius: 999px;
cursor: pointer;
border-radius: 50%;
display: grid;
place-items: center;
color: @inkSoft;
background: @canvas;
transition: transform 0.15s ease, color 0.15s ease;
}
.ci__qtyBtn--danger {
color: @wine;
}
.ci__qtyBtn:active {
transform: scale(0.9);
}
.ci__qtyValue {
min-width: 22px;
text-align: center;
font-size: 12.5px;
font-family: inherit;
font-size: 12px;
font-weight: 700;
color: @ink;
color: @wine;
background: @hairlineSoft;
}
.ci__remove:active {
transform: scale(0.96);
}
.ci__note {
@@ -35,7 +35,7 @@
</div>
<div class="oi__figures">
<span class="oi__figure">
<span v-if="value.quantity > 1" class="oi__figure">
{{ value.quantity }}
<i>عدد</i>
</span>
@@ -135,14 +135,30 @@ function wageLabel(v: number) {
display: block;
}
// The stand-in is the brand mark, not a photo, so it stays a mark in the
// middle of the frame instead of filling the slot the piece will occupy.
.pc__art img[lazy='loading'],
.pc__art img[lazy='error'] {
max-width: 62%;
max-height: 52%;
}
// Dark mode: product photos are shot on white, so they sit on a light mat
// instead of showing as white boxes on a dark card (@photoMat — transparent,
// i.e. no change, in light mode).
// i.e. no change, in light mode). The brand mark that stands in while a photo
// loads is black line art, so it stays on the card and is drawn light, the
// same way the header logo is.
html.theme-dark .pc__art img {
background: @photoMat;
border-radius: 12px;
}
html.theme-dark .pc__art img[lazy='loading'],
html.theme-dark .pc__art img[lazy='error'] {
background: transparent;
filter: invert(1);
}
.pc__artEmpty {
width: 100%;
height: 100%;
@@ -212,32 +212,7 @@
<!-- ── Action bar ────────────────────────────────────── -->
<div class="pd__bar">
<!-- The reference's left pill. When the piece is not yet in the cart
it deep-links to its category (a real destination); once it is, it
becomes the quantity stepper, which has to live somewhere and this
is the slot the layout already gives it. -->
<!-- Stepper order rule, the same one CartItem.vue follows: plus on
the visual right, minus (or trash at qty 1) on the visual left.
The page is RTL, so a plain flex row already lays its DOM-first
child on the right — plus has to be FIRST in markup, not last, to
land there. (FIXED: this used to be minus/trash, value, plus —
the reverse of the intended layout, which put plus on the visual
left instead of the right.) -->
<div v-if="inCart" class="pd__qty">
<button type="button" class="pd__qtyBtn" aria-label="افزایش" @click="increment">
<Icon icon="vuesax-linear:add" size="15" />
</button>
<span class="pd__qtyValue">{{ cartQty }}</span>
<button
type="button"
class="pd__qtyBtn"
:aria-label="cartQty === 1 ? 'حذف' : 'کاهش'"
@click="decrement"
>
<Icon :icon="cartQty === 1 ? 'vuesax-linear:trash' : 'vuesax-linear:minus'" size="15" />
</button>
</div>
<button v-else-if="categoryTitle" type="button" class="pd__pill" @click="goCategory">
<button v-if="categoryTitle" type="button" class="pd__pill" @click="goCategory">
<Icon icon="vuesax-linear:category" size="17" />
<span class="pd__pillText">{{ categoryTitle }}</span>
</button>
@@ -254,7 +229,7 @@
</button>
<router-link v-else class="pd__cta" :to="{ name: 'cart' }">
<Icon icon="vuesax-linear:bag-2" size="17" />
مشاهده سبد خرید
در سبد خرید
</router-link>
</div>
</article>
@@ -475,7 +450,7 @@ const wageLabel = computed(() => {
})
const stockLabel = computed(() => {
if (qty.value > 0) return `${qty.value} عدد موجود`
if (qty.value > 0) return 'موجود'
return isPreOrder.value ? 'پیش‌سفارش' : 'ناموجود'
})
const stockClass = computed(() => {
@@ -491,27 +466,7 @@ const storeProduct = computed<CartItem>(() => ({
qty: 1,
}))
// Quantity control reuses the store's updateFeild(...,'qty') path, the same one
// CartItem uses; the store has no dedicated increment/decrement.
const inCart = computed(() => cart.isInCart(storeProduct.value))
const cartIndex = computed(() => cart.find_index(storeProduct.value))
const cartQty = computed(() => (cartIndex.value > -1 ? cart.items[cartIndex.value].qty : 0))
// Stock ceiling: a pre-order variant may exceed what is on hand, anything else
// stops at its qty. The store clamps too — this just keeps the button honest.
const cartMaxQty = computed(() => cart.maxQty(storeProduct.value))
const canIncrement = computed(() => cartMaxQty.value === null || cartQty.value < cartMaxQty.value)
const increment = () => {
if (!canIncrement.value) return
cart.updateFeild(cartQty.value + 1, cartIndex.value, storeProduct.value, 'qty')
}
const decrement = () => {
if (cartQty.value <= 1) {
cart.remove(storeProduct.value)
return
}
cart.updateFeild(cartQty.value - 1, cartIndex.value, storeProduct.value, 'qty')
}
const isOrderable = (v: Parameters<typeof isVariantOrderable>[0]) => isVariantOrderable(v)
@@ -1295,40 +1250,6 @@ onBeforeUnmount(() => {
text-overflow: ellipsis;
}
.pd__qty {
flex-shrink: 0;
height: 48px;
padding: 0 6px;
display: inline-flex;
align-items: center;
gap: 4px;
border-radius: 999px;
border: 1px solid @hairline;
background: @canvas;
}
.pd__qtyBtn {
width: 32px;
height: 32px;
display: grid;
place-items: center;
border: 0;
border-radius: 50%;
cursor: pointer;
color: @ink;
background: @hairlineSoft;
transition: transform 0.18s ease;
}
.pd__qtyBtn:active {
transform: scale(0.9);
}
.pd__qtyValue {
min-width: 20px;
text-align: center;
font-size: 13px;
font-weight: 800;
color: @ink;
}
.pd__cta {
flex: 1;
min-width: 0;
+5 -6
View File
@@ -9,7 +9,7 @@ import { setUnauthorizedHandler } from './api/client';
import { useSessionStore } from './stores/session';
import { useAppearanceStore } from './stores/appearanceStore';
import Container from './components/ui/Container.vue';
import Vector from './assets/img/Vector.svg';
import brandLogo from './assets/img/brand-logo.svg';
// Vant defaults to zh-CN. Every view in this app is Persian (RTL), so any
// Vant component rendering its own built-in text (PullRefresh's
@@ -48,12 +48,11 @@ setUnauthorizedHandler(() => {
void router.replace({ name: 'login' });
}
});
// Ported from legacy main.ts:32-46 — Vector.svg is both the while-loading and
// the on-error placeholder, so a missing/failed image paints the brand mark
// instead of collapsing the layout.
// While a catalogue photo is still loading, and if it fails, the brand mark
// stands in the frame. Same file the header and the login screen use.
app.use(VueLazyLoad, {
loading: Vector,
error: Vector,
loading: brandLogo,
error: brandLogo,
});
app.component('container', Container);
app.mount('#app');
+10 -11
View File
@@ -6,7 +6,7 @@
* (cart never reserved stock; this list also never reserves).
* - Move here → cart: uses cart.addToCart stock guards; on failure the line
* stays here so an out-of-stock deferral is not lost.
* - Qty/note editable here without stock ceiling (desired qty for later).
* - One of each variant. A line is the piece itself; qty stays 1.
* - Survives daily cart expiry and successful order (active cart only clears).
* - Guest: localStorage; logged-in: debounced PUT /cart/buy-later.
* - Acting-as / logout: list is cleared in memory like the active cart.
@@ -26,8 +26,8 @@ function loadStored(): CartItem[] {
const raw = localStorage.getItem(STORAGE_KEY)
if (!raw) return []
const parsed = JSON.parse(raw)
if (Array.isArray(parsed)) return parsed
return Array.isArray(parsed?.items) ? parsed.items : []
if (Array.isArray(parsed)) return parsed.map((item) => ({ ...item, qty: 1 }))
return Array.isArray(parsed?.items) ? parsed.items.map((item: CartItem) => ({ ...item, qty: 1 })) : []
} catch {
return []
}
@@ -41,7 +41,7 @@ function toServerItems(items: CartItem[]) {
return items.map((i) => ({
pid: i.pid,
vid: i.vid,
qty: i.qty,
qty: 1,
note: i.note ?? null,
}))
}
@@ -115,7 +115,7 @@ export const useBuyLaterStore = defineStore('buyLater', {
next.push({
pid: line.pid,
vid: line.vid,
qty: Math.max(1, Math.floor(line.qty) || 1),
qty: 1,
note: line.note ?? null,
product: blob,
})
@@ -143,17 +143,16 @@ export const useBuyLaterStore = defineStore('buyLater', {
return this.items.findIndex((i) => i.pid === item.pid && i.vid === item.vid)
},
/** Upsert a line without any stock check. */
/** Upsert a line. A variant already here stays the one piece it is. */
upsert(item: CartItem) {
const qty = Math.max(1, Math.floor(item.qty) || 1)
const i = this.find_index(item)
if (i === -1) {
this.items.push({ ...item, qty })
this.items.push({ ...item, qty: 1 })
} else {
this.items[i] = {
...this.items[i],
product: item.product,
qty,
qty: 1,
note: item.note ?? this.items[i].note,
}
}
@@ -167,10 +166,10 @@ export const useBuyLaterStore = defineStore('buyLater', {
this.writeLocal()
},
updateQty(item: Pick<CartItem, 'pid' | 'vid'>, qty: number) {
updateQty(item: Pick<CartItem, 'pid' | 'vid'>, _qty: number) {
const i = this.find_index(item)
if (i === -1) return
this.items[i].qty = Math.max(1, Math.floor(qty) || 1)
this.items[i].qty = 1
this.writeLocal()
},
+15 -34
View File
@@ -33,10 +33,10 @@ function loadStored(): StoredCart {
const parsed = JSON.parse(raw)
// Legacy shape was a bare array.
if (Array.isArray(parsed)) {
return { items: parsed, touchedAt: null }
return { items: oneEach(parsed), touchedAt: null }
}
return {
items: Array.isArray(parsed?.items) ? parsed.items : [],
items: oneEach(Array.isArray(parsed?.items) ? parsed.items : []),
touchedAt: typeof parsed?.touchedAt === 'string' ? parsed.touchedAt : null,
}
} catch {
@@ -48,11 +48,16 @@ function persist(items: CartItem[], touchedAt: string | null) {
localStorage.setItem(STORAGE_KEY, JSON.stringify({ items, touchedAt }))
}
/** A variant is one physical piece, so a line never carries a count above one. */
function oneEach(items: CartItem[]): CartItem[] {
return items.map((item) => ({ ...item, qty: 1 }))
}
function toServerItems(items: CartItem[]) {
return items.map((i) => ({
pid: i.pid,
vid: i.vid,
qty: i.qty,
qty: 1,
note: i.note ?? null,
}))
}
@@ -172,7 +177,7 @@ export const useCartStore = defineStore('cart', {
next.push({
pid: line.pid,
vid: line.vid,
qty: line.qty,
qty: 1,
note: line.note ?? null,
product,
})
@@ -207,35 +212,16 @@ export const useCartStore = defineStore('cart', {
if (!((target.qty ?? 0) > 0 || target.isPreOrder)) return false
const index = this.find_index(data)
if (index === -1) this.items.push({ ...data, qty: this.capQty(data.qty, target) })
if (index === -1) this.items.push({ ...data, qty: 1 })
else {
// Already in the basket. Refresh the product blob and leave the line
// as the one piece it is — a second add must not become two.
this.items[index].product = data.product
this.items[index].qty = this.capQty(data.qty, target)
this.items[index].qty = 1
}
this.writeLocal(true)
return true
},
/**
* Stock ceiling for a cart line. A pre-order variant is orderable beyond
* what is on hand (that is the point of pre-ordering), so it is uncapped;
* anything else stops at its on-hand quantity.
*/
capQty(want: number, variant: { qty?: number; isPreOrder?: boolean }) {
const n = Math.max(1, Math.floor(want) || 1)
if (variant.isPreOrder) return n
const max = variant.qty ?? 0
return max > 0 ? Math.min(n, max) : n
},
/** The ceiling for an item already in the cart; null when uncapped. */
maxQty(item: Pick<CartItem, 'pid' | 'vid'>) {
const i = this.find_index(item)
if (i === -1) return null
const v = this.variant(this.items[i])
if (!v || v.isPreOrder) return null
return (v.qty ?? 0) > 0 ? (v.qty as number) : null
},
remove(item: CartItem) {
const index = this.find_index(item)
if (index > -1) this.items.splice(index, 1)
@@ -246,13 +232,8 @@ export const useCartStore = defineStore('cart', {
updateFeild(value: unknown, index: number | null, item: Pick<CartItem, 'pid' | 'vid'>, field: keyof CartItem) {
if (!this.isInCart(item)) return false
const i = index ?? this.find_index(item)
// A quantity change is clamped to what is actually in stock (unless the
// variant is pre-orderable), so the ceiling holds no matter which screen
// drives the stepper.
if (field === 'qty') {
const v = this.variant(this.items[i])
value = v ? this.capQty(Number(value), v) : Math.max(1, Number(value) || 1)
}
// A line is one piece. Notes still update; a quantity write cannot.
if (field === 'qty') value = 1
;(this.items[i] as any)[field] = value
this.writeLocal(true)
return true
+1 -1
View File
@@ -342,7 +342,7 @@ const submitOrder = async () => {
const variant = cart.variant(item)
return {
productVariantId: item.vid,
quantity: item.qty,
quantity: 1,
// Sent for older API builds only: the server prices app orders itself
// (MON-08) and ignores this figure.
wagePercent: variant ? parseFloat(variant.defaultWagePercent) : 0,