refactor(04): service new img dialog

This commit is contained in:
puriphatt 2024-09-12 11:22:11 +07:00
parent f1168c781f
commit 21ac39d538
4 changed files with 296 additions and 92 deletions

View file

@ -185,22 +185,32 @@ const useProductServiceStore = defineStore('api-product-service', () => {
return false;
}
async function createProduct(data: ProductCreate) {
async function createProduct(
data: ProductCreate,
imgList: {
selectedImage: string;
list: { url: string; imgFile: File | null; name: string }[];
},
) {
const { image, status, ...payload } = data;
const res = await api.post<ProductCreate & { imageUploadUrl: string }>(
'/product',
{
...payload,
},
);
image &&
(await axios
.put(res.data.imageUploadUrl, image, {
headers: { 'Content-Type': image.type },
onUploadProgress: (e) => console.log(e),
})
.catch((e) => console.error(e)));
const res = await api.post<ProductCreate>('/product', {
...payload,
selectedImage: imgList.selectedImage || '',
});
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,
'product',
);
}
}
if (!res) return false;
@ -234,14 +244,6 @@ const useProductServiceStore = defineStore('api-product-service', () => {
},
);
image &&
(await axios
.put(res.data.imageUploadUrl, image, {
headers: { 'Content-Type': image.type },
onUploadProgress: (e) => console.log(e),
})
.catch((e) => console.error(e)));
if (res.status === 200) {
return res.data;
}
@ -314,23 +316,35 @@ const useProductServiceStore = defineStore('api-product-service', () => {
return false;
}
async function createService(data: ServiceCreate) {
async function createService(
data: ServiceCreate,
imgList: {
selectedImage: string;
list: { url: string; imgFile: File | null; name: string }[];
},
) {
const { image, status, ...payload } = data;
const res = await api.post<ServiceCreate & { imageUploadUrl: string }>(
'/service',
{
...payload,
selectedImage: imgList.selectedImage || '',
},
);
image &&
(await axios
.put(res.data.imageUploadUrl, image, {
headers: { 'Content-Type': image.type },
onUploadProgress: (e) => console.log(e),
})
.catch((e) => console.error(e)));
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,
'service',
);
}
}
if (!res) return false;
@ -397,14 +411,6 @@ const useProductServiceStore = defineStore('api-product-service', () => {
},
);
image &&
(await axios
.put(res.data.imageUploadUrl, image, {
headers: { 'Content-Type': image.type },
onUploadProgress: (e) => console.log(e),
})
.catch((e) => console.error(e)));
if (!res) return false;
if (res.status === 200) {
@ -554,6 +560,67 @@ const useProductServiceStore = defineStore('api-product-service', () => {
}
}
async function fetchImageListById(
id: string,
type: 'product' | 'service',
flow?: {
sessionId?: string;
refTransactionId?: string;
transactionId?: string;
},
) {
const res = await api.get(`/${type}/${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,
userId: string,
name: string,
type: 'product' | 'service',
) {
await api
.put(`/${type}/${userId}/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,
type: 'product' | 'service',
flow?: {
sessionId?: string;
refTransactionId?: string;
transactionId?: string;
},
) {
const res = await api.delete(`/${type}/${id}/image/${name}`, {
headers: {
'X-Session-Id': flow?.sessionId,
'X-Rtid': flow?.refTransactionId || flowStore.rtid,
'X-Tid': flow?.transactionId,
},
});
if (!res) return false;
}
return {
workNameItems,
@ -587,6 +654,10 @@ const useProductServiceStore = defineStore('api-product-service', () => {
fetchListProductByIdWork,
fetchListOfWork,
fetchImageListById,
addImageList,
deleteImageByName,
};
});

View file

@ -10,6 +10,7 @@ export interface TreeProduct {
}
export interface Service {
selectedImage?: string;
productGroupId: string;
updatedAt: string;
updatedBy: UpdatedBy;
@ -47,6 +48,7 @@ export interface Work {
}
export interface ServiceCreate {
id?: string;
work: {
attributes: Attributes;
productId: string[];
@ -59,6 +61,7 @@ export interface ServiceCreate {
image?: File;
status?: Status;
productGroupId: string;
selectedImage?: string;
}
export interface Attributes {
@ -131,6 +134,7 @@ export interface WorkItems {
}
export interface ProductList {
selectedImage?: string;
expenseType: string;
vatIncluded: boolean;
remark: string;
@ -152,6 +156,8 @@ export interface ProductList {
}
export interface ProductCreate {
id?: string;
selectedImage?: string;
expenseType: string;
vatIncluded: boolean;
productGroupId: string;
@ -168,6 +174,7 @@ export interface ProductCreate {
}
export interface ProductUpdate {
selectedImage?: string;
productGroupId: string;
remark: string;
serviceCharge: number;