refactor: create payment file

This commit is contained in:
Thanaphon Frappet 2024-10-21 15:17:27 +07:00
parent 00a04edd1d
commit 5774dfcf5c
2 changed files with 47 additions and 61 deletions

View file

@ -9,11 +9,15 @@ import {
QuotationPayload,
QuotationPaymentData,
QuotationStats,
PaymentPayload,
} from './types';
import { PaginationResult } from 'src/types';
import { AxiosInstance } from 'axios';
import { AxiosInstance, AxiosProgressEvent } from 'axios';
import { baseUrl, manageAttachment } from '../utils';
export const useQuotationStore = defineStore('quotation-store', () => {
const base = ref<string>('');
const data = ref<Quotation[]>([]);
const page = ref<number>(1);
const pageMax = ref<number>(1);
@ -129,61 +133,6 @@ export const useQuotationStore = defineStore('quotation-store', () => {
};
});
function managePaymentFile(api: AxiosInstance) {
return {
listFile: async (opts: { quotationId: string; paymentId: string }) => {
const res = await api.get<string[]>(`/${base}/${opts.quotationId}/file`);
if (res.status >= 400) return false;
return res.data;
},
getFile: async (opts: {
group: T;
parentId: string;
fileId: string;
download?: boolean;
}) => {
const url = `/${base}/${opts.parentId}/file-${opts.group}/${opts.fileId}`;
const res = await api.get<string>(url);
if (opts.download) {
fetch(res.data)
.then(async (res) => await res.blob())
.then((blob) => {
const a = document.createElement('a');
a.download = opts.fileId;
a.href = window.URL.createObjectURL(blob);
a.click();
a.remove();
});
}
return res.data;
},
putFile: async (opts: {
group: T;
parentId: string;
fileId: string;
file: File;
}) => {
const res = await api.put(
`/${base}/${opts.parentId}/file-${opts.group}/${opts.fileId}`,
opts.file,
{
headers: { 'Content-Type': opts.file.type },
onUploadProgress: (e) => console.log(e),
},
);
if (res.status < 400) return true;
return false;
},
delFile: async (opts: { group: T; parentId: string; fileId: string }) => {
const res = await api.delete(
`/${base}/${opts.parentId}/file-${opts.group}/${opts.fileId}`,
);
if (res.status < 400) return true;
return false;
},
};
}
export const useQuotationPayment = defineStore('quotation-payment', () => {
async function getQuotationPayment(quotationId: string) {
const res = await api.get<
@ -195,7 +144,32 @@ export const useQuotationPayment = defineStore('quotation-payment', () => {
return null;
}
async function updateQuotationPayment(
quotationId: string,
paymentId: string,
payload: PaymentPayload,
) {
const res = await api.put<PaymentPayload & { id: string }>(
`/quotation/${quotationId}/payment/${paymentId}`,
payload,
);
if (res.status < 400) {
return res.data;
}
return null;
}
function createPaymentFileManager(
api: AxiosInstance,
quotationId: string,
opts?: { onUploadProgress: (e: AxiosProgressEvent) => void },
) {
return manageAttachment(api, `/quotation/${quotationId}/payment`, opts);
}
return {
getQuotationPayment,
createPaymentFileManager,
updateQuotationPayment,
};
});

View file

@ -5,7 +5,7 @@ import { ComposerTranslation } from 'vue-i18n';
import { defineStore } from 'pinia';
import { Ref, ref } from 'vue';
import { getRole } from 'src/services/keycloak';
import { AxiosInstance } from 'axios';
import { AxiosInstance, AxiosProgressEvent } from 'axios';
export const baseUrl = import.meta.env.VITE_API_BASE_URL;
@ -251,7 +251,11 @@ export function resetScrollBar(elementId: string) {
}
}
export function manageAttachment(api: AxiosInstance, base: string) {
export function manageAttachment(
api: AxiosInstance,
base: string,
option?: { onUploadProgress?: (e: AxiosProgressEvent) => void },
) {
return {
listAttachment: async (opts: { parentId: string }) => {
const res = await api.get<string[]>(
@ -290,7 +294,9 @@ export function manageAttachment(api: AxiosInstance, base: string) {
opts.file,
{
headers: { 'Content-Type': opts.file.type },
onUploadProgress: (e) => console.log(e),
onUploadProgress: option?.onUploadProgress
? option.onUploadProgress
: (e) => console.log(e),
},
);
if (res.status < 400) return true;
@ -306,7 +312,11 @@ export function manageAttachment(api: AxiosInstance, base: string) {
};
}
export function manageFile<T extends string>(api: AxiosInstance, base: string) {
export function manageFile<T extends string>(
api: AxiosInstance,
base: string,
option?: { onUploadProgress?: (e: AxiosProgressEvent) => void },
) {
return {
listFile: async (opts: { group: T; parentId: string }) => {
const res = await api.get<string[]>(
@ -347,7 +357,9 @@ export function manageFile<T extends string>(api: AxiosInstance, base: string) {
opts.file,
{
headers: { 'Content-Type': opts.file.type },
onUploadProgress: (e) => console.log(e),
onUploadProgress: option?.onUploadProgress
? option.onUploadProgress
: (e) => console.log(e),
},
);
if (res.status < 400) return true;