fix: user type and store

This commit is contained in:
puriphatt 2024-04-11 22:20:28 +07:00
parent 8b0c0a33ac
commit cf215ba717
2 changed files with 53 additions and 4 deletions

View file

@ -12,8 +12,11 @@ import {
UserOption,
} from './types';
import axios from 'axios';
import useBranchStore from '../branch';
import { Branch } from '../branch/types';
const branchStore = useBranchStore()
const useUserStore = defineStore('api-user', () => {
const userOption = ref<UserOption>({
hqOpts: [],
@ -56,8 +59,50 @@ const useUserStore = defineStore('api-user', () => {
{ label: 'สถานที่อบรมแรงงานกัมพูชา-บ้านแหลม จ.จันทบุร' },
],
});
const data = ref<Pagination<User[]>>();
function calculateAge(birthDate: Date | null): string {
if (!birthDate) return '';
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 result = `${years} ปี`
return result;
}
async function fetchHqOption() {
if (userOption.value.hqOpts.length === 0) {
const res = await branchStore.fetchList({ pageSize: 999, filter: 'head' });
if (res) {
res.result.map((item) => {
userOption.value.hqOpts.push({
label: item.code,
value: item.id,
});
});
}
}
}
async function fetchBrOption(id: string) {
const res = await branchStore.fetchById(id, {
includeSubBranch: true,
});
if (res && res?.branch) {
res.branch.map((item) => {
userOption.value.brOpts.push({
label: item.code,
value: item.id,
});
});
}
}
async function fetchRoleOption() {
const res = await api.get<RoleData[]>('/keycloak/role');
res.data.map((item) => {
@ -429,6 +474,10 @@ const useUserStore = defineStore('api-user', () => {
return {
data,
userOption,
calculateAge,
fetchHqOption,
fetchBrOption,
fetchRoleOption,
fetchList,

View file

@ -14,15 +14,15 @@ export type User = {
trainingPlace: string | null;
importNationality: string | null;
sourceNationality: string | null;
licenseExpireDate: string | null;
licenseIssueDate: string | null;
licenseExpireDate: Date | null;
licenseIssueDate: Date | null;
licenseNo: string | null;
discountCondition: string | null;
gender: string;
userRole: string;
userType: string;
retireDate: string | null;
startDate: string | null;
retireDate: Date | null;
startDate: Date | null;
registrationNo: string | null;
telephoneNo: string;
email: string;