refactor: br id

This commit is contained in:
puriphatt 2024-09-04 17:49:12 +07:00
parent db10c7619c
commit 55f26b3758
2 changed files with 41 additions and 10 deletions

View file

@ -1,7 +1,7 @@
<script setup lang="ts"> <script setup lang="ts">
import useUserStore from 'stores/user'; import useUserStore from 'stores/user';
import { selectFilterOptionRefMod } from 'stores/utils'; import { selectFilterOptionRefMod } from 'stores/utils';
import { onMounted, ref } from 'vue'; import { onMounted, ref, watch } from 'vue';
import { useI18n } from 'vue-i18n'; import { useI18n } from 'vue-i18n';
import { isRoleInclude } from 'src/stores/utils'; import { isRoleInclude } from 'src/stores/utils';
@ -15,7 +15,7 @@ const userRole = defineModel<string>('userRole');
const username = defineModel<string | null | undefined>('username'); const username = defineModel<string | null | undefined>('username');
const userCode = defineModel<string>('userCode'); const userCode = defineModel<string>('userCode');
defineProps<{ const props = defineProps<{
title?: string; title?: string;
dense?: boolean; dense?: boolean;
outlined?: boolean; outlined?: boolean;
@ -73,9 +73,33 @@ const roleFilter = selectFilterOptionRefMod(
); );
onMounted(async () => { onMounted(async () => {
if (userStore.userOption.hqOpts[0].value) if (userStore.userOption.hqOpts[0].value && !props.readonly) {
await userStore.fetchBrOption(userStore.userOption.hqOpts[0].value); await userStore.fetchBrOption(userStore.userOption.hqOpts[0].value);
if (userStore.userOption.brOpts.length === 1) {
brId.value = userStore.userOption.brOpts[0].value;
}
brFilter = selectFilterOptionRefMod(
ref(userStore.userOption.brOpts),
brOptions,
'label',
);
}
}); });
watch(
() => hqId.value,
async (v) => {
if (v) {
userStore.userOption.brOpts = [];
await userStore.fetchBrOption(v);
brFilter = selectFilterOptionRefMod(
ref(userStore.userOption.brOpts),
brOptions,
'label',
);
}
},
);
</script> </script>
<template> <template>
<div class="row col-12"> <div class="row col-12">

View file

@ -59,12 +59,19 @@ const useUserStore = defineStore('api-user', () => {
const res = await branchStore.fetchById(id, { const res = await branchStore.fetchById(id, {
includeSubBranch: true, includeSubBranch: true,
}); });
if (res && res?.branch) {
res.branch.map((item) => { if (res && res.branch) {
userOption.value.brOpts.push({ res.branch.forEach((item) => {
label: item.code, const exists = userOption.value.brOpts.some(
value: item.id, (opt) => opt.value === item.id,
}); );
if (!exists) {
userOption.value.brOpts.push({
label: item.code,
value: item.id,
});
}
}); });
} }
} }