Save catalog delivery addresses and reuse them on the next request.
Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
@@ -0,0 +1,178 @@
|
||||
<script setup lang="ts">
|
||||
import { onMounted, ref } from 'vue'
|
||||
import BaseButton from '@/components/ui/BaseButton.vue'
|
||||
import ProfileSubpageLayout from '@/views/profile/components/ProfileSubpageLayout.vue'
|
||||
import { addressesApi, type SavedAddress } from '@/services/addresses'
|
||||
import { ApiError } from '@/services/http'
|
||||
|
||||
const rows = ref<SavedAddress[]>([])
|
||||
const text = ref('')
|
||||
const label = ref('')
|
||||
const error = ref('')
|
||||
const loading = ref(true)
|
||||
const saving = ref(false)
|
||||
|
||||
async function load() {
|
||||
loading.value = true
|
||||
error.value = ''
|
||||
try {
|
||||
rows.value = await addressesApi.list()
|
||||
} catch (e) {
|
||||
error.value = e instanceof ApiError || e instanceof Error ? e.message : 'دریافت آدرسها ناموفق بود.'
|
||||
} finally {
|
||||
loading.value = false
|
||||
}
|
||||
}
|
||||
|
||||
onMounted(load)
|
||||
|
||||
async function add() {
|
||||
const trimmed = text.value.trim()
|
||||
if (trimmed.length < 3 || saving.value) {
|
||||
error.value = 'آدرس را کاملتر بنویسید.'
|
||||
return
|
||||
}
|
||||
saving.value = true
|
||||
error.value = ''
|
||||
try {
|
||||
await addressesApi.create({ text: trimmed, label: label.value.trim() || undefined })
|
||||
text.value = ''
|
||||
label.value = ''
|
||||
rows.value = await addressesApi.list()
|
||||
} catch (e) {
|
||||
error.value = e instanceof ApiError || e instanceof Error ? e.message : 'ذخیره آدرس ناموفق بود.'
|
||||
} finally {
|
||||
saving.value = false
|
||||
}
|
||||
}
|
||||
|
||||
async function makeDefault(id: string) {
|
||||
error.value = ''
|
||||
try {
|
||||
rows.value = await addressesApi.makeDefault(id)
|
||||
} catch (e) {
|
||||
error.value = e instanceof Error ? e.message : 'تغییر آدرس پیشفرض ناموفق بود.'
|
||||
}
|
||||
}
|
||||
|
||||
async function remove(id: string) {
|
||||
error.value = ''
|
||||
try {
|
||||
rows.value = await addressesApi.remove(id)
|
||||
} catch (e) {
|
||||
error.value = e instanceof Error ? e.message : 'حذف آدرس ناموفق بود.'
|
||||
}
|
||||
}
|
||||
</script>
|
||||
|
||||
<template>
|
||||
<ProfileSubpageLayout title="آدرسها" subtitle="برای ارسال کالیته از آدرس پیشفرض استفاده میشود">
|
||||
<p v-if="error" class="addr-error">{{ error }}</p>
|
||||
<p v-if="loading" class="addr-muted">در حال دریافت…</p>
|
||||
<ul v-else-if="rows.length" class="addr-list">
|
||||
<li v-for="row in rows" :key="row.id" class="addr-item">
|
||||
<div>
|
||||
<strong>{{ row.label || 'آدرس' }}</strong>
|
||||
<span v-if="row.isDefault" class="addr-badge">پیشفرض</span>
|
||||
<p>{{ row.text }}</p>
|
||||
</div>
|
||||
<div class="addr-actions">
|
||||
<button v-if="!row.isDefault" type="button" @click="makeDefault(row.id)">پیشفرض</button>
|
||||
<button type="button" class="addr-remove" @click="remove(row.id)">حذف</button>
|
||||
</div>
|
||||
</li>
|
||||
</ul>
|
||||
<p v-else class="addr-muted">هنوز آدرسی ثبت نشده است.</p>
|
||||
|
||||
<label class="addr-field">
|
||||
<span>عنوان (اختیاری)</span>
|
||||
<input v-model="label" type="text" maxlength="40" placeholder="مثلاً فروشگاه" />
|
||||
</label>
|
||||
<label class="addr-field">
|
||||
<span>آدرس</span>
|
||||
<textarea v-model="text" rows="3" maxlength="500" placeholder="آدرس کامل محل فعالیت" />
|
||||
</label>
|
||||
<BaseButton :loading="saving" @click="add">افزودن آدرس</BaseButton>
|
||||
</ProfileSubpageLayout>
|
||||
</template>
|
||||
|
||||
<style scoped>
|
||||
.addr-error {
|
||||
color: var(--color-danger);
|
||||
font-size: 0.8125rem;
|
||||
}
|
||||
|
||||
.addr-muted {
|
||||
color: var(--color-text-muted);
|
||||
font-size: 0.875rem;
|
||||
}
|
||||
|
||||
.addr-list {
|
||||
list-style: none;
|
||||
margin: 0 0 16px;
|
||||
padding: 0;
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
gap: 10px;
|
||||
}
|
||||
|
||||
.addr-item {
|
||||
display: flex;
|
||||
justify-content: space-between;
|
||||
gap: 12px;
|
||||
padding: 12px;
|
||||
border: 1px solid var(--color-border);
|
||||
border-radius: 12px;
|
||||
}
|
||||
|
||||
.addr-item p {
|
||||
margin: 6px 0 0;
|
||||
font-size: 0.875rem;
|
||||
line-height: 1.6;
|
||||
}
|
||||
|
||||
.addr-badge {
|
||||
margin-right: 8px;
|
||||
font-size: 0.75rem;
|
||||
color: var(--color-primary);
|
||||
}
|
||||
|
||||
.addr-actions {
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
gap: 6px;
|
||||
}
|
||||
|
||||
.addr-actions button {
|
||||
border: 0;
|
||||
background: transparent;
|
||||
color: var(--color-primary);
|
||||
font: inherit;
|
||||
cursor: pointer;
|
||||
}
|
||||
|
||||
.addr-remove {
|
||||
color: var(--color-danger) !important;
|
||||
}
|
||||
|
||||
.addr-field {
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
gap: 6px;
|
||||
margin-bottom: 12px;
|
||||
font-size: 0.8125rem;
|
||||
font-weight: 600;
|
||||
}
|
||||
|
||||
.addr-field input,
|
||||
.addr-field textarea {
|
||||
width: 100%;
|
||||
padding: 10px 12px;
|
||||
border: 1px solid var(--color-border);
|
||||
border-radius: 10px;
|
||||
font: inherit;
|
||||
font-weight: 400;
|
||||
color: var(--color-text);
|
||||
background: transparent;
|
||||
}
|
||||
</style>
|
||||
@@ -91,7 +91,7 @@ const companyRows = computed(() => [
|
||||
|
||||
const quickAccess = [
|
||||
{ label: 'حسابهای بانکی', icon: IconCashSettlement },
|
||||
{ label: 'آدرسها', icon: IconMapPin },
|
||||
{ label: 'آدرسها', icon: IconMapPin, path: '/profile/addresses' },
|
||||
{ label: 'حمایت و پشتیبانی', icon: IconHeadset, href: 'tel:02112345678' },
|
||||
{ label: 'فاکتورهای من', icon: IconOrders, path: '/orders' },
|
||||
]
|
||||
|
||||
Reference in New Issue
Block a user