Save catalog delivery addresses and reuse them on the next request.
Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
import { ref } from 'vue'
|
||||
import { useAuthStore } from '@/stores/auth'
|
||||
import { addressesApi } from '@/services/addresses'
|
||||
import { ApiError } from '@/services/http'
|
||||
import { useCatalogRequestsStore } from '@/stores/catalogRequests'
|
||||
import type { CatalogProductCard } from '@/types/crm'
|
||||
|
||||
@@ -7,7 +8,6 @@ const SUCCESS_MESSAGE =
|
||||
'درخواست دریافت فیزیکی کالیته برای شما ثبت شد و به زودی همکاران ما برای شما خواهند آورد'
|
||||
|
||||
export function useCatalogRequest() {
|
||||
const auth = useAuthStore()
|
||||
const requestsStore = useCatalogRequestsStore()
|
||||
|
||||
const requesting = ref(false)
|
||||
@@ -47,11 +47,17 @@ export function useCatalogRequest() {
|
||||
|
||||
async function requestCatalog(product: CatalogProductCard) {
|
||||
addressError.value = ''
|
||||
const savedAddress = auth.user?.address?.trim()
|
||||
if (savedAddress) {
|
||||
let saved = ''
|
||||
try {
|
||||
const rows = await addressesApi.list()
|
||||
saved = (rows.find((row) => row.isDefault) ?? rows[0])?.text.trim() ?? ''
|
||||
} catch {
|
||||
saved = ''
|
||||
}
|
||||
if (saved) {
|
||||
requesting.value = true
|
||||
try {
|
||||
await createRequest(product, savedAddress)
|
||||
await createRequest(product, saved)
|
||||
} finally {
|
||||
requesting.value = false
|
||||
}
|
||||
@@ -73,14 +79,14 @@ export function useCatalogRequest() {
|
||||
addressError.value = ''
|
||||
requesting.value = true
|
||||
try {
|
||||
const updated = await auth.updateProfile({ address: trimmed })
|
||||
if (!updated) {
|
||||
addressError.value = 'ذخیره آدرس ناموفق بود.'
|
||||
return false
|
||||
}
|
||||
await addressesApi.create({ text: trimmed })
|
||||
const ok = await createRequest(product, trimmed)
|
||||
if (ok) closeAddressModal()
|
||||
return ok
|
||||
} catch (e) {
|
||||
addressError.value =
|
||||
e instanceof ApiError || e instanceof Error ? e.message : 'ذخیره آدرس ناموفق بود.'
|
||||
return false
|
||||
} finally {
|
||||
requesting.value = false
|
||||
}
|
||||
|
||||
@@ -227,6 +227,12 @@ const router = createRouter({
|
||||
component: () => import('@/views/profile/ProfileEditView.vue'),
|
||||
meta: { requiresAuth: true },
|
||||
},
|
||||
{
|
||||
path: '/profile/addresses',
|
||||
name: 'profile-addresses',
|
||||
component: () => import('@/views/profile/AddressesView.vue'),
|
||||
meta: { requiresAuth: true },
|
||||
},
|
||||
{
|
||||
path: '/profile/security',
|
||||
name: 'profile-security',
|
||||
|
||||
@@ -0,0 +1,17 @@
|
||||
import { apiRequest } from '@/services/http'
|
||||
|
||||
export type SavedAddress = {
|
||||
id: string
|
||||
label: string | null
|
||||
text: string
|
||||
isDefault: boolean
|
||||
}
|
||||
|
||||
export const addressesApi = {
|
||||
list: () => apiRequest<SavedAddress[]>('/addresses'),
|
||||
create: (body: { text: string; label?: string }) =>
|
||||
apiRequest<SavedAddress>('/addresses', { method: 'POST', body }),
|
||||
makeDefault: (id: string) =>
|
||||
apiRequest<SavedAddress[]>(`/addresses/${id}/default`, { method: 'POST' }),
|
||||
remove: (id: string) => apiRequest<SavedAddress[]>(`/addresses/${id}`, { method: 'DELETE' }),
|
||||
}
|
||||
@@ -1,6 +1,6 @@
|
||||
import { defineStore } from 'pinia'
|
||||
import { computed, ref } from 'vue'
|
||||
import { apiRequest } from '@/services/http'
|
||||
import { ApiError, apiRequest } from '@/services/http'
|
||||
|
||||
const STORAGE_KEY = 'ft_catalog_requests'
|
||||
|
||||
@@ -182,8 +182,10 @@ export const useCatalogRequestsStore = defineStore('catalogRequests', () => {
|
||||
writeAll(requests.value)
|
||||
return { success: true as const, request: mapped }
|
||||
} catch (err) {
|
||||
if (err instanceof ApiError && err.status >= 400 && err.status < 500) {
|
||||
return { success: false as const, message: err.message }
|
||||
}
|
||||
useApi.value = false
|
||||
// fall through to local
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -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