feat: Personnel =>addrOption, roleOption, profileImg
This commit is contained in:
parent
f422d00d23
commit
f28b68bcee
1 changed files with 101 additions and 33 deletions
|
|
@ -5,7 +5,12 @@ import { storeToRefs } from 'pinia';
|
||||||
import useUserStore from 'stores/user';
|
import useUserStore from 'stores/user';
|
||||||
import useBranchStore from 'src/stores/branch';
|
import useBranchStore from 'src/stores/branch';
|
||||||
|
|
||||||
import { UserCreate, UserTypeStats, UserOption } from 'src/stores/user/types';
|
import {
|
||||||
|
UserCreate,
|
||||||
|
UserTypeStats,
|
||||||
|
UserOption,
|
||||||
|
RoleData,
|
||||||
|
} from 'src/stores/user/types';
|
||||||
import { BranchUserStats } from 'src/stores/branch/types';
|
import { BranchUserStats } from 'src/stores/branch/types';
|
||||||
|
|
||||||
import PersonCard from 'components/home/PersonCard.vue';
|
import PersonCard from 'components/home/PersonCard.vue';
|
||||||
|
|
@ -53,6 +58,7 @@ const defaultFormData = {
|
||||||
responsibleArea: '',
|
responsibleArea: '',
|
||||||
};
|
};
|
||||||
|
|
||||||
|
const urlProfile = ref<string>();
|
||||||
const isEdit = ref(false);
|
const isEdit = ref(false);
|
||||||
const modal = ref(false);
|
const modal = ref(false);
|
||||||
const status = ref(false);
|
const status = ref(false);
|
||||||
|
|
@ -62,6 +68,10 @@ const selectorLabel = ref('');
|
||||||
const hqId = ref('');
|
const hqId = ref('');
|
||||||
const brId = ref('');
|
const brId = ref('');
|
||||||
const username = ref('');
|
const username = ref('');
|
||||||
|
const age = ref<string>();
|
||||||
|
const formDialogRef = ref();
|
||||||
|
const userStats = ref<BranchUserStats[]>();
|
||||||
|
const typeStats = ref<UserTypeStats>();
|
||||||
const formData = ref<UserCreate>({
|
const formData = ref<UserCreate>({
|
||||||
provinceId: null,
|
provinceId: null,
|
||||||
districtId: null,
|
districtId: null,
|
||||||
|
|
@ -93,9 +103,6 @@ const formData = ref<UserCreate>({
|
||||||
birthDate: null,
|
birthDate: null,
|
||||||
responsibleArea: '',
|
responsibleArea: '',
|
||||||
});
|
});
|
||||||
const userStats = ref<BranchUserStats[]>();
|
|
||||||
const typeStats = ref<UserTypeStats>();
|
|
||||||
const age = ref<number>();
|
|
||||||
|
|
||||||
const profileFile = ref<File | undefined>(undefined);
|
const profileFile = ref<File | undefined>(undefined);
|
||||||
const inputFile = document.createElement('input');
|
const inputFile = document.createElement('input');
|
||||||
|
|
@ -120,6 +127,7 @@ const genderOpts = [
|
||||||
const userOption = ref<UserOption>({
|
const userOption = ref<UserOption>({
|
||||||
hqOpts: [],
|
hqOpts: [],
|
||||||
brOpts: [],
|
brOpts: [],
|
||||||
|
roleOpts: [],
|
||||||
});
|
});
|
||||||
|
|
||||||
const selectorList = [
|
const selectorList = [
|
||||||
|
|
@ -139,6 +147,16 @@ async function createKeycloak() {
|
||||||
return res.data;
|
return res.data;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async function fetchRoleOption() {
|
||||||
|
const res = await api.get<RoleData[]>('/keycloak/role');
|
||||||
|
res.data.map((item) => {
|
||||||
|
userOption.value.roleOpts.push({
|
||||||
|
label: item.name,
|
||||||
|
value: item.id,
|
||||||
|
});
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
async function fetchHqOption() {
|
async function fetchHqOption() {
|
||||||
if (userOption.value.hqOpts.length === 0) {
|
if (userOption.value.hqOpts.length === 0) {
|
||||||
const res = await branchStore.fetchList({ pageSize: 999, filter: 'head' });
|
const res = await branchStore.fetchList({ pageSize: 999, filter: 'head' });
|
||||||
|
|
@ -169,6 +187,7 @@ async function fetchBrOption(id: string) {
|
||||||
|
|
||||||
async function openDialog(id?: string) {
|
async function openDialog(id?: string) {
|
||||||
await fetchHqOption();
|
await fetchHqOption();
|
||||||
|
await fetchRoleOption();
|
||||||
modal.value = true;
|
modal.value = true;
|
||||||
if (id && userData.value) {
|
if (id && userData.value) {
|
||||||
const foundUser = userData.value.result.find((user) => user.id === id);
|
const foundUser = userData.value.result.find((user) => user.id === id);
|
||||||
|
|
@ -196,7 +215,11 @@ async function openDialog(id?: string) {
|
||||||
hqId.value = foundUser.branch[0].headOfficeId as string;
|
hqId.value = foundUser.branch[0].headOfficeId as string;
|
||||||
brId.value = foundUser.branch[0].id;
|
brId.value = foundUser.branch[0].id;
|
||||||
code.value = foundUser.code;
|
code.value = foundUser.code;
|
||||||
|
urlProfile.value = foundUser.profileImageUrl;
|
||||||
isEdit.value = true;
|
isEdit.value = true;
|
||||||
|
await fetchBrOption(hqId.value);
|
||||||
|
await formDialogRef.value.fetchSubDistrict(formData.value.districtId);
|
||||||
|
await formDialogRef.value.fetchDistrict(formData.value.provinceId);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -204,31 +227,28 @@ async function openDialog(id?: string) {
|
||||||
function onClose() {
|
function onClose() {
|
||||||
modal.value = false;
|
modal.value = false;
|
||||||
Object.assign(formData.value, defaultFormData);
|
Object.assign(formData.value, defaultFormData);
|
||||||
|
mapUserType(selectorLabel.value);
|
||||||
|
isEdit.value = false;
|
||||||
hqId.value = '';
|
hqId.value = '';
|
||||||
brId.value = '';
|
brId.value = '';
|
||||||
username.value = '';
|
username.value = '';
|
||||||
userId.value = '';
|
userId.value = '';
|
||||||
code.value = '';
|
code.value = '';
|
||||||
mapUserType(selectorLabel.value);
|
urlProfile.value = '';
|
||||||
}
|
}
|
||||||
|
|
||||||
async function onSubmit() {
|
async function onSubmit() {
|
||||||
console.log('enter');
|
|
||||||
formData.value.profileImage = profileFile.value as File;
|
formData.value.profileImage = profileFile.value as File;
|
||||||
if (isEdit.value === true && userId.value) {
|
if (isEdit.value === true && userId.value) {
|
||||||
console.log('edit');
|
const formDataEdit = { ...formData.value };
|
||||||
await userStore.editById(userId.value, formData.value);
|
delete formDataEdit.keycloakId;
|
||||||
|
await userStore.editById(userId.value, formDataEdit);
|
||||||
} else {
|
} else {
|
||||||
console.log('post');
|
|
||||||
formData.value.keycloakId = await createKeycloak();
|
formData.value.keycloakId = await createKeycloak();
|
||||||
if (formData.value.keycloakId !== '') {
|
if (formData.value.keycloakId) {
|
||||||
console.log('keycloakOk');
|
|
||||||
const result = await userStore.create(formData.value);
|
const result = await userStore.create(formData.value);
|
||||||
console.log('Result after create:', result);
|
|
||||||
if (result) {
|
if (result) {
|
||||||
console.log('hi3');
|
await branchStore.addUser(brId.value, result.id);
|
||||||
console.log(result.id);
|
|
||||||
// await branchStore.addUser(result.id, userId.value);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -253,6 +273,27 @@ function mapUserType(label: string) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function calculateAge(birthDate: Date | null) {
|
||||||
|
if (!birthDate) return null;
|
||||||
|
const birthDateTimeStamp = new Date(birthDate).getTime();
|
||||||
|
const now = new Date();
|
||||||
|
const diff = now.getTime() - birthDateTimeStamp;
|
||||||
|
|
||||||
|
const ageDate = new Date(diff);
|
||||||
|
const years = ageDate.getUTCFullYear() - 1970;
|
||||||
|
const months = ageDate.getUTCMonth();
|
||||||
|
const days = ageDate.getUTCDate() - 1;
|
||||||
|
|
||||||
|
age.value = `${years} ปี ${months} เดือน ${days} วัน`;
|
||||||
|
}
|
||||||
|
|
||||||
|
async function selectHq(id: string) {
|
||||||
|
if (!id) return;
|
||||||
|
brId.value = '';
|
||||||
|
userOption.value.brOpts = [];
|
||||||
|
await fetchBrOption(id);
|
||||||
|
}
|
||||||
|
|
||||||
onMounted(async () => {
|
onMounted(async () => {
|
||||||
await userStore.fetchList({ includeBranch: true });
|
await userStore.fetchList({ includeBranch: true });
|
||||||
typeStats.value = await userStore.typeStats();
|
typeStats.value = await userStore.typeStats();
|
||||||
|
|
@ -274,11 +315,11 @@ watch(
|
||||||
);
|
);
|
||||||
|
|
||||||
watch(
|
watch(
|
||||||
() => hqId.value,
|
() => formData.value.birthDate,
|
||||||
async (id) => {
|
(birthDate) => {
|
||||||
fetchBrOption(id);
|
if (birthDate) {
|
||||||
brId.value = '';
|
calculateAge(birthDate);
|
||||||
userOption.value.brOpts = [];
|
}
|
||||||
},
|
},
|
||||||
);
|
);
|
||||||
</script>
|
</script>
|
||||||
|
|
@ -377,6 +418,7 @@ watch(
|
||||||
|
|
||||||
<!-- form -->
|
<!-- form -->
|
||||||
<FormDialog
|
<FormDialog
|
||||||
|
ref="formDialogRef"
|
||||||
v-model:modal="modal"
|
v-model:modal="modal"
|
||||||
v-model:address="formData.address"
|
v-model:address="formData.address"
|
||||||
v-model:addressEN="formData.addressEN"
|
v-model:addressEN="formData.addressEN"
|
||||||
|
|
@ -405,6 +447,7 @@ watch(
|
||||||
v-model="hqId"
|
v-model="hqId"
|
||||||
:options="userOption.hqOpts"
|
:options="userOption.hqOpts"
|
||||||
:rules="[(val: string) => !!val || 'กรุณาเลือกสำนักงานใหญ่']"
|
:rules="[(val: string) => !!val || 'กรุณาเลือกสำนักงานใหญ่']"
|
||||||
|
@update:model-value="(val: string) => selectHq(val)"
|
||||||
/>
|
/>
|
||||||
<q-select
|
<q-select
|
||||||
dense
|
dense
|
||||||
|
|
@ -448,7 +491,7 @@ watch(
|
||||||
option-label="label"
|
option-label="label"
|
||||||
option-value="value"
|
option-value="value"
|
||||||
v-model="formData.userRole"
|
v-model="formData.userRole"
|
||||||
:options="genderOpts"
|
:options="userOption.roleOpts"
|
||||||
/>
|
/>
|
||||||
<!-- :rules="[(val: string) => !!val || 'กรุณาเลือกสิทธิ์ผู้ใช้งาน']" -->
|
<!-- :rules="[(val: string) => !!val || 'กรุณาเลือกสิทธิ์ผู้ใช้งาน']" -->
|
||||||
<q-input
|
<q-input
|
||||||
|
|
@ -476,13 +519,31 @@ watch(
|
||||||
<template #prepend>
|
<template #prepend>
|
||||||
<div class="q-pl-md text-center">
|
<div class="q-pl-md text-center">
|
||||||
<div class="upload-img-preview">
|
<div class="upload-img-preview">
|
||||||
|
<q-img
|
||||||
|
v-if="urlProfile"
|
||||||
|
:src="urlProfile"
|
||||||
|
style="object-fit: cover; width: 100%; height: 100%"
|
||||||
|
/>
|
||||||
<q-icon
|
<q-icon
|
||||||
|
v-else
|
||||||
name="mdi-account full-height"
|
name="mdi-account full-height"
|
||||||
size="3vw"
|
size="3vw"
|
||||||
style="color: var(--border-color)"
|
style="color: var(--border-color)"
|
||||||
/>
|
/>
|
||||||
</div>
|
</div>
|
||||||
<q-btn
|
<q-btn
|
||||||
|
v-if="urlProfile"
|
||||||
|
dense
|
||||||
|
unelevated
|
||||||
|
outlined
|
||||||
|
padding="8px"
|
||||||
|
icon="mdi-pencil-outline"
|
||||||
|
class="edit-img-btn q-my-md full-width"
|
||||||
|
label="แก้ไขโปรไฟล์"
|
||||||
|
@click="inputFile.click()"
|
||||||
|
/>
|
||||||
|
<q-btn
|
||||||
|
v-else
|
||||||
dense
|
dense
|
||||||
unelevated
|
unelevated
|
||||||
outlined
|
outlined
|
||||||
|
|
@ -503,13 +564,6 @@ watch(
|
||||||
/>
|
/>
|
||||||
<span>สถานะผู้ใช้งาน</span>
|
<span>สถานะผู้ใช้งาน</span>
|
||||||
</div>
|
</div>
|
||||||
<q-file
|
|
||||||
dense
|
|
||||||
outlined
|
|
||||||
ref="file"
|
|
||||||
style="visibility: hidden"
|
|
||||||
v-model="formData.profileImage"
|
|
||||||
/>
|
|
||||||
</div>
|
</div>
|
||||||
</template>
|
</template>
|
||||||
|
|
||||||
|
|
@ -669,7 +723,7 @@ watch(
|
||||||
<VueDatePicker
|
<VueDatePicker
|
||||||
utc
|
utc
|
||||||
autoApply
|
autoApply
|
||||||
v-model="formData.retireDate"
|
v-model="formData.birthDate"
|
||||||
:locale="'th'"
|
:locale="'th'"
|
||||||
:enableTimePicker="false"
|
:enableTimePicker="false"
|
||||||
class="col-3"
|
class="col-3"
|
||||||
|
|
@ -681,7 +735,7 @@ watch(
|
||||||
<q-input
|
<q-input
|
||||||
dense
|
dense
|
||||||
outlined
|
outlined
|
||||||
label="วันที่พ้นสภาพพนักงาน"
|
label="วันเดือนปีเกิด"
|
||||||
:model-value="dateFormat(formData.birthDate)"
|
:model-value="dateFormat(formData.birthDate)"
|
||||||
>
|
>
|
||||||
<template v-slot:prepend>
|
<template v-slot:prepend>
|
||||||
|
|
@ -694,17 +748,24 @@ watch(
|
||||||
</template>
|
</template>
|
||||||
<template v-slot:append>
|
<template v-slot:append>
|
||||||
<q-icon
|
<q-icon
|
||||||
v-if="formData.retireDate"
|
v-if="formData.birthDate"
|
||||||
name="mdi-close"
|
name="mdi-close"
|
||||||
class="cursor-pointer"
|
class="cursor-pointer"
|
||||||
size="xs"
|
size="xs"
|
||||||
@click="formData.retireDate = undefined"
|
@click="formData.birthDate = undefined"
|
||||||
/>
|
/>
|
||||||
</template>
|
</template>
|
||||||
</q-input>
|
</q-input>
|
||||||
</template>
|
</template>
|
||||||
</VueDatePicker>
|
</VueDatePicker>
|
||||||
<q-input dense outlined label="อายุ" class="col-3" v-model="age" />
|
<q-input
|
||||||
|
dense
|
||||||
|
outlined
|
||||||
|
readonly
|
||||||
|
label="อายุ"
|
||||||
|
class="col-3"
|
||||||
|
v-model="age"
|
||||||
|
/>
|
||||||
<q-select
|
<q-select
|
||||||
v-if="formData.userType === 'MESSENGER'"
|
v-if="formData.userType === 'MESSENGER'"
|
||||||
dense
|
dense
|
||||||
|
|
@ -819,4 +880,11 @@ watch(
|
||||||
background-color: transparent;
|
background-color: transparent;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
.edit-img-btn {
|
||||||
|
color: hsl(var(--info-bg));
|
||||||
|
border: 1px solid hsl(var(--info-bg));
|
||||||
|
background-color: transparent;
|
||||||
|
border-radius: var(--radius-2);
|
||||||
|
}
|
||||||
</style>
|
</style>
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue