import {defineStore} from "pinia"; import type {MessagesHandler} from "~/types/messagesHandler"; const runtime = useRuntimeConfig().public; const messages = ref({}); const typeUser = ref([]); export const useSettingStore = defineStore("SettingStore", () => { messages.value = {} const listMenu = ref([]); const getMenu = async () => { await $fetch(`${runtime.APIBASE}/menu/getlist`, { // await $fetch("/api/setting/getListMenu", { method: "GET", // mode: "no-cors", headers: {"Content-Type": "application/json"} }) .then((res) => { listMenu.value = res // console.log("menu pinia :",listMenu.value); }); }; const roleMenuById = ref([]); const getRoleMenuById = async (body: Record) => { typeUser.value = body; console.log("rolemenu byid",body) // await $fetch(`/api/setting/getRoleMenu`, { await $fetch(`${runtime.APIBASE}/menu/role/type/${body}`, { // mode: "no-cors", headers: { "Content-Type": "application/json", }, method: "GET", // body: JSON.stringify(body), }).then((res) => { roleMenuById.value = res; }); console.log("rolemenu byid",roleMenuById.value) }; const listTypeUser = ref([]); const getTypeUser = async () => { // await $fetch("/api/setting/getTipeUser", { await $fetch(`${runtime.APIBASE}/menu/type`, { method: "GET", headers: { "Content-Type": "application/json", }, // mode: 'no-cors', // headers: { // 'Access-Control-Allow-Origin': '*', // Accept: 'application/json', // 'Content-Type': 'application/json', // }, }) .then((res) => { listTypeUser.value = res }); }; const postRoleUserMenu = async (body: Record) => { 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) // await $fetch(`/api/setting/deleteRoleMenuUser`, { // method: 'POST', // headers: {'Content-Type': 'application/json'}, // body: JSON.stringify({key: body.menus[0].type_user_id}) // }).then(async (res) => { // await $fetch(`/api/setting/postRoleMenuUser`, { 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) }) .then((res) => { messages.value.type = 'success' messages.value.title = 'Akses User Sudah Disimpan' }) .catch((err) => { console.log(err) messages.value.type = 'error' messages.value.title = 'Akses User Tidak Berhasil Disimpan' }) // }) } return { getMenu, getRoleMenuById, getTypeUser, postRoleUserMenu, messages, listMenu, roleMenuById, typeUser, listTypeUser }; })