Enrich invoice line items and previews, migrate several CRM surfaces to Ant, and wire shared money/picker helpers.
178 lines
4.3 KiB
Vue
178 lines
4.3 KiB
Vue
<script setup lang="ts">
|
|
import { computed, ref, watch } from 'vue';
|
|
import {
|
|
Button as AButton,
|
|
Drawer as ADrawer,
|
|
Flex as AFlex,
|
|
Select as ASelect,
|
|
} from 'ant-design-vue';
|
|
import { UserAddOutlined } from '@/icons/ant-bridge';
|
|
import { api } from '@/lib/api';
|
|
import PersonFormView from '@/views/PersonFormView.vue';
|
|
import type { PaginatedResponse, PersonDto } from '@mento/shared';
|
|
|
|
const props = withDefaults(
|
|
defineProps<{
|
|
modelValue?: string;
|
|
placeholder?: string;
|
|
allowClear?: boolean;
|
|
disabled?: boolean;
|
|
addLabel?: string;
|
|
seedPeople?: PersonDto[];
|
|
}>(),
|
|
{
|
|
modelValue: '',
|
|
placeholder: 'جستجو و انتخاب شخص',
|
|
allowClear: true,
|
|
disabled: false,
|
|
addLabel: 'افزودن شخص جدید',
|
|
seedPeople: () => [],
|
|
},
|
|
);
|
|
|
|
const emit = defineEmits<{
|
|
'update:modelValue': [value: string];
|
|
change: [person: PersonDto | null];
|
|
created: [person: PersonDto];
|
|
}>();
|
|
|
|
const remembered = ref<PersonDto[]>([]);
|
|
const searchResults = ref<PersonDto[] | null>(null);
|
|
const searching = ref(false);
|
|
const searchQuery = ref('');
|
|
const modalOpen = ref(false);
|
|
let searchTimer: ReturnType<typeof setTimeout> | undefined;
|
|
let searchSeq = 0;
|
|
|
|
watch(
|
|
() => props.seedPeople,
|
|
(list) => {
|
|
for (const p of list ?? []) {
|
|
if (!remembered.value.some((x) => x.id === p.id)) {
|
|
remembered.value = [...remembered.value, p];
|
|
}
|
|
}
|
|
},
|
|
{ immediate: true, deep: true },
|
|
);
|
|
|
|
function remember(p: PersonDto) {
|
|
if (!remembered.value.some((x) => x.id === p.id)) {
|
|
remembered.value = [p, ...remembered.value];
|
|
}
|
|
}
|
|
|
|
const optionPeople = computed(() => searchResults.value ?? remembered.value);
|
|
|
|
const selectOptions = computed(() =>
|
|
optionPeople.value.map((p) => ({
|
|
value: p.id,
|
|
label: p.company?.name ? `${p.fullName} · ${p.company.name}` : p.fullName,
|
|
})),
|
|
);
|
|
|
|
const selectValue = computed(() => props.modelValue || undefined);
|
|
|
|
function onSearch(q: string) {
|
|
searchQuery.value = q;
|
|
if (searchTimer) clearTimeout(searchTimer);
|
|
const trimmed = q.trim();
|
|
if (!trimmed) {
|
|
searchResults.value = null;
|
|
searching.value = false;
|
|
return;
|
|
}
|
|
searching.value = true;
|
|
const seq = ++searchSeq;
|
|
searchTimer = setTimeout(async () => {
|
|
try {
|
|
const { data } = await api.get<PaginatedResponse<PersonDto>>('/people', {
|
|
params: { q: trimmed, limit: 30 },
|
|
});
|
|
if (seq === searchSeq) searchResults.value = data.items;
|
|
} catch {
|
|
if (seq === searchSeq) searchResults.value = [];
|
|
} finally {
|
|
if (seq === searchSeq) searching.value = false;
|
|
}
|
|
}, 300);
|
|
}
|
|
|
|
function onSelect(value: unknown) {
|
|
const pid = typeof value === 'string' ? value : '';
|
|
if (!pid) {
|
|
emit('update:modelValue', '');
|
|
emit('change', null);
|
|
return;
|
|
}
|
|
const p =
|
|
optionPeople.value.find((x) => x.id === pid) ??
|
|
remembered.value.find((x) => x.id === pid) ??
|
|
null;
|
|
if (p) remember(p);
|
|
emit('update:modelValue', pid);
|
|
emit('change', p);
|
|
}
|
|
|
|
function openCreate() {
|
|
modalOpen.value = true;
|
|
}
|
|
|
|
function onCreated(p?: PersonDto) {
|
|
modalOpen.value = false;
|
|
if (!p) return;
|
|
searchQuery.value = '';
|
|
searchResults.value = null;
|
|
remember(p);
|
|
emit('update:modelValue', p.id);
|
|
emit('created', p);
|
|
emit('change', p);
|
|
}
|
|
</script>
|
|
|
|
<template>
|
|
<a-flex align="center" :gap="8" style="width: 100%">
|
|
<a-select
|
|
:value="selectValue"
|
|
show-search
|
|
:allow-clear="allowClear"
|
|
:disabled="disabled"
|
|
:filter-option="false"
|
|
:options="selectOptions"
|
|
:loading="searching"
|
|
:list-height="160"
|
|
:not-found-content="searching ? 'در حال جستجو…' : 'شخصی یافت نشد'"
|
|
:placeholder="placeholder"
|
|
style="flex: 1; min-width: 0"
|
|
@search="onSearch"
|
|
@update:value="onSelect"
|
|
/>
|
|
<a-button
|
|
:disabled="disabled"
|
|
:title="addLabel"
|
|
:aria-label="addLabel"
|
|
@click="openCreate"
|
|
>
|
|
<template #icon><UserAddOutlined /></template>
|
|
</a-button>
|
|
</a-flex>
|
|
|
|
<a-drawer
|
|
v-model:open="modalOpen"
|
|
title="شخص جدید"
|
|
placement="right"
|
|
:width="720"
|
|
:footer="null"
|
|
destroy-on-close
|
|
@close="modalOpen = false"
|
|
>
|
|
<PersonFormView
|
|
v-if="modalOpen"
|
|
embedded
|
|
:initial-query="searchQuery"
|
|
@saved="onCreated"
|
|
@cancel="modalOpen = false"
|
|
/>
|
|
</a-drawer>
|
|
</template>
|