refactor(frontend): moved types to global types and successfull build

This commit is contained in:
2025-03-03 06:32:46 -06:00
parent 89a2b3ea9e
commit f3b92e94e3
11 changed files with 91 additions and 50 deletions

View File

@@ -15,7 +15,6 @@ export function AppSidebar() {
const {user} = useSessionStore();
const {modules} = useModuleStore();
console.log(user);
return (
<Sidebar collapsible="icon">
<SidebarContent>

View File

@@ -1,14 +1,17 @@
import {QueryClient, QueryClientProvider} from "@tanstack/react-query";
import {useModuleStore} from "../../lib/store/useModuleStore";
import {useEffect} from "react";
import {useGetUserRoles} from "@/lib/store/useGetRoles";
const queryClient = new QueryClient();
export const SessionProvider = ({children}: {children: React.ReactNode}) => {
const {fetchModules} = useModuleStore();
const {fetchUserRoles} = useGetUserRoles();
useEffect(() => {
fetchModules();
fetchUserRoles();
}, []);
return <QueryClientProvider client={queryClient}>{children}</QueryClientProvider>;
};

View File

@@ -1,17 +1,6 @@
import {User} from "@/types/users";
import {create} from "zustand";
type User = {
user_id: string;
email: string;
username: string;
roles: keyof Roles[];
role: string;
};
interface Roles {
role: string;
}
export type SessionState = {
user: User | null;
token: string | null;

View File

@@ -1,17 +1,6 @@
import {create} from "zustand";
import {useSessionStore} from "./sessionStore";
//import useSWR from "swr";
interface Modules {
module_id: string;
name: string;
active: boolean;
roles: string;
add_user: string;
add_date: Date;
upd_user: string;
upd_date: Date;
}
import {Modules} from "@/types/modules";
interface SettingState {
userRoles: Modules[];
@@ -23,7 +12,7 @@ interface FetchModulesResponse {
data: Modules[];
}
export const useModuleStore = create<SettingState>()((set) => ({
export const useGetUserRoles = create<SettingState>()((set) => ({
userRoles: [],
setUserRoles: (userRoles) => set({userRoles}),
fetchUserRoles: async () => {

View File

@@ -1,17 +1,5 @@
import {Modules} from "@/types/modules";
import {create} from "zustand";
//import useSWR from "swr";
// interface Modules {
// module_id: string;
// name: string;
// active: boolean;
// roles: string;
// add_user: string;
// add_date: Date;
// upd_user: string;
// upd_date: Date;
// }
interface SettingState {
modules: Modules[];

View File

@@ -19,6 +19,7 @@ import { Route as IndexImport } from './routes/index'
import { Route as OcpIndexImport } from './routes/ocp/index'
import { Route as OcpLotsImport } from './routes/ocp/lots'
import { Route as AuthProfileImport } from './routes/_auth/profile'
import { Route as AdminModulesImport } from './routes/_admin/modules'
// Create/Update Routes
@@ -68,6 +69,12 @@ const AuthProfileRoute = AuthProfileImport.update({
getParentRoute: () => AuthRoute,
} as any)
const AdminModulesRoute = AdminModulesImport.update({
id: '/modules',
path: '/modules',
getParentRoute: () => AdminRoute,
} as any)
// Populate the FileRoutesByPath interface
declare module '@tanstack/react-router' {
@@ -107,6 +114,13 @@ declare module '@tanstack/react-router' {
preLoaderRoute: typeof LoginImport
parentRoute: typeof rootRoute
}
'/_admin/modules': {
id: '/_admin/modules'
path: '/modules'
fullPath: '/modules'
preLoaderRoute: typeof AdminModulesImport
parentRoute: typeof AdminImport
}
'/_auth/profile': {
id: '/_auth/profile'
path: '/profile'
@@ -133,6 +147,16 @@ declare module '@tanstack/react-router' {
// Create and export the route tree
interface AdminRouteChildren {
AdminModulesRoute: typeof AdminModulesRoute
}
const AdminRouteChildren: AdminRouteChildren = {
AdminModulesRoute: AdminModulesRoute,
}
const AdminRouteWithChildren = AdminRoute._addFileChildren(AdminRouteChildren)
interface AuthRouteChildren {
AuthProfileRoute: typeof AuthProfileRoute
}
@@ -148,6 +172,7 @@ export interface FileRoutesByFullPath {
'': typeof AuthRouteWithChildren
'/about': typeof AboutRoute
'/login': typeof LoginRoute
'/modules': typeof AdminModulesRoute
'/profile': typeof AuthProfileRoute
'/ocp/lots': typeof OcpLotsRoute
'/ocp': typeof OcpIndexRoute
@@ -158,6 +183,7 @@ export interface FileRoutesByTo {
'': typeof AuthRouteWithChildren
'/about': typeof AboutRoute
'/login': typeof LoginRoute
'/modules': typeof AdminModulesRoute
'/profile': typeof AuthProfileRoute
'/ocp/lots': typeof OcpLotsRoute
'/ocp': typeof OcpIndexRoute
@@ -166,10 +192,11 @@ export interface FileRoutesByTo {
export interface FileRoutesById {
__root__: typeof rootRoute
'/': typeof IndexRoute
'/_admin': typeof AdminRoute
'/_admin': typeof AdminRouteWithChildren
'/_auth': typeof AuthRouteWithChildren
'/about': typeof AboutRoute
'/login': typeof LoginRoute
'/_admin/modules': typeof AdminModulesRoute
'/_auth/profile': typeof AuthProfileRoute
'/ocp/lots': typeof OcpLotsRoute
'/ocp/': typeof OcpIndexRoute
@@ -177,9 +204,25 @@ export interface FileRoutesById {
export interface FileRouteTypes {
fileRoutesByFullPath: FileRoutesByFullPath
fullPaths: '/' | '' | '/about' | '/login' | '/profile' | '/ocp/lots' | '/ocp'
fullPaths:
| '/'
| ''
| '/about'
| '/login'
| '/modules'
| '/profile'
| '/ocp/lots'
| '/ocp'
fileRoutesByTo: FileRoutesByTo
to: '/' | '' | '/about' | '/login' | '/profile' | '/ocp/lots' | '/ocp'
to:
| '/'
| ''
| '/about'
| '/login'
| '/modules'
| '/profile'
| '/ocp/lots'
| '/ocp'
id:
| '__root__'
| '/'
@@ -187,6 +230,7 @@ export interface FileRouteTypes {
| '/_auth'
| '/about'
| '/login'
| '/_admin/modules'
| '/_auth/profile'
| '/ocp/lots'
| '/ocp/'
@@ -195,7 +239,7 @@ export interface FileRouteTypes {
export interface RootRouteChildren {
IndexRoute: typeof IndexRoute
AdminRoute: typeof AdminRoute
AdminRoute: typeof AdminRouteWithChildren
AuthRoute: typeof AuthRouteWithChildren
AboutRoute: typeof AboutRoute
LoginRoute: typeof LoginRoute
@@ -205,7 +249,7 @@ export interface RootRouteChildren {
const rootRouteChildren: RootRouteChildren = {
IndexRoute: IndexRoute,
AdminRoute: AdminRoute,
AdminRoute: AdminRouteWithChildren,
AuthRoute: AuthRouteWithChildren,
AboutRoute: AboutRoute,
LoginRoute: LoginRoute,
@@ -236,7 +280,10 @@ export const routeTree = rootRoute
"filePath": "index.tsx"
},
"/_admin": {
"filePath": "_admin.tsx"
"filePath": "_admin.tsx",
"children": [
"/_admin/modules"
]
},
"/_auth": {
"filePath": "_auth.tsx",
@@ -250,6 +297,10 @@ export const routeTree = rootRoute
"/login": {
"filePath": "login.tsx"
},
"/_admin/modules": {
"filePath": "_admin/modules.tsx",
"parent": "/_admin"
},
"/_auth/profile": {
"filePath": "_auth/profile.tsx",
"parent": "/_auth"

View File

@@ -0,0 +1,9 @@
import { createFileRoute } from '@tanstack/react-router'
export const Route = createFileRoute('/_admin/modules')({
component: RouteComponent,
})
function RouteComponent() {
return <div>Hello "/_admin/modules"!</div>
}

View File

@@ -3,8 +3,8 @@ export interface Modules {
name: string;
active: boolean;
roles: string;
add_User: string;
add_Date: Date;
add_user: string;
add_date: Date;
upd_user: string;
upd_date: Date;
}

View File

@@ -0,0 +1,4 @@
export interface Roles {
role: string;
module_id: string;
}

View File

@@ -0,0 +1,9 @@
import {Roles} from "./roles";
export type User = {
user_id: string;
email: string;
username: string;
roles: Roles[];
role: string;
};

View File

@@ -4,12 +4,13 @@ type User = {
user_id: string;
email: string;
username: string;
roles: keyof Roles[];
roles: Roles[];
role: string;
};
interface Roles {
role: string;
module_id: string;
}
// user will need access to the module.
@@ -17,9 +18,8 @@ interface Roles {
export function hasAccess(user: User | null, moduleName: string | null, modules: Modules[]): boolean {
// get the modules for the id
const filteredModule = modules?.filter((f) => f.name === moduleName);
console.log(filteredModule);
//console.log(filteredModule[0].module_id);
// userroles and filter out by the module id,
console.log(user);
return false;
return user?.roles.find((role) => role.module_id === filteredModule[0].module_id) ? true : false;
}