@stacksjs/defaults 0.75.618 → 0.75.622
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/ai/skills/stacks-actions/SKILL.md +1 -1
- package/ai/skills/stacks-auth/SKILL.md +12 -1
- package/ai/skills/stacks-auto-imports/SKILL.md +1 -1
- package/ai/skills/stacks-buddy/SKILL.md +70 -7
- package/ai/skills/stacks-commerce/SKILL.md +78 -4
- package/ai/skills/stacks-dashboard/SKILL.md +32 -2
- package/ai/skills/stacks-database/SKILL.md +1 -1
- package/ai/skills/stacks-development/SKILL.md +3 -0
- package/ai/skills/stacks-migrations/SKILL.md +10 -2
- package/ai/skills/stacks-mobile/SKILL.md +21 -2
- package/ai/skills/stacks-models/SKILL.md +2 -2
- package/ai/skills/stacks-native/CATALOG.md +21 -21
- package/ai/skills/stacks-orm/SKILL.md +2 -2
- package/ai/skills/stacks-utils/SKILL.md +8 -3
- package/app/Actions/Dashboard/Commerce/CommerceDiscountDestroyAction.ts +27 -0
- package/app/Actions/Dashboard/Commerce/CommerceDiscountStoreAction.ts +28 -0
- package/app/Actions/Dashboard/Commerce/CommerceDiscountUpdateAction.ts +30 -0
- package/app/Actions/Dashboard/Commerce/CommerceDiscountsAction.ts +42 -0
- package/app/Actions/Dashboard/Commerce/CommerceUpsellDestroyAction.ts +31 -0
- package/app/Actions/Dashboard/Commerce/CommerceUpsellDetailAction.ts +28 -0
- package/app/Actions/Dashboard/Commerce/CommerceUpsellStoreAction.ts +26 -0
- package/app/Actions/Dashboard/Commerce/CommerceUpsellUpdateAction.ts +29 -0
- package/app/Actions/Dashboard/Commerce/CommerceUpsellsAction.ts +40 -0
- package/app/Actions/Dashboard/Commerce/commerce-discount-list.ts +204 -0
- package/app/Actions/Dashboard/Commerce/commerce-discount-write.ts +53 -0
- package/app/Actions/Dashboard/Commerce/commerce-discounts-upsells.test.ts +228 -0
- package/app/Actions/Dashboard/Commerce/commerce-upsell-list.ts +289 -0
- package/app/Actions/Dashboard/Commerce/discount-records.ts +392 -0
- package/app/Actions/Dashboard/Commerce/upsell-offer-records.ts +307 -0
- package/app/Actions/Dashboard/Models/ModelSaveAction.ts +6 -1
- package/app/Actions/Dashboard/Models/model-relations.ts +17 -7
- package/app/Actions/Dashboard/Models/model-row-writes.ts +9 -8
- package/app/Actions/Dashboard/Models/model-write.ts +51 -2
- package/app/Actions/Dashboard/Remote/RemoteCommandRunAction.ts +1 -2
- package/app/Actions/Dashboard/Remote/RemoteTerminalCloseAction.ts +1 -2
- package/app/Actions/Dashboard/Remote/RemoteTerminalInputAction.ts +1 -2
- package/app/Actions/Dashboard/Remote/RemoteTerminalOpenAction.ts +1 -2
- package/app/Actions/Dashboard/Remote/RemoteTerminalResizeAction.ts +1 -2
- package/app/Actions/Dashboard/Remote/RemoteTerminalStreamAction.ts +1 -2
- package/app/Actions/Dashboard/Remote/remote-commands.test.ts +2 -2
- package/app/Actions/Dashboard/Remote/remote-commands.ts +4 -4
- package/app/Actions/Dashboard/Remote/remote-terminal.test.ts +45 -25
- package/app/Actions/Dashboard/Remote/remote-terminal.ts +9 -9
- package/app/Actions/Storefront/AcceptOrderOfferAction.ts +68 -0
- package/app/Actions/Storefront/ApplyDiscountCodeAction.ts +83 -0
- package/app/Actions/Storefront/DeclineOrderOfferAction.ts +25 -0
- package/app/Actions/Storefront/PlaceOrderAction.ts +135 -72
- package/app/Gates.ts +4 -4
- package/app/Mail/OrderConfirmation.ts +7 -1
- package/app/Models/commerce/Cart.ts +14 -0
- package/app/Models/commerce/Discount.ts +255 -0
- package/app/Models/commerce/PostPurchaseOffer.ts +175 -0
- package/app/Models/commerce/PostPurchaseOfferResponse.ts +145 -0
- package/app/Policies/PostPolicy.ts +11 -11
- package/app/Storefront/DiscountCodeMessages.ts +12 -0
- package/app/Storefront/OrderKey.ts +29 -0
- package/app/Storefront/StorefrontPricing.ts +211 -0
- package/app/Storefront/UpsellCharge.ts +43 -0
- package/functions/projects.test.ts +38 -0
- package/functions/projects.ts +99 -0
- package/ide/vscode/package.json +1 -1
- package/package.json +7 -7
- package/resources/components/Dashboard/Commerce/CommerceDiscountBreakdown.stx +72 -0
- package/resources/components/Dashboard/Commerce/CommerceDiscountDeleteDialog.stx +19 -0
- package/resources/components/Dashboard/Commerce/CommerceDiscountDetailsDialog.stx +110 -0
- package/resources/components/Dashboard/Commerce/CommerceDiscountDialog.stx +353 -0
- package/resources/components/Dashboard/Commerce/CommerceDiscountsDashboard.stx +211 -0
- package/resources/components/Dashboard/Commerce/CommerceDiscountsTable.stx +105 -0
- package/resources/components/Dashboard/Commerce/CommerceProductPicker.stx +115 -0
- package/resources/components/Dashboard/Commerce/CommerceUpsellDeleteDialog.stx +21 -0
- package/resources/components/Dashboard/Commerce/CommerceUpsellDetailsDialog.stx +153 -0
- package/resources/components/Dashboard/Commerce/CommerceUpsellDialog.stx +191 -0
- package/resources/components/Dashboard/Commerce/CommerceUpsellsDashboard.stx +214 -0
- package/resources/components/Dashboard/Commerce/CommerceUpsellsTable.stx +91 -0
- package/resources/components/Dashboard/Email/InboxSettingsDashboard.stx +4 -19
- package/resources/components/Dashboard/ProjectSwitcher.stx +302 -0
- package/resources/components/Dashboard/Settings/SettingsDashboard.stx +5 -3
- package/resources/components/Dashboard/UI/SearchFilter.stx +7 -4
- package/resources/components/Storefront/OrderSummary.stx +17 -0
- package/resources/functions/dashboard/project-switcher.test.ts +14 -0
- package/resources/functions/dashboard/project-switcher.ts +28 -0
- package/resources/functions/dashboard/sidebar.ts +4 -1
- package/resources/plugins/preloader.ts +33 -2
- package/resources/views/cart.stx +50 -16
- package/resources/views/checkout/contact.stx +11 -5
- package/resources/views/checkout/payment.stx +11 -5
- package/resources/views/checkout/shipping.stx +11 -5
- package/resources/views/orders/[id].stx +227 -50
- package/routes/dashboard-api.ts +12 -0
- package/routes/dashboard.ts +2 -0
- package/routes/storefront.ts +8 -0
- package/views/dashboard/commerce/discounts/index.stx +10 -0
- package/views/dashboard/commerce/upsells/index.stx +10 -0
- package/views/dashboard/layouts/default.stx +17 -0
|
@@ -0,0 +1,191 @@
|
|
|
1
|
+
<script client>
|
|
2
|
+
import type { UpsellOfferRecord } from '../../../../app/Actions/Dashboard/Commerce/upsell-offer-records'
|
|
3
|
+
import { minorToInput, moneyInputError, moneyInputStep, parseMoneyInput } from '@stacksjs/commerce/money'
|
|
4
|
+
import { formErrorMessages } from '../../../../functions/dashboard-form'
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Add or edit a post-purchase offer: what the confirmation page says, which
|
|
8
|
+
* products it offers and at what discount, which orders see it and for how
|
|
9
|
+
* long. A preview shows the banner the shopper will read.
|
|
10
|
+
*/
|
|
11
|
+
interface PickedProduct { id: string, label: string }
|
|
12
|
+
|
|
13
|
+
const open = useReactiveProp('open', false)
|
|
14
|
+
const mode = useReactiveProp('mode', 'add')
|
|
15
|
+
const record = useReactiveProp('record', null as UpsellOfferRecord | null)
|
|
16
|
+
const currency = useReactiveProp('currency', 'USD')
|
|
17
|
+
const busy = useReactiveProp('busy', false)
|
|
18
|
+
const errors = useReactiveProp('errors', {} as Record<string, string>)
|
|
19
|
+
const emit = defineEmits(['submit', 'close'])
|
|
20
|
+
|
|
21
|
+
const offerName = state('')
|
|
22
|
+
const headlineText = state('')
|
|
23
|
+
const messageText = state('')
|
|
24
|
+
const products = state<PickedProduct[]>([])
|
|
25
|
+
const discountPercent = state('60')
|
|
26
|
+
const minQuantity = state('1')
|
|
27
|
+
const defaultQuantity = state('1')
|
|
28
|
+
const maxQuantity = state('')
|
|
29
|
+
const triggers = state<PickedProduct[]>([])
|
|
30
|
+
const minOrderTotal = state('')
|
|
31
|
+
const windowMinutes = state('30')
|
|
32
|
+
const priority = state('0')
|
|
33
|
+
const startsAt = state('')
|
|
34
|
+
const endsAt = state('')
|
|
35
|
+
const isActive = state(true)
|
|
36
|
+
let hydrationKey = ''
|
|
37
|
+
|
|
38
|
+
effect(() => {
|
|
39
|
+
if (!open()) {
|
|
40
|
+
hydrationKey = ''
|
|
41
|
+
return
|
|
42
|
+
}
|
|
43
|
+
const current = record()
|
|
44
|
+
const key = `${mode()}:${current?.id || 'new'}`
|
|
45
|
+
if (hydrationKey === key)
|
|
46
|
+
return
|
|
47
|
+
hydrationKey = key
|
|
48
|
+
offerName.set(current?.name || '')
|
|
49
|
+
headlineText.set(current?.headline || '')
|
|
50
|
+
messageText.set(current?.message || '')
|
|
51
|
+
products.set((current?.products ?? []).map(p => ({ id: p.id, label: p.label })))
|
|
52
|
+
discountPercent.set(String(current?.discountPercent ?? 60))
|
|
53
|
+
minQuantity.set(String(current?.minQuantity ?? 1))
|
|
54
|
+
defaultQuantity.set(String(current?.defaultQuantity ?? 1))
|
|
55
|
+
maxQuantity.set(current?.maxQuantity ? String(current.maxQuantity) : '')
|
|
56
|
+
triggers.set((current?.triggerProducts ?? []).map(p => ({ id: p.id, label: p.label })))
|
|
57
|
+
minOrderTotal.set(current?.minOrderTotal != null ? minorToInput(current.minOrderTotal, currency()) : '')
|
|
58
|
+
windowMinutes.set(String(current?.windowMinutes ?? 30))
|
|
59
|
+
priority.set(String(current?.priority ?? 0))
|
|
60
|
+
startsAt.set(current?.startsAt || '')
|
|
61
|
+
endsAt.set(current?.endsAt || '')
|
|
62
|
+
isActive.set(current?.isActive ?? true)
|
|
63
|
+
})
|
|
64
|
+
|
|
65
|
+
function setProducts(list: PickedProduct[]): void {
|
|
66
|
+
products.set(list)
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
function setTriggers(list: PickedProduct[]): void {
|
|
70
|
+
triggers.set(list)
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
function whole(text: string): number | undefined {
|
|
74
|
+
if (!text.trim())
|
|
75
|
+
return undefined
|
|
76
|
+
const n = Number(text)
|
|
77
|
+
return Number.isFinite(n) ? n : undefined
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
function quantitiesValid(): boolean {
|
|
81
|
+
const min = Number(minQuantity())
|
|
82
|
+
const start = Number(defaultQuantity())
|
|
83
|
+
const max = maxQuantity().trim() ? Number(maxQuantity()) : null
|
|
84
|
+
return Number.isInteger(min) && min >= 1
|
|
85
|
+
&& Number.isInteger(start) && start >= min
|
|
86
|
+
&& (max === null || (Number.isInteger(max) && max >= min && start <= max))
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
function valid(): boolean {
|
|
90
|
+
const percent = Number(discountPercent())
|
|
91
|
+
const window = Number(windowMinutes())
|
|
92
|
+
return Boolean(offerName().trim())
|
|
93
|
+
&& products().length > 0
|
|
94
|
+
&& Number.isInteger(percent) && percent >= 0 && percent <= 100
|
|
95
|
+
&& quantitiesValid()
|
|
96
|
+
&& Number.isInteger(window) && window >= 1
|
|
97
|
+
&& !moneyInputError(minOrderTotal(), currency(), { required: false })
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
/** The banner as the confirmation page will show it. */
|
|
101
|
+
function previewHeadline(): string {
|
|
102
|
+
return headlineText().trim() || 'One-time offer'
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
function previewMessage(): string {
|
|
106
|
+
return messageText().trim() || `Add ${minQuantity()}+ and get ${discountPercent() || 0}% off, only on this page.`
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
function submit(): void {
|
|
110
|
+
if (!valid())
|
|
111
|
+
return
|
|
112
|
+
emit('submit', {
|
|
113
|
+
name: offerName().trim(),
|
|
114
|
+
headline: headlineText().trim(),
|
|
115
|
+
message: messageText().trim(),
|
|
116
|
+
productIds: products().map(p => p.id),
|
|
117
|
+
discountPercent: Number(discountPercent()),
|
|
118
|
+
minQuantity: Number(minQuantity()),
|
|
119
|
+
defaultQuantity: Number(defaultQuantity()),
|
|
120
|
+
maxQuantity: whole(maxQuantity()),
|
|
121
|
+
triggerProductIds: triggers().map(p => p.id),
|
|
122
|
+
minOrderTotal: parseMoneyInput(minOrderTotal(), currency(), { required: false }) ?? undefined,
|
|
123
|
+
windowMinutes: Number(windowMinutes()),
|
|
124
|
+
priority: whole(priority()) ?? 0,
|
|
125
|
+
startsAt: startsAt() || undefined,
|
|
126
|
+
endsAt: endsAt() || undefined,
|
|
127
|
+
isActive: isActive(),
|
|
128
|
+
})
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
function errorMessages(): string[] {
|
|
132
|
+
return formErrorMessages(errors())
|
|
133
|
+
}
|
|
134
|
+
</script>
|
|
135
|
+
|
|
136
|
+
<Modal
|
|
137
|
+
:isOpen="open()"
|
|
138
|
+
:title="mode() === 'edit' ? 'Edit post-purchase offer' : 'Add post-purchase offer'"
|
|
139
|
+
description="Shown once on the order confirmation page. Accepting adds the items to the order just placed, charged to the same payment method."
|
|
140
|
+
size="xl"
|
|
141
|
+
@close="emit('close')"
|
|
142
|
+
>
|
|
143
|
+
<form id="upsell-form" class="flex flex-col gap-6" @submit.prevent="submit">
|
|
144
|
+
<ul :if="errorMessages().length > 0" class="p-3 space-y-1 text-red-700 text-sm dark:text-red-300 bg-red-50 dark:bg-red-950/30 border border-red-200 rounded-lg dark:border-red-900">
|
|
145
|
+
<li :for="text in errorMessages()" :key="text">{{ text }}</li>
|
|
146
|
+
</ul>
|
|
147
|
+
|
|
148
|
+
<section class="grid gap-4 grid-cols-1 sm:grid-cols-2" aria-label="What the shopper sees">
|
|
149
|
+
<div class="sm:col-span-2"><Input v-model:value="offerName" label="Name" hint="For you: how this offer is listed here" :maxLength="255" :required="true" /></div>
|
|
150
|
+
<Input v-model:value="headlineText" label="Headline" placeholder="One-time offer" :maxLength="255" />
|
|
151
|
+
<div class="sm:row-span-2"><Textarea v-model:value="messageText" label="Message" :rows="4" placeholder="Thank you for your purchase! Grab 5+ pairs and get 60% off." /></div>
|
|
152
|
+
<div class="flex gap-3 items-start p-4 bg-gray-50 dark:bg-neutral-900/50 border border-gray-200 rounded-lg dark:border-neutral-700" aria-label="Preview">
|
|
153
|
+
<span class="flex-shrink-0 mt-0.5 h-5 w-5 text-gray-600 dark:text-neutral-300 i-hugeicons-gift"></span>
|
|
154
|
+
<div class="min-w-0">
|
|
155
|
+
<p class="font-semibold text-gray-900 text-sm dark:text-white">{{ previewHeadline() }}</p>
|
|
156
|
+
<p class="mt-1 text-gray-600 text-sm dark:text-neutral-300">{{ previewMessage() }}</p>
|
|
157
|
+
</div>
|
|
158
|
+
</div>
|
|
159
|
+
</section>
|
|
160
|
+
|
|
161
|
+
<section class="flex flex-col gap-4" aria-label="The offer">
|
|
162
|
+
<CommerceProductPicker label="Products offered" :selected="products()" :max="12" emptyLabel="Choose the products to offer, in the order they should appear" :error="errors().productIds || ''" @change="setProducts" />
|
|
163
|
+
<div class="grid gap-4 grid-cols-2 sm:grid-cols-4">
|
|
164
|
+
<Input v-model:value="discountPercent" label="Percent off" type="number" min="0" max="100" step="1" :required="true" />
|
|
165
|
+
<Input v-model:value="minQuantity" label="Minimum quantity" type="number" min="1" step="1" :required="true" />
|
|
166
|
+
<Input v-model:value="defaultQuantity" label="Default quantity" type="number" min="1" step="1" :required="true" />
|
|
167
|
+
<Input v-model:value="maxQuantity" label="Maximum quantity" type="number" min="1" step="1" placeholder="No limit" />
|
|
168
|
+
</div>
|
|
169
|
+
<p :if="!quantitiesValid()" class="text-amber-700 text-xs dark:text-amber-300">The default quantity has to sit between the minimum and the maximum.</p>
|
|
170
|
+
</section>
|
|
171
|
+
|
|
172
|
+
<section class="flex flex-col gap-4" aria-label="Who sees it">
|
|
173
|
+
<p class="font-semibold text-gray-900 text-sm dark:text-white">Who sees it</p>
|
|
174
|
+
<CommerceProductPicker label="Only orders containing" :selected="triggers()" emptyLabel="Every order" hint="Leave empty to show the offer after any order." @change="setTriggers" />
|
|
175
|
+
<div class="grid gap-4 grid-cols-1 sm:grid-cols-3">
|
|
176
|
+
<Input v-model:value="minOrderTotal" label="Minimum order total" type="number" min="0" :step="moneyInputStep(currency())" placeholder="Any total" />
|
|
177
|
+
<Input v-model:value="windowMinutes" label="Open for (minutes)" type="number" min="1" max="10080" step="1" hint="After the order is placed" :required="true" />
|
|
178
|
+
<Input v-model:value="priority" label="Priority" type="number" step="1" hint="Higher shows first" />
|
|
179
|
+
<Input v-model:value="startsAt" label="Starts" type="date" />
|
|
180
|
+
<Input v-model:value="endsAt" label="Ends" type="date" />
|
|
181
|
+
</div>
|
|
182
|
+
</section>
|
|
183
|
+
|
|
184
|
+
<div class="p-4 bg-gray-50 dark:bg-neutral-900/50 rounded-lg">
|
|
185
|
+
<Toggle v-model:checked="isActive" label="Running" description="Switched off, the offer is kept with its results but shown to nobody." labelPosition="left" />
|
|
186
|
+
</div>
|
|
187
|
+
</form>
|
|
188
|
+
<template #footer>
|
|
189
|
+
<div class="flex gap-2 justify-end w-full"><Button variant="secondary" :disabled="busy()" @click="emit('close')">Cancel</Button><Button type="submit" form="upsell-form" :loading="busy()" :disabled="!valid()">{{ mode() === 'edit' ? 'Save offer' : 'Add offer' }}</Button></div>
|
|
190
|
+
</template>
|
|
191
|
+
</Modal>
|
|
@@ -0,0 +1,214 @@
|
|
|
1
|
+
<script client>
|
|
2
|
+
import type { UpsellOfferRecord, UpsellSummary } from '../../../../app/Actions/Dashboard/Commerce/upsell-offer-records'
|
|
3
|
+
import { formatCurrency } from '@stacksjs/commerce/money'
|
|
4
|
+
import { DashboardApiError, dashboardApi } from '../../../../functions/dashboard-api'
|
|
5
|
+
import { useServerList } from '../../../../functions/dashboard-list'
|
|
6
|
+
import { pushToast } from '../../../../functions/toasts'
|
|
7
|
+
|
|
8
|
+
interface UpsellIndexResponse {
|
|
9
|
+
records: UpsellOfferRecord[]
|
|
10
|
+
total: number
|
|
11
|
+
page: number
|
|
12
|
+
summary: UpsellSummary
|
|
13
|
+
currency: string
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
const emptySummary: UpsellSummary = { total: 0, running: 0, views: 0, accepted: 0, declined: 0, conversionRate: 0, revenue: 0, units: 0, top: [] }
|
|
17
|
+
const summary = state<UpsellSummary>({ ...emptySummary })
|
|
18
|
+
const currency = state('USD')
|
|
19
|
+
const stateFilter = state('all')
|
|
20
|
+
const sort = state('priority')
|
|
21
|
+
const dialogOpen = state(false)
|
|
22
|
+
const dialogMode = state<'add' | 'edit'>('add')
|
|
23
|
+
const editingRecord = state<UpsellOfferRecord | null>(null)
|
|
24
|
+
const detailsRecord = state<UpsellOfferRecord | null>(null)
|
|
25
|
+
const deletingRecord = state<UpsellOfferRecord | null>(null)
|
|
26
|
+
const saving = state(false)
|
|
27
|
+
const deleting = state(false)
|
|
28
|
+
const validationErrors = state<Record<string, string>>({})
|
|
29
|
+
|
|
30
|
+
const SELECT_SORTS: Record<string, { sort: string, direction: 'asc' | 'desc' }> = {
|
|
31
|
+
priority: { sort: 'priority', direction: 'desc' },
|
|
32
|
+
revenue: { sort: 'revenue', direction: 'desc' },
|
|
33
|
+
newest: { sort: 'newest', direction: 'desc' },
|
|
34
|
+
name: { sort: 'name', direction: 'asc' },
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
const {
|
|
38
|
+
records,
|
|
39
|
+
total,
|
|
40
|
+
page,
|
|
41
|
+
perPage,
|
|
42
|
+
search,
|
|
43
|
+
loading,
|
|
44
|
+
loadError,
|
|
45
|
+
load,
|
|
46
|
+
changePage,
|
|
47
|
+
changePageSize,
|
|
48
|
+
changeSort,
|
|
49
|
+
} = useServerList<UpsellOfferRecord>({
|
|
50
|
+
fetch: query => dashboardApi<UpsellIndexResponse>(`/api/dashboard/commerce/upsells?${query}`),
|
|
51
|
+
defaultSort: () => SELECT_SORTS[sort()] ?? SELECT_SORTS.priority!,
|
|
52
|
+
columnSorts: { name: 'name' },
|
|
53
|
+
filters: () => ({ state: stateFilter() }),
|
|
54
|
+
onLoad: (result) => {
|
|
55
|
+
const data = result as UpsellIndexResponse
|
|
56
|
+
summary.set(data.summary ?? { ...emptySummary })
|
|
57
|
+
currency.set(data.currency || 'USD')
|
|
58
|
+
},
|
|
59
|
+
onError: error => pushToast('error', 'Could not load offers', { detail: error instanceof Error ? error.message : String(error) }),
|
|
60
|
+
})
|
|
61
|
+
|
|
62
|
+
function money(value: number): string {
|
|
63
|
+
return formatCurrency(value, currency())
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
function percent(rate: number): string {
|
|
67
|
+
return `${(rate * 100).toFixed(rate > 0 && rate < 0.1 ? 1 : 0)}%`
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
function metricCards(): Array<{ label: string, value: string, detail: string }> {
|
|
71
|
+
const current = summary()
|
|
72
|
+
return [
|
|
73
|
+
{ label: 'Running', value: String(current.running), detail: `of ${current.total} offers` },
|
|
74
|
+
{ label: 'Shown', value: current.views.toLocaleString(), detail: 'Products shown after checkout' },
|
|
75
|
+
{ label: 'Conversion', value: percent(current.conversionRate), detail: `${current.accepted.toLocaleString()} added, ${current.declined.toLocaleString()} declined` },
|
|
76
|
+
{ label: 'Added to orders', value: money(current.revenue), detail: `${current.units.toLocaleString()} units` },
|
|
77
|
+
]
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
function matchingLabel(): string {
|
|
81
|
+
const count = total()
|
|
82
|
+
return `${count.toLocaleString()} matching ${count === 1 ? 'offer' : 'offers'}`
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
function openAdd(): void {
|
|
86
|
+
dialogMode.set('add')
|
|
87
|
+
editingRecord.set(null)
|
|
88
|
+
validationErrors.set({})
|
|
89
|
+
dialogOpen.set(true)
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
function openEdit(record: UpsellOfferRecord): void {
|
|
93
|
+
dialogMode.set('edit')
|
|
94
|
+
editingRecord.set(record)
|
|
95
|
+
validationErrors.set({})
|
|
96
|
+
dialogOpen.set(true)
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
function closeDialog(): void {
|
|
100
|
+
if (saving())
|
|
101
|
+
return
|
|
102
|
+
dialogOpen.set(false)
|
|
103
|
+
editingRecord.set(null)
|
|
104
|
+
validationErrors.set({})
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
async function saveOffer(payload: Record<string, unknown>): Promise<void> {
|
|
108
|
+
const current = editingRecord()
|
|
109
|
+
const editing = dialogMode() === 'edit' && current !== null
|
|
110
|
+
saving.set(true)
|
|
111
|
+
validationErrors.set({})
|
|
112
|
+
try {
|
|
113
|
+
await dashboardApi(editing ? `/api/dashboard/commerce/upsells/${current.id}` : '/api/dashboard/commerce/upsells', {
|
|
114
|
+
method: editing ? 'PATCH' : 'POST',
|
|
115
|
+
body: payload,
|
|
116
|
+
})
|
|
117
|
+
dialogOpen.set(false)
|
|
118
|
+
editingRecord.set(null)
|
|
119
|
+
pushToast('success', editing ? 'Offer updated' : 'Offer added', { detail: String(payload.name ?? '') })
|
|
120
|
+
await load()
|
|
121
|
+
}
|
|
122
|
+
catch (error) {
|
|
123
|
+
if (error instanceof DashboardApiError)
|
|
124
|
+
validationErrors.set(error.fields ?? {})
|
|
125
|
+
pushToast('error', 'Could not save offer', { detail: error instanceof Error ? error.message : String(error) })
|
|
126
|
+
}
|
|
127
|
+
finally {
|
|
128
|
+
saving.set(false)
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
function openDetails(record: UpsellOfferRecord): void {
|
|
133
|
+
detailsRecord.set(record)
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
function closeDetails(): void {
|
|
137
|
+
detailsRecord.set(null)
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
function openDelete(record: UpsellOfferRecord): void {
|
|
141
|
+
deletingRecord.set(record)
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
function closeDelete(): void {
|
|
145
|
+
if (!deleting())
|
|
146
|
+
deletingRecord.set(null)
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
async function deleteOffer(): Promise<void> {
|
|
150
|
+
const current = deletingRecord()
|
|
151
|
+
if (!current)
|
|
152
|
+
return
|
|
153
|
+
deleting.set(true)
|
|
154
|
+
try {
|
|
155
|
+
await dashboardApi(`/api/dashboard/commerce/upsells/${current.id}`, { method: 'DELETE' })
|
|
156
|
+
deletingRecord.set(null)
|
|
157
|
+
detailsRecord.set(null)
|
|
158
|
+
pushToast('success', 'Offer deleted', { detail: current.name })
|
|
159
|
+
await load()
|
|
160
|
+
}
|
|
161
|
+
catch (error) {
|
|
162
|
+
pushToast('error', 'Could not delete offer', { detail: error instanceof Error ? error.message : String(error) })
|
|
163
|
+
}
|
|
164
|
+
finally {
|
|
165
|
+
deleting.set(false)
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
onMount(() => {
|
|
170
|
+
void load()
|
|
171
|
+
})
|
|
172
|
+
</script>
|
|
173
|
+
|
|
174
|
+
<div class="flex flex-col gap-6">
|
|
175
|
+
<PageHeader title="Upsells" description="One-time, discounted offers on the order confirmation page. Accepting adds the items to the order just placed, with no second checkout.">
|
|
176
|
+
<div class="flex flex-wrap gap-2">
|
|
177
|
+
<Button variant="secondary" :loading="loading()" @click="load()"><span :if="!loading()" class="h-4 w-4 i-hugeicons-refresh"></span>Refresh</Button>
|
|
178
|
+
<Button @click="openAdd()"><span class="h-4 w-4 i-hugeicons-add-01"></span>Add offer</Button>
|
|
179
|
+
</div>
|
|
180
|
+
</PageHeader>
|
|
181
|
+
|
|
182
|
+
<div :if="loadError()" class="p-4 text-red-700 text-sm dark:text-red-300 bg-red-50 dark:bg-red-950/30 border border-red-200 rounded-lg dark:border-red-900">{{ loadError() }}</div>
|
|
183
|
+
|
|
184
|
+
<MetricCards label="Upsell metrics" columns="grid-cols-2 lg:grid-cols-4" :items="metricCards()" />
|
|
185
|
+
|
|
186
|
+
<section :if="summary().top.length > 0" aria-label="Best offers" class="p-4 bg-white dark:bg-neutral-800 border border-gray-200 rounded-lg dark:border-neutral-700">
|
|
187
|
+
<h2 class="font-semibold text-gray-900 text-sm dark:text-white">Best offers</h2>
|
|
188
|
+
<ol class="grid gap-3 grid-cols-1 sm:grid-cols-2 lg:grid-cols-5 mt-3">
|
|
189
|
+
<li :for="(entry, index) in summary().top" :key="entry.id" class="flex gap-3 items-baseline">
|
|
190
|
+
<span class="font-mono text-gray-400 text-xs">{{ index + 1 }}</span>
|
|
191
|
+
<div class="min-w-0">
|
|
192
|
+
<p class="text-gray-900 text-sm truncate dark:text-white">{{ entry.name }}</p>
|
|
193
|
+
<p class="tabular-nums text-gray-500 text-xs dark:text-neutral-400">{{ money(entry.revenue) }} · {{ percent(entry.conversionRate) }}</p>
|
|
194
|
+
</div>
|
|
195
|
+
</li>
|
|
196
|
+
</ol>
|
|
197
|
+
</section>
|
|
198
|
+
|
|
199
|
+
<section class="flex flex-col gap-3">
|
|
200
|
+
<div class="grid gap-3 grid-cols-1 sm:grid-cols-2 xl:grid-cols-4">
|
|
201
|
+
<div class="sm:col-span-2"><SearchFilter v-model:searchValue="search" placeholder="Search names, headlines or messages" ariaLabel="Search offers" /></div>
|
|
202
|
+
<Select v-model:value="stateFilter" ariaLabel="Offer status" placeholder="" :options="[{ value: 'all', label: 'Any status' }, { value: 'running', label: 'Running' }, { value: 'scheduled', label: 'Scheduled' }, { value: 'ended', label: 'Ended' }, { value: 'disabled', label: 'Disabled' }]" />
|
|
203
|
+
<Select v-model:value="sort" ariaLabel="Sort offers" placeholder="" :options="[{ value: 'priority', label: 'Priority' }, { value: 'revenue', label: 'Most revenue' }, { value: 'newest', label: 'Newest' }, { value: 'name', label: 'Name' }]" />
|
|
204
|
+
</div>
|
|
205
|
+
|
|
206
|
+
<p class="text-gray-500 text-sm dark:text-neutral-400">{{ matchingLabel() }}</p>
|
|
207
|
+
|
|
208
|
+
<CommerceUpsellsTable :sort-reset-key="sort()" :records="records()" :total="total()" :current-page="page()" :per-page="perPage()" :loading="loading()" :currency="currency()" @view="openDetails" @edit="openEdit" @delete="openDelete" @page-change="changePage" @page-size-change="changePageSize" @sort="changeSort" />
|
|
209
|
+
</section>
|
|
210
|
+
</div>
|
|
211
|
+
|
|
212
|
+
<CommerceUpsellDialog :open="dialogOpen()" :mode="dialogMode()" :record="editingRecord()" :currency="currency()" :busy="saving()" :errors="validationErrors()" @submit="saveOffer" @close="closeDialog()" />
|
|
213
|
+
<CommerceUpsellDetailsDialog :open="Boolean(detailsRecord())" :record="detailsRecord()" :currency="currency()" @edit="openEdit" @delete="openDelete" @close="closeDetails()" />
|
|
214
|
+
<CommerceUpsellDeleteDialog :open="Boolean(deletingRecord())" :record="deletingRecord()" :busy="deleting()" @confirm="deleteOffer()" @close="closeDelete()" />
|
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
@import('../UI/Table')
|
|
2
|
+
|
|
3
|
+
<script client>
|
|
4
|
+
import type { UpsellOfferRecord } from '../../../../app/Actions/Dashboard/Commerce/upsell-offer-records'
|
|
5
|
+
|
|
6
|
+
const records = useReactiveProp('records', [] as UpsellOfferRecord[])
|
|
7
|
+
const loading = useReactiveProp('loading', false)
|
|
8
|
+
const sortResetKey = useReactiveProp<unknown>('sortResetKey', '')
|
|
9
|
+
const currency = useReactiveProp('currency', 'USD')
|
|
10
|
+
const total = useReactiveProp('total', 0)
|
|
11
|
+
const currentPage = useReactiveProp('currentPage', 1)
|
|
12
|
+
const perPage = useReactiveProp('perPage', 25)
|
|
13
|
+
const emit = defineEmits(['view', 'edit', 'delete', 'page-change', 'page-size-change', 'sort'])
|
|
14
|
+
|
|
15
|
+
function percent(rate: number): string {
|
|
16
|
+
return `${(rate * 100).toFixed(rate > 0 && rate < 0.1 ? 1 : 0)}%`
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
const COLUMNS = derived(() => [
|
|
20
|
+
{ key: 'name', label: 'Offer', sortable: true, subtitleKey: 'productsLabel', class: 'font-medium text-gray-900 dark:text-white' },
|
|
21
|
+
{ key: 'dealLabel', label: 'Deal', subtitleKey: 'quantityLabel', searchable: false, class: 'font-semibold text-emerald-700 whitespace-nowrap dark:text-emerald-300' },
|
|
22
|
+
{
|
|
23
|
+
key: 'state',
|
|
24
|
+
label: 'Status',
|
|
25
|
+
type: 'badge' as const,
|
|
26
|
+
badgeClasses: {
|
|
27
|
+
running: 'bg-emerald-100 text-emerald-800 dark:bg-emerald-950/50 dark:text-emerald-300',
|
|
28
|
+
scheduled: 'bg-blue-100 text-blue-800 dark:bg-blue-950/50 dark:text-blue-300',
|
|
29
|
+
},
|
|
30
|
+
badgeLabels: { running: 'Running', scheduled: 'Scheduled', ended: 'Ended', disabled: 'Disabled' },
|
|
31
|
+
},
|
|
32
|
+
{ key: 'shown', label: 'Shown', type: 'number' as const, align: 'right' as const, searchable: false },
|
|
33
|
+
{ key: 'takenLabel', label: 'Taken', align: 'right' as const, subtitleKey: 'conversionLabel', searchable: false, class: 'font-mono whitespace-nowrap' },
|
|
34
|
+
{ key: 'revenue', label: 'Added to orders', type: 'currency' as const, currency: currency(), align: 'right' as const, searchable: false, class: 'font-semibold whitespace-nowrap' },
|
|
35
|
+
{ key: 'priority', label: 'Priority', type: 'number' as const, align: 'right' as const, searchable: false },
|
|
36
|
+
])
|
|
37
|
+
|
|
38
|
+
const ROW_ACTIONS = [
|
|
39
|
+
{ action: 'view', label: 'View results' },
|
|
40
|
+
{ action: 'edit', label: 'Edit' },
|
|
41
|
+
{ action: 'delete', label: 'Delete' },
|
|
42
|
+
]
|
|
43
|
+
|
|
44
|
+
const rows = derived(() => records().map(record => ({
|
|
45
|
+
...record,
|
|
46
|
+
productsLabel: record.products.map(product => product.label).join(', ') || 'No products',
|
|
47
|
+
dealLabel: record.discountPercent > 0 ? `${record.discountPercent}% off` : 'Full price',
|
|
48
|
+
quantityLabel: record.maxQuantity ? `${record.minQuantity} to ${record.maxQuantity} each` : `${record.minQuantity}+ each`,
|
|
49
|
+
shown: record.stats.views,
|
|
50
|
+
takenLabel: record.stats.accepted.toLocaleString(),
|
|
51
|
+
conversionLabel: `${percent(record.stats.conversionRate)} conversion`,
|
|
52
|
+
revenue: record.stats.revenue,
|
|
53
|
+
})))
|
|
54
|
+
|
|
55
|
+
function handleRowAction(payload: { action: string, row: UpsellOfferRecord }): void {
|
|
56
|
+
emit(payload.action, payload.row)
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
function changePage(page: number): void {
|
|
60
|
+
emit('page-change', page)
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
function changePageSize(size: number): void {
|
|
64
|
+
emit('page-size-change', size)
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
function changeSort(sort: { key: string, direction: string }): void {
|
|
68
|
+
emit('sort', sort)
|
|
69
|
+
}
|
|
70
|
+
</script>
|
|
71
|
+
|
|
72
|
+
<Table
|
|
73
|
+
:sort-reset-key="sortResetKey()"
|
|
74
|
+
:columns="COLUMNS()"
|
|
75
|
+
:data="rows()"
|
|
76
|
+
:loading="loading()"
|
|
77
|
+
:row-actions="ROW_ACTIONS"
|
|
78
|
+
action-label-key="name"
|
|
79
|
+
paginated
|
|
80
|
+
manual-pagination
|
|
81
|
+
manual-sort
|
|
82
|
+
:total="total()"
|
|
83
|
+
:current-page="currentPage()"
|
|
84
|
+
:page-size="perPage()"
|
|
85
|
+
empty-title="No post-purchase offers yet"
|
|
86
|
+
empty-description="Add an offer to show shoppers a one-time deal on their order confirmation page."
|
|
87
|
+
@row-action="handleRowAction"
|
|
88
|
+
@page-change="changePage"
|
|
89
|
+
@page-size-change="changePageSize"
|
|
90
|
+
@sort="changeSort"
|
|
91
|
+
/>
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
<template>
|
|
2
2
|
<div class="min-h-screen dark:bg-neutral-800">
|
|
3
3
|
<div class="flex h-screen">
|
|
4
|
-
<EmailSidebar :activeFolder="activeFolder" :folders="folders" :
|
|
4
|
+
<EmailSidebar :activeFolder="activeFolder" :folders="folders" :mailbox="mailbox" @update:active-folder="handleFolderChange" @compose="handleCompose" />
|
|
5
5
|
|
|
6
6
|
<div class="flex overflow-hidden flex-1 flex-col">
|
|
7
7
|
<div class="px-6 py-4 bg-white dark:bg-neutral-700 border-b border-gray-200 dark:border-neutral-600">
|
|
@@ -245,7 +245,6 @@ import type {
|
|
|
245
245
|
NotificationSound,
|
|
246
246
|
ReplyBehavior,
|
|
247
247
|
} from '@stacksjs/defaults/functions/mail-preferences'
|
|
248
|
-
import { fetchInboxActivity } from '@stacksjs/defaults/functions/inbox'
|
|
249
248
|
import { fetchMailPreference, saveMailPreference } from '@stacksjs/defaults/functions/mail-preferences'
|
|
250
249
|
|
|
251
250
|
interface SettingsSection {
|
|
@@ -264,15 +263,6 @@ const folders = [
|
|
|
264
263
|
{ id: 'trash', name: 'Trash', icon: 'waste' },
|
|
265
264
|
]
|
|
266
265
|
|
|
267
|
-
const unreadCounts = state({
|
|
268
|
-
inbox: 0,
|
|
269
|
-
starred: 0,
|
|
270
|
-
sent: 0,
|
|
271
|
-
drafts: 0,
|
|
272
|
-
archive: 0,
|
|
273
|
-
spam: 0,
|
|
274
|
-
trash: 0,
|
|
275
|
-
})
|
|
276
266
|
const mailbox = state('')
|
|
277
267
|
|
|
278
268
|
const settingsSections: SettingsSection[] = [
|
|
@@ -431,14 +421,9 @@ async function saveSettings(): Promise<void> {
|
|
|
431
421
|
|
|
432
422
|
onMount(async () => {
|
|
433
423
|
try {
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
mailbox.set(activity.mailbox)
|
|
438
|
-
accountEmail.set(activity.mailbox)
|
|
439
|
-
}
|
|
440
|
-
|
|
441
|
-
const preference = await fetchMailPreference(activity?.mailbox)
|
|
424
|
+
// Settings come from the configured mailbox's persisted preferences.
|
|
425
|
+
// Reading them must not require an available inbound storage provider.
|
|
426
|
+
const preference = await fetchMailPreference()
|
|
442
427
|
if (preference)
|
|
443
428
|
applyPreference(preference)
|
|
444
429
|
}
|