refactor: api store

This commit is contained in:
Methapon Metanipat 2024-09-17 11:41:09 +07:00
parent 6f54693eb1
commit 66b167dea5
2 changed files with 169 additions and 264 deletions

View file

@ -4,6 +4,7 @@ import { ComposerTranslation, useI18n } from 'vue-i18n';
import { defineStore } from 'pinia';
import { Ref, ref } from 'vue';
import { getRole } from 'src/services/keycloak';
import { AxiosInstance } from 'axios';
export const baseUrl = import.meta.env.VITE_API_BASE_URL;
@ -234,4 +235,110 @@ export function resetScrollBar(elementId: string) {
}
}
export function manageAttachment(api: AxiosInstance, base: string) {
return {
listAttachment: async () => {
const res = await api.get<string[]>(`${base}/attachment`);
if (res.status >= 400) return false;
return res.data;
},
getAttachment: async (opts: {
parentId: string;
name: string;
download?: boolean;
}) => {
const url = `/${opts.parentId}/attachment/${opts.name}`;
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.name;
a.href = window.URL.createObjectURL(blob);
a.click();
a.remove();
});
}
return res.data;
},
putAttachment: async (opts: {
parentId: string;
name: string;
file: File;
}) => {
const res = await api.put(
`/${opts.parentId}/attachment/${opts.name}`,
opts.file,
{
headers: { 'Content-Type': opts.file.type },
onUploadProgress: (e) => console.log(e),
},
);
if (res.status < 400) return true;
return false;
},
delAttachment: async (opts: { parentId: string; name: string }) => {
const res = await api.delete(`/${opts.parentId}/attachment/${opts.name}`);
if (res.status < 400) return true;
return false;
},
};
}
export function manageFile<T extends string>(api: AxiosInstance, base: string) {
return {
listFile: async (opts: { group: T }) => {
const res = await api.get<string[]>(`${base}/file-${opts.group}`);
if (res.status >= 400) return false;
return res.data;
},
getFile: async (opts: {
group: T;
parentId: string;
fileId: string;
download?: boolean;
}) => {
const url = `/${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(
`/${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(
`/${opts.parentId}/file-${opts.group}/${opts.fileId}`,
);
if (res.status < 400) return true;
return false;
},
};
}
export default useUtilsStore;