test
This commit is contained in:
64
composables/sidebarMenu/mergeSidebar.ts
Normal file
64
composables/sidebarMenu/mergeSidebar.ts
Normal file
@@ -0,0 +1,64 @@
|
||||
|
||||
import type { SidebarItem } from '~/types/menuAkses/sidebar'
|
||||
import type { PageAccess } from '~/types/menuAkses/access'
|
||||
interface SidebarItemWithPermissions extends SidebarItem {
|
||||
permissions: string[];
|
||||
}
|
||||
|
||||
export function mergeSidebar(menuItems: SidebarItem[], allowedPages: PageAccess[]): SidebarItem[] {
|
||||
const result: SidebarItem[] = []
|
||||
|
||||
for (const item of menuItems) {
|
||||
// Jika punya anak (children)
|
||||
//console.log('item', item);
|
||||
//console.log('allowedPages', allowedPages);
|
||||
if (Array.isArray(item.children)) {
|
||||
const filteredChildren = mergeSidebar(item.children, allowedPages)
|
||||
//console.log('ada array item', item);
|
||||
|
||||
// console.log('filteredChildren', filteredChildren);
|
||||
if (filteredChildren.length > 0) {
|
||||
result.push({
|
||||
...item,
|
||||
children: filteredChildren
|
||||
})
|
||||
}
|
||||
//console.log('result', result);
|
||||
}
|
||||
|
||||
// Jika punya path, cocokkan dengan json dummy / backend
|
||||
else if (item.to) {
|
||||
const matchPage = allowedPages.find(page => page.title === item.title && page.path === item.to) //cocokkan title dan path link halaman
|
||||
|
||||
//console.log('match', matchPage,matchPage?.permissions);
|
||||
if (matchPage && matchPage.permissions?.includes('view')) {
|
||||
result.push({
|
||||
...item,
|
||||
permissions: matchPage?.permissions,
|
||||
|
||||
} as SidebarItemWithPermissions)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return result
|
||||
}
|
||||
|
||||
export function mergeSidebarIcon(staticIcon: any[], IconAccess: any[]) {
|
||||
const result : any[] = []
|
||||
|
||||
for (const item of staticIcon) {
|
||||
const matchIcon = IconAccess.find(icon => icon.id === item.id)
|
||||
//console.log('matchIcon', matchIcon);
|
||||
if (matchIcon) {
|
||||
result.push({
|
||||
...item,
|
||||
|
||||
})
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
return result
|
||||
}
|
||||
|
||||
47
composables/sidebarMenu/useSidebarAkses.ts
Normal file
47
composables/sidebarMenu/useSidebarAkses.ts
Normal file
@@ -0,0 +1,47 @@
|
||||
import { useHakAksesStore } from '@/stores/sidebarMenuAkses/useMenuAksesStore'
|
||||
import { mergeSidebar, mergeSidebarIcon } from '~/composables/sidebarMenu/mergeSidebar';
|
||||
import sidebarRole from "@/stores/rolePages"; // data dummy
|
||||
import sidebarItems from '~/components/layout/full/vertical-sidebar/sidebarItem'; //sidebar static dari template
|
||||
import MiniSideIcons from '~/components/layout/full/vertical-sidebar/MinIconItems';
|
||||
|
||||
|
||||
export function useSidebarAccess(data: any) {
|
||||
const sidebarMenu = shallowRef(sidebarItems);
|
||||
const sidebarMiniSideIcons = shallowRef(MiniSideIcons);
|
||||
|
||||
const userRole = "admin";
|
||||
const roleSidebarItems = sidebarRole.filter(item => item.role === userRole);
|
||||
const mergedPages = mergeSidebar(sidebarMenu.value, roleSidebarItems[0].pages)
|
||||
data = mergeSidebarIcon(sidebarMiniSideIcons.value, roleSidebarItems[0].pages)
|
||||
console.log('mergedPages useSidebarAccess', mergedPages);
|
||||
|
||||
return data
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
// export function useAccess() {
|
||||
|
||||
// const store = useHakAksesStore()
|
||||
// const getRole = () => store.role
|
||||
// const getPages = () => store.pageAccess
|
||||
|
||||
// const getPermissionsByPath = (path: string): string[] => {
|
||||
// const match = store.pageAccess.find(p => p.path === path)
|
||||
// return match?.permissions || []
|
||||
// }
|
||||
|
||||
// return {
|
||||
// getRole,
|
||||
// getPages,
|
||||
// getPermissionsByPath
|
||||
// }
|
||||
// }
|
||||
10
composables/useAccess.ts
Normal file
10
composables/useAccess.ts
Normal file
@@ -0,0 +1,10 @@
|
||||
//import { sidebarMeta } from '@/components/layout/sidebar'
|
||||
import { mergeSidebar } from '~/composables/sidebarMenu/mergeSidebar'
|
||||
import { useSidebarAccess } from '~/composables/sidebarMenu/useSidebarAkses'
|
||||
|
||||
export const useAccess = () => {
|
||||
const result = useSidebarAccess("tes")
|
||||
|
||||
|
||||
return result
|
||||
}
|
||||
25
composables/useAuth123.ts
Normal file
25
composables/useAuth123.ts
Normal file
@@ -0,0 +1,25 @@
|
||||
// // composables/useAuth.ts
|
||||
// import { useCookie } from '#app'
|
||||
|
||||
// export const useAuth = () => {
|
||||
// const user = useState('auth_user', () => null)
|
||||
// const token = useCookie('auth_token')
|
||||
|
||||
// const setUser = (data: any, tokenValue: string) => {
|
||||
// user.value = data
|
||||
// token.value = tokenValue
|
||||
// }
|
||||
|
||||
// const logout = () => {
|
||||
// user.value = null
|
||||
// token.value = null
|
||||
// }
|
||||
|
||||
// return {
|
||||
// user,
|
||||
// token,
|
||||
// setUser,
|
||||
// logout,
|
||||
// isLoggedIn: computed(() => !!user.value)
|
||||
// }
|
||||
// }
|
||||
Reference in New Issue
Block a user