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 {user} = useSessionStore();
const {modules} = useModuleStore(); const {modules} = useModuleStore();
console.log(user);
return ( return (
<Sidebar collapsible="icon"> <Sidebar collapsible="icon">
<SidebarContent> <SidebarContent>

View File

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

View File

@@ -1,17 +1,6 @@
import {User} from "@/types/users";
import {create} from "zustand"; 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 = { export type SessionState = {
user: User | null; user: User | null;
token: string | null; token: string | null;

View File

@@ -1,17 +1,6 @@
import {create} from "zustand"; import {create} from "zustand";
import {useSessionStore} from "./sessionStore"; import {useSessionStore} from "./sessionStore";
//import useSWR from "swr"; import {Modules} from "@/types/modules";
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 { interface SettingState {
userRoles: Modules[]; userRoles: Modules[];
@@ -23,7 +12,7 @@ interface FetchModulesResponse {
data: Modules[]; data: Modules[];
} }
export const useModuleStore = create<SettingState>()((set) => ({ export const useGetUserRoles = create<SettingState>()((set) => ({
userRoles: [], userRoles: [],
setUserRoles: (userRoles) => set({userRoles}), setUserRoles: (userRoles) => set({userRoles}),
fetchUserRoles: async () => { fetchUserRoles: async () => {

View File

@@ -1,17 +1,5 @@
import {Modules} from "@/types/modules"; import {Modules} from "@/types/modules";
import {create} from "zustand"; 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 { interface SettingState {
modules: Modules[]; 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 OcpIndexImport } from './routes/ocp/index'
import { Route as OcpLotsImport } from './routes/ocp/lots' import { Route as OcpLotsImport } from './routes/ocp/lots'
import { Route as AuthProfileImport } from './routes/_auth/profile' import { Route as AuthProfileImport } from './routes/_auth/profile'
import { Route as AdminModulesImport } from './routes/_admin/modules'
// Create/Update Routes // Create/Update Routes
@@ -68,6 +69,12 @@ const AuthProfileRoute = AuthProfileImport.update({
getParentRoute: () => AuthRoute, getParentRoute: () => AuthRoute,
} as any) } as any)
const AdminModulesRoute = AdminModulesImport.update({
id: '/modules',
path: '/modules',
getParentRoute: () => AdminRoute,
} as any)
// Populate the FileRoutesByPath interface // Populate the FileRoutesByPath interface
declare module '@tanstack/react-router' { declare module '@tanstack/react-router' {
@@ -107,6 +114,13 @@ declare module '@tanstack/react-router' {
preLoaderRoute: typeof LoginImport preLoaderRoute: typeof LoginImport
parentRoute: typeof rootRoute parentRoute: typeof rootRoute
} }
'/_admin/modules': {
id: '/_admin/modules'
path: '/modules'
fullPath: '/modules'
preLoaderRoute: typeof AdminModulesImport
parentRoute: typeof AdminImport
}
'/_auth/profile': { '/_auth/profile': {
id: '/_auth/profile' id: '/_auth/profile'
path: '/profile' path: '/profile'
@@ -133,6 +147,16 @@ declare module '@tanstack/react-router' {
// Create and export the route tree // Create and export the route tree
interface AdminRouteChildren {
AdminModulesRoute: typeof AdminModulesRoute
}
const AdminRouteChildren: AdminRouteChildren = {
AdminModulesRoute: AdminModulesRoute,
}
const AdminRouteWithChildren = AdminRoute._addFileChildren(AdminRouteChildren)
interface AuthRouteChildren { interface AuthRouteChildren {
AuthProfileRoute: typeof AuthProfileRoute AuthProfileRoute: typeof AuthProfileRoute
} }
@@ -148,6 +172,7 @@ export interface FileRoutesByFullPath {
'': typeof AuthRouteWithChildren '': typeof AuthRouteWithChildren
'/about': typeof AboutRoute '/about': typeof AboutRoute
'/login': typeof LoginRoute '/login': typeof LoginRoute
'/modules': typeof AdminModulesRoute
'/profile': typeof AuthProfileRoute '/profile': typeof AuthProfileRoute
'/ocp/lots': typeof OcpLotsRoute '/ocp/lots': typeof OcpLotsRoute
'/ocp': typeof OcpIndexRoute '/ocp': typeof OcpIndexRoute
@@ -158,6 +183,7 @@ export interface FileRoutesByTo {
'': typeof AuthRouteWithChildren '': typeof AuthRouteWithChildren
'/about': typeof AboutRoute '/about': typeof AboutRoute
'/login': typeof LoginRoute '/login': typeof LoginRoute
'/modules': typeof AdminModulesRoute
'/profile': typeof AuthProfileRoute '/profile': typeof AuthProfileRoute
'/ocp/lots': typeof OcpLotsRoute '/ocp/lots': typeof OcpLotsRoute
'/ocp': typeof OcpIndexRoute '/ocp': typeof OcpIndexRoute
@@ -166,10 +192,11 @@ export interface FileRoutesByTo {
export interface FileRoutesById { export interface FileRoutesById {
__root__: typeof rootRoute __root__: typeof rootRoute
'/': typeof IndexRoute '/': typeof IndexRoute
'/_admin': typeof AdminRoute '/_admin': typeof AdminRouteWithChildren
'/_auth': typeof AuthRouteWithChildren '/_auth': typeof AuthRouteWithChildren
'/about': typeof AboutRoute '/about': typeof AboutRoute
'/login': typeof LoginRoute '/login': typeof LoginRoute
'/_admin/modules': typeof AdminModulesRoute
'/_auth/profile': typeof AuthProfileRoute '/_auth/profile': typeof AuthProfileRoute
'/ocp/lots': typeof OcpLotsRoute '/ocp/lots': typeof OcpLotsRoute
'/ocp/': typeof OcpIndexRoute '/ocp/': typeof OcpIndexRoute
@@ -177,9 +204,25 @@ export interface FileRoutesById {
export interface FileRouteTypes { export interface FileRouteTypes {
fileRoutesByFullPath: FileRoutesByFullPath fileRoutesByFullPath: FileRoutesByFullPath
fullPaths: '/' | '' | '/about' | '/login' | '/profile' | '/ocp/lots' | '/ocp' fullPaths:
| '/'
| ''
| '/about'
| '/login'
| '/modules'
| '/profile'
| '/ocp/lots'
| '/ocp'
fileRoutesByTo: FileRoutesByTo fileRoutesByTo: FileRoutesByTo
to: '/' | '' | '/about' | '/login' | '/profile' | '/ocp/lots' | '/ocp' to:
| '/'
| ''
| '/about'
| '/login'
| '/modules'
| '/profile'
| '/ocp/lots'
| '/ocp'
id: id:
| '__root__' | '__root__'
| '/' | '/'
@@ -187,6 +230,7 @@ export interface FileRouteTypes {
| '/_auth' | '/_auth'
| '/about' | '/about'
| '/login' | '/login'
| '/_admin/modules'
| '/_auth/profile' | '/_auth/profile'
| '/ocp/lots' | '/ocp/lots'
| '/ocp/' | '/ocp/'
@@ -195,7 +239,7 @@ export interface FileRouteTypes {
export interface RootRouteChildren { export interface RootRouteChildren {
IndexRoute: typeof IndexRoute IndexRoute: typeof IndexRoute
AdminRoute: typeof AdminRoute AdminRoute: typeof AdminRouteWithChildren
AuthRoute: typeof AuthRouteWithChildren AuthRoute: typeof AuthRouteWithChildren
AboutRoute: typeof AboutRoute AboutRoute: typeof AboutRoute
LoginRoute: typeof LoginRoute LoginRoute: typeof LoginRoute
@@ -205,7 +249,7 @@ export interface RootRouteChildren {
const rootRouteChildren: RootRouteChildren = { const rootRouteChildren: RootRouteChildren = {
IndexRoute: IndexRoute, IndexRoute: IndexRoute,
AdminRoute: AdminRoute, AdminRoute: AdminRouteWithChildren,
AuthRoute: AuthRouteWithChildren, AuthRoute: AuthRouteWithChildren,
AboutRoute: AboutRoute, AboutRoute: AboutRoute,
LoginRoute: LoginRoute, LoginRoute: LoginRoute,
@@ -236,7 +280,10 @@ export const routeTree = rootRoute
"filePath": "index.tsx" "filePath": "index.tsx"
}, },
"/_admin": { "/_admin": {
"filePath": "_admin.tsx" "filePath": "_admin.tsx",
"children": [
"/_admin/modules"
]
}, },
"/_auth": { "/_auth": {
"filePath": "_auth.tsx", "filePath": "_auth.tsx",
@@ -250,6 +297,10 @@ export const routeTree = rootRoute
"/login": { "/login": {
"filePath": "login.tsx" "filePath": "login.tsx"
}, },
"/_admin/modules": {
"filePath": "_admin/modules.tsx",
"parent": "/_admin"
},
"/_auth/profile": { "/_auth/profile": {
"filePath": "_auth/profile.tsx", "filePath": "_auth/profile.tsx",
"parent": "/_auth" "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; name: string;
active: boolean; active: boolean;
roles: string; roles: string;
add_User: string; add_user: string;
add_Date: Date; add_date: Date;
upd_user: string; upd_user: string;
upd_date: Date; 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; user_id: string;
email: string; email: string;
username: string; username: string;
roles: keyof Roles[]; roles: Roles[];
role: string; role: string;
}; };
interface Roles { interface Roles {
role: string; role: string;
module_id: string;
} }
// user will need access to the module. // 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 { export function hasAccess(user: User | null, moduleName: string | null, modules: Modules[]): boolean {
// get the modules for the id // get the modules for the id
const filteredModule = modules?.filter((f) => f.name === moduleName); 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, // userroles and filter out by the module id,
console.log(user); return user?.roles.find((role) => role.module_id === filteredModule[0].module_id) ? true : false;
return false;
} }