2023-10-11 15:32:18 -04:00
|
|
|
<script lang="ts" setup>
|
|
|
|
import { debounce } from "lodash-es";
|
|
|
|
import { useField } from "vee-validate";
|
2024-01-04 17:00:21 -05:00
|
|
|
import { IUser } from "@models/user";
|
2023-10-11 16:26:47 -04:00
|
|
|
const props = defineProps<{
|
|
|
|
fieldName: string;
|
|
|
|
multi: boolean;
|
2024-01-04 17:00:21 -05:00
|
|
|
initialOption:
|
|
|
|
| number
|
|
|
|
| {
|
|
|
|
label: string;
|
|
|
|
value: number;
|
|
|
|
}
|
|
|
|
| null;
|
2023-10-11 16:26:47 -04:00
|
|
|
}>();
|
2024-01-04 17:00:21 -05:00
|
|
|
|
|
|
|
let initO;
|
|
|
|
if (typeof props.initialOption == "number") {
|
|
|
|
const { data } = await useApiFetch<IUser>(`/user/${props.initialOption}`);
|
|
|
|
initO = {
|
|
|
|
label: data.value.username,
|
|
|
|
value: data.value._id,
|
|
|
|
};
|
|
|
|
} else if (typeof props.initialOption == "object") {
|
|
|
|
initO = props.initialOption;
|
|
|
|
}
|
|
|
|
|
2023-10-11 15:32:18 -04:00
|
|
|
const state = reactive<{
|
|
|
|
data: { value: number; label: string }[];
|
|
|
|
fetching: boolean;
|
|
|
|
}>({
|
2024-01-04 17:00:21 -05:00
|
|
|
data: props.initialOption ? [initO] : [],
|
2023-10-11 15:32:18 -04:00
|
|
|
fetching: false,
|
|
|
|
});
|
|
|
|
let fieldo = useField<number | number[]>(props.fieldName, undefined, {
|
|
|
|
// @ts-ignore
|
2024-01-04 17:00:21 -05:00
|
|
|
initialValue: initO.value || null,
|
2023-10-11 15:32:18 -04:00
|
|
|
});
|
|
|
|
|
|
|
|
const { value, errorMessage, name, setValue } = fieldo;
|
|
|
|
|
|
|
|
const fetchUsers = debounce((value) => {
|
|
|
|
state.fetching = true;
|
|
|
|
useApiFetch<{ _id: number; username: string }[]>(`/all-users`, {
|
|
|
|
query: {
|
|
|
|
name: value,
|
|
|
|
},
|
|
|
|
}).then(({ data }) => {
|
|
|
|
const blip = data.value?.map((a) => ({
|
|
|
|
label: a.username,
|
|
|
|
value: a._id,
|
|
|
|
}));
|
|
|
|
state.data = blip!;
|
|
|
|
state.fetching = false;
|
|
|
|
});
|
|
|
|
}, 750);
|
|
|
|
</script>
|
|
|
|
<template>
|
|
|
|
<a-select
|
|
|
|
option-filter-prop="label"
|
|
|
|
:show-search="true"
|
|
|
|
@search="fetchUsers"
|
|
|
|
:filter-option="true"
|
|
|
|
style="width: 100%"
|
|
|
|
placeholder="Find a user..."
|
|
|
|
:mode="multi ? 'multiple' : undefined"
|
|
|
|
:options="state.data"
|
|
|
|
v-model:value="value"
|
|
|
|
>
|
|
|
|
<template v-if="state.fetching" #notFoundContent>
|
|
|
|
<a-spin size="small" />
|
|
|
|
</template>
|
|
|
|
</a-select>
|
|
|
|
</template>
|