Files
2025-07-21 10:14:27 +07:00

112 lines
3.2 KiB
TypeScript

import { defineStore } from "pinia";
const runtime = useRuntimeConfig().public;
export const useSettingStore = defineStore("setting", {
state: (): any => ({
typeUser: [],
listTypeUser: [],
listMenu: [],
roleMenuById: [],
message: "",
}),
actions: {
async getTypeUser(body: Record<string, any>) {
// console.log("ini di pinia:",body)
this.typeUser = body;
},
async getMenu() {
try {
const response = await $fetch(`${runtime.APIBASE}/menu/getlist`, {
method: "GET",
headers: { "Content-Type": "application/json" },
});
console.log("dipinia ini responnya:", response);
this.listMenu = response as [];
} catch (error) {
if (error instanceof Error) {
this.message = error.message;
// console.log(error.message)
}
}
},
async getRoleMenuById(body: Record<string, any>) {
try {
console.log(body);
const response = await $fetch(
`${runtime.APIBASE}/menu/role/type/${body}`,
{
headers: {
"Content-Type": "application/json",
},
method: "GET",
}
);
this.roleMenuById = response as [];
} catch (error) {
if (error instanceof Error) {
this.message = error.message;
// console.log(error.message)
}
}
},
async getAllTypeUser() {
try {
const response = await $fetch(`${runtime.APIBASE}/menu/type`, {
method: "GET",
headers: { "Content-Type": "application/json" },
});
this.listTypeUser = response as [];
} catch (error) {
if (error instanceof Error) {
this.message = error.message;
// console.log(error.message)
}
}
},
async postRoleUserMenu(body: Record<string, any>) {
try {
const roleUserMenu = body.menus.map((item) => ({
type_user_id: item.type_user_id,
menu_id: item.menu_id,
access: [
{
add: parseInt(item.access[0].value),
update: parseInt(item.access[1].value),
read: parseInt(item.access[2].value),
delete: parseInt(item.access[3].value),
},
],
}));
console.log("body post:", body.menus);
const response = await $fetch(
`${runtime.APIBASE}/menu/update/rolemenu?id=${body.menus[0].type_user_id}`,
{
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify(roleUserMenu),
}
);
} catch (error) {
if (error instanceof Error) {
this.message = error.message;
// console.log(error.message)
}
}
},
// async addTypeUser(data: any) {
// console.log(data)
// try {
// const response = await $fetch('https://',{
// method: 'POST',
// headers: {'Content-Type': 'application/json'},
// body: JSON.stringify(data)
// })
// } catch (error) {
// if(error instanceof Error){
// this.message = error.message
// // console.log(error.message)
// }
// }
// }
},
});