refactor: employee new img dialog

This commit is contained in:
puriphatt 2024-09-11 16:53:08 +07:00
parent 893b0a8c2a
commit 685ad0cc54
7 changed files with 254 additions and 57 deletions

View file

@ -63,25 +63,81 @@ const useEmployeeStore = defineStore('api-employee', () => {
return false;
}
async function create(data: EmployeeCreate) {
async function fetchImageListById(
id: string,
flow?: {
sessionId?: string;
refTransactionId?: string;
transactionId?: string;
},
) {
const res = await api.get(`/employee/${id}/image`, {
headers: {
'X-Session-Id': flow?.sessionId,
'X-Rtid': flow?.refTransactionId || flowStore.rtid,
'X-Tid': flow?.transactionId,
},
});
if (!res) return false;
if (res.status === 200) return res.data;
if (res.status === 204) return null;
return false;
}
async function addImageList(file: File, customerId: string, name: string) {
await api
.put(`/employee/${customerId}/image/${name}`, file, {
headers: { 'Content-Type': file.type },
onUploadProgress: (e) => console.log(e),
})
.catch((e) => console.error(e));
return name;
}
async function deleteImageByName(
id: string,
name: string,
flow?: {
sessionId?: string;
refTransactionId?: string;
transactionId?: string;
},
) {
const res = await api.delete(`/employee/${id}/image/${name}`, {
headers: {
'X-Session-Id': flow?.sessionId,
'X-Rtid': flow?.refTransactionId || flowStore.rtid,
'X-Tid': flow?.transactionId,
},
});
if (!res) return false;
}
async function create(
data: EmployeeCreate,
imgList: {
selectedImage: string;
list: { url: string; imgFile: File | null; name: string }[];
},
) {
const { id, code, image, file, zipCode, ...payload } = data;
const res = await api.post<
Employee & { profileImageUrl: string; profileImageUploadUrl: string }
>('/employee', payload, {
headers: { 'X-Rtid': flowStore.rtid },
});
>(
'/employee',
{ ...payload, selectedImage: imgList.selectedImage },
{
headers: { 'X-Rtid': flowStore.rtid },
},
);
if (!res) return false;
if (res.data.id) {
if (image) {
await api
.put(`/employee/${res.data.id}/image`, image, {
headers: { 'Content-Type': image?.type },
onUploadProgress: (e) => console.log(e),
})
.catch((e) => console.error(e));
}
if (file) {
const attachmentUpload = file.map(async ({ group, file }) => {
if (file) {
@ -97,6 +153,13 @@ const useEmployeeStore = defineStore('api-employee', () => {
});
await Promise.all(attachmentUpload);
}
if (imgList.list.length > 0 && res.data.id) {
for (let index = 0; index < imgList.list.length; index++) {
const imgFile = imgList.list[index].imgFile;
if (imgFile)
await addImageList(imgFile, res.data.id, imgList.list[index].name);
}
}
}
return res.data;
@ -392,7 +455,7 @@ const useEmployeeStore = defineStore('api-employee', () => {
fetch(res.data)
.then(async (res) => await res.blob())
.then((blob) => {
let a = document.createElement('a');
const a = document.createElement('a');
a.download = filename;
a.href = window.URL.createObjectURL(blob);
a.click();
@ -434,6 +497,11 @@ const useEmployeeStore = defineStore('api-employee', () => {
fetchById,
fetchList,
fetchImageListById,
addImageList,
deleteImageByName,
create,
editById,
deleteById,

View file

@ -5,6 +5,7 @@ import { User } from '../user/types';
import { Customer, CustomerBranch } from '../customer/types';
export type Employee = {
selectedImage?: string;
id: string;
code: string;
nrcNo: string;
@ -50,7 +51,6 @@ export type Employee = {
subDistrict: SubDistrict | null;
district: District | null;
province: Province | null;
profileImageUrl: string | null;
customerBranch: CustomerBranch & { customer: Customer };
employeeWork?: EmployeeWorkCreate[];
employeeCheckup?: EmployeeCheckupCreate[];
@ -58,6 +58,7 @@ export type Employee = {
};
export type EmployeeCreate = {
selectedImage?: string;
id?: string;
code: string;
image: File | null;