Add a coupons page where a shop user lists, creates, edits, enables/disables, and deletes only its own templates, with localized titles, minor-unit discount and threshold, currency, claim stock, and an active window. Expose it as a coupons navigation entry beside the existing shop operations.
317 lines
9.8 KiB
Vue
317 lines
9.8 KiB
Vue
<script setup lang="ts">
|
|
import type { CouponTemplate, CouponTemplateInput, Currency } from "@vmall/shared";
|
|
|
|
definePageMeta({ middleware: "auth" });
|
|
|
|
const { $api } = useNuxtApp();
|
|
const { locale, t: translate } = useI18n();
|
|
const { load: loadMoney, fmt } = useMoney();
|
|
|
|
const templates = ref<CouponTemplate[]>([]);
|
|
const currencies = ref<Currency[]>([]);
|
|
const loading = ref(true);
|
|
const saving = ref(false);
|
|
const actionId = ref("");
|
|
const error = ref("");
|
|
const message = ref("");
|
|
const editingId = ref("");
|
|
|
|
const form = reactive({
|
|
titleEn: "",
|
|
titleZh: "",
|
|
amountMinor: 0,
|
|
thresholdMinor: 0,
|
|
currency: "USD",
|
|
stock: 0,
|
|
enabled: true,
|
|
startsAt: "",
|
|
endsAt: "",
|
|
});
|
|
|
|
function toLocalInput(iso: string): string {
|
|
const d = new Date(iso);
|
|
const pad = (n: number) => String(n).padStart(2, "0");
|
|
return `${d.getFullYear()}-${pad(d.getMonth() + 1)}-${pad(d.getDate())}T${pad(d.getHours())}:${pad(d.getMinutes())}`;
|
|
}
|
|
|
|
function resetForm(): void {
|
|
const now = new Date();
|
|
const later = new Date(now.getTime() + 30 * 24 * 3600 * 1000);
|
|
editingId.value = "";
|
|
form.titleEn = "";
|
|
form.titleZh = "";
|
|
form.amountMinor = 0;
|
|
form.thresholdMinor = 0;
|
|
form.currency =
|
|
currencies.value.find((c) => c.is_base)?.code ?? currencies.value[0]?.code ?? "USD";
|
|
form.stock = 0;
|
|
form.enabled = true;
|
|
form.startsAt = toLocalInput(now.toISOString());
|
|
form.endsAt = toLocalInput(later.toISOString());
|
|
}
|
|
|
|
async function loadTemplates(): Promise<void> {
|
|
loading.value = true;
|
|
error.value = "";
|
|
try {
|
|
templates.value = await $api.shop.listCouponTemplates();
|
|
} catch (err: unknown) {
|
|
error.value = err instanceof Error ? err.message : translate("common.error");
|
|
} finally {
|
|
loading.value = false;
|
|
}
|
|
}
|
|
|
|
async function loadCurrencies(): Promise<void> {
|
|
try {
|
|
currencies.value = (await $api.listCurrencies()).filter((c) => c.enabled);
|
|
} catch {
|
|
currencies.value = [];
|
|
}
|
|
}
|
|
|
|
function payload(): CouponTemplateInput {
|
|
return {
|
|
title: { en: form.titleEn.trim(), zh: form.titleZh.trim() },
|
|
amount_minor: Number(form.amountMinor),
|
|
threshold_minor: Number(form.thresholdMinor),
|
|
currency: form.currency,
|
|
stock: Number(form.stock),
|
|
enabled: form.enabled,
|
|
starts_at: new Date(form.startsAt).toISOString(),
|
|
ends_at: new Date(form.endsAt).toISOString(),
|
|
};
|
|
}
|
|
|
|
async function submit(): Promise<void> {
|
|
error.value = "";
|
|
message.value = "";
|
|
const body = payload();
|
|
if (!body.title.en || !body.title.zh) {
|
|
error.value = translate("shop.couponTitleRequired");
|
|
return;
|
|
}
|
|
if (!(body.amount_minor > 0)) {
|
|
error.value = translate("shop.couponAmountInvalid");
|
|
return;
|
|
}
|
|
if (body.threshold_minor < 0) {
|
|
error.value = translate("shop.couponThresholdInvalid");
|
|
return;
|
|
}
|
|
if (body.stock < 0) {
|
|
error.value = translate("shop.couponStockInvalid");
|
|
return;
|
|
}
|
|
if (new Date(body.ends_at) < new Date(body.starts_at)) {
|
|
error.value = translate("shop.couponWindowInvalid");
|
|
return;
|
|
}
|
|
saving.value = true;
|
|
try {
|
|
if (editingId.value) await $api.shop.updateCouponTemplate(editingId.value, body);
|
|
else await $api.shop.createCouponTemplate(body);
|
|
message.value = translate("shop.couponSaved");
|
|
resetForm();
|
|
await loadTemplates();
|
|
} catch (err: unknown) {
|
|
error.value = err instanceof Error ? err.message : translate("common.error");
|
|
} finally {
|
|
saving.value = false;
|
|
}
|
|
}
|
|
|
|
function edit(template: CouponTemplate): void {
|
|
editingId.value = template.id;
|
|
form.titleEn = template.title.en ?? "";
|
|
form.titleZh = template.title.zh ?? "";
|
|
form.amountMinor = template.amount_minor;
|
|
form.thresholdMinor = template.threshold_minor;
|
|
form.currency = template.currency;
|
|
form.stock = template.stock;
|
|
form.enabled = template.enabled;
|
|
form.startsAt = toLocalInput(template.starts_at);
|
|
form.endsAt = toLocalInput(template.ends_at);
|
|
}
|
|
|
|
async function toggleEnabled(template: CouponTemplate): Promise<void> {
|
|
actionId.value = template.id;
|
|
error.value = "";
|
|
try {
|
|
await $api.shop.updateCouponTemplate(template.id, {
|
|
title: template.title,
|
|
amount_minor: template.amount_minor,
|
|
threshold_minor: template.threshold_minor,
|
|
currency: template.currency,
|
|
stock: template.stock,
|
|
enabled: !template.enabled,
|
|
starts_at: template.starts_at,
|
|
ends_at: template.ends_at,
|
|
});
|
|
await loadTemplates();
|
|
} catch (err: unknown) {
|
|
error.value = err instanceof Error ? err.message : translate("common.error");
|
|
} finally {
|
|
actionId.value = "";
|
|
}
|
|
}
|
|
|
|
async function remove(template: CouponTemplate): Promise<void> {
|
|
actionId.value = template.id;
|
|
error.value = "";
|
|
message.value = "";
|
|
try {
|
|
await $api.shop.deleteCouponTemplate(template.id);
|
|
message.value = translate("shop.couponDeleted");
|
|
await loadTemplates();
|
|
} catch (err: unknown) {
|
|
error.value = err instanceof Error ? err.message : translate("common.error");
|
|
} finally {
|
|
actionId.value = "";
|
|
}
|
|
}
|
|
|
|
onMounted(async () => {
|
|
loadMoney();
|
|
await loadCurrencies();
|
|
resetForm();
|
|
await loadTemplates();
|
|
});
|
|
</script>
|
|
|
|
<template>
|
|
<div class="page">
|
|
<h1 class="page-title">{{ $t("shop.couponList") }}</h1>
|
|
<div v-if="error" class="error-text" role="alert">{{ error }}</div>
|
|
<div v-if="message" class="muted" role="status">{{ message }}</div>
|
|
|
|
<section class="card">
|
|
<h2>{{ editingId ? $t("shop.editCoupon") : $t("shop.newCoupon") }}</h2>
|
|
<div class="form-grid">
|
|
<label>
|
|
<span>{{ $t("shop.couponTitleEn") }}</span>
|
|
<input v-model="form.titleEn" class="input" type="text" />
|
|
</label>
|
|
<label>
|
|
<span>{{ $t("shop.couponTitleZh") }}</span>
|
|
<input v-model="form.titleZh" class="input" type="text" />
|
|
</label>
|
|
<label>
|
|
<span>{{ $t("shop.couponAmount") }}</span>
|
|
<input v-model.number="form.amountMinor" class="input" type="number" min="1" step="1" />
|
|
</label>
|
|
<label>
|
|
<span>{{ $t("shop.couponThreshold") }}</span>
|
|
<input v-model.number="form.thresholdMinor" class="input" type="number" min="0" step="1" />
|
|
</label>
|
|
<label>
|
|
<span>{{ $t("shop.couponCurrency") }}</span>
|
|
<select v-model="form.currency" class="input">
|
|
<option v-for="c in currencies" :key="c.code" :value="c.code">{{ c.code }}</option>
|
|
</select>
|
|
</label>
|
|
<label>
|
|
<span>{{ $t("shop.couponStock") }}</span>
|
|
<input v-model.number="form.stock" class="input" type="number" min="0" step="1" />
|
|
</label>
|
|
<label>
|
|
<span>{{ $t("shop.couponStartsAt") }}</span>
|
|
<input v-model="form.startsAt" class="input" type="datetime-local" />
|
|
</label>
|
|
<label>
|
|
<span>{{ $t("shop.couponEndsAt") }}</span>
|
|
<input v-model="form.endsAt" class="input" type="datetime-local" />
|
|
</label>
|
|
<label class="checkbox">
|
|
<input v-model="form.enabled" type="checkbox" />
|
|
<span>{{ $t("shop.couponEnabled") }}</span>
|
|
</label>
|
|
</div>
|
|
<div class="form-actions">
|
|
<button class="btn primary" :disabled="saving" @click="submit">
|
|
{{ editingId ? $t("shop.updateCoupon") : $t("shop.createCoupon") }}
|
|
</button>
|
|
<button v-if="editingId" class="btn" type="button" @click="resetForm">
|
|
{{ $t("common.cancel") }}
|
|
</button>
|
|
</div>
|
|
</section>
|
|
|
|
<p v-if="loading" class="muted">{{ $t("common.loading") }}</p>
|
|
<div v-else-if="!templates.length" class="card muted">{{ $t("common.empty") }}</div>
|
|
<div v-else class="table-wrap">
|
|
<table class="table">
|
|
<thead>
|
|
<tr>
|
|
<th>{{ $t("shop.couponTitleEn") }}</th>
|
|
<th>{{ $t("shop.couponAmount") }}</th>
|
|
<th>{{ $t("shop.couponThreshold") }}</th>
|
|
<th>{{ $t("shop.couponStock") }}</th>
|
|
<th>{{ $t("shop.couponStartsAt") }}</th>
|
|
<th>{{ $t("shop.couponEndsAt") }}</th>
|
|
<th>{{ $t("common.status") }}</th>
|
|
<th>{{ $t("common.actions") }}</th>
|
|
</tr>
|
|
</thead>
|
|
<tbody>
|
|
<tr v-for="template in templates" :key="template.id">
|
|
<td>{{ template.title[locale] ?? template.title.en }}</td>
|
|
<td>{{ fmt(template.amount_minor, template.currency) }}</td>
|
|
<td>{{ fmt(template.threshold_minor, template.currency) }}</td>
|
|
<td>{{ template.stock }}</td>
|
|
<td>{{ template.starts_at.slice(0, 10) }}</td>
|
|
<td>{{ template.ends_at.slice(0, 10) }}</td>
|
|
<td>
|
|
<span class="badge" :class="template.enabled ? 'green' : 'red'">
|
|
{{ template.enabled ? $t("shop.enabledOn") : $t("shop.enabledOff") }}
|
|
</span>
|
|
</td>
|
|
<td class="actions">
|
|
<button class="btn sm" :disabled="actionId === template.id" @click="edit(template)">
|
|
{{ $t("shop.edit") }}
|
|
</button>
|
|
<button class="btn sm" :disabled="actionId === template.id" @click="toggleEnabled(template)">
|
|
{{ template.enabled ? $t("shop.enabledOff") : $t("shop.enabledOn") }}
|
|
</button>
|
|
<button class="btn sm" :disabled="actionId === template.id" @click="remove(template)">
|
|
{{ $t("common.delete") }}
|
|
</button>
|
|
</td>
|
|
</tr>
|
|
</tbody>
|
|
</table>
|
|
</div>
|
|
</div>
|
|
</template>
|
|
|
|
<style scoped>
|
|
.form-grid {
|
|
display: grid;
|
|
grid-template-columns: repeat(auto-fit, minmax(220px, 1fr));
|
|
gap: 12px;
|
|
margin: 12px 0;
|
|
}
|
|
.form-grid label {
|
|
display: flex;
|
|
flex-direction: column;
|
|
gap: 4px;
|
|
font-size: 13px;
|
|
}
|
|
.form-grid .checkbox {
|
|
flex-direction: row;
|
|
align-items: center;
|
|
gap: 8px;
|
|
}
|
|
.form-actions {
|
|
display: flex;
|
|
gap: 8px;
|
|
}
|
|
.table-wrap {
|
|
overflow-x: auto;
|
|
}
|
|
.actions {
|
|
display: flex;
|
|
gap: 6px;
|
|
}
|
|
</style>
|