refactor(modules): moved modules to app to control everything based on there active setting
This commit is contained in:
@@ -2,7 +2,6 @@ import { useRouter } from "@tanstack/react-router";
|
||||
import { useEffect } from "react";
|
||||
import { useModuleStore } from "../../routes/_old/old/-lib/store/useModuleStore";
|
||||
import { useSettingStore } from "../../routes/_old/old/-lib/store/useSettings";
|
||||
import { useSubModuleStore } from "../../routes/_old/old/-lib/store/useSubModuleStore";
|
||||
import { useSession, useUserRoles } from "../authClient";
|
||||
|
||||
export function SessionGuard({ children }: { children: React.ReactNode }) {
|
||||
@@ -10,7 +9,6 @@ export function SessionGuard({ children }: { children: React.ReactNode }) {
|
||||
const { fetchRoles } = useUserRoles();
|
||||
const { fetchModules } = useModuleStore();
|
||||
const { fetchSettings } = useSettingStore();
|
||||
const { fetchSubModules } = useSubModuleStore();
|
||||
const router = useRouter();
|
||||
|
||||
useEffect(() => {
|
||||
@@ -19,7 +17,6 @@ export function SessionGuard({ children }: { children: React.ReactNode }) {
|
||||
// }
|
||||
fetchModules();
|
||||
fetchSettings();
|
||||
fetchSubModules();
|
||||
//if (session) {
|
||||
fetchRoles();
|
||||
//}
|
||||
|
||||
@@ -36,7 +36,7 @@ export default function LoginForm({ redirectPath }: { redirectPath: string }) {
|
||||
|
||||
try {
|
||||
await axios.post("/lst/api/user/login", {
|
||||
username: value.username,
|
||||
username: value.username.toLowerCase(),
|
||||
password: value.password,
|
||||
});
|
||||
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import { Link } from "@tanstack/react-router";
|
||||
import { Link, useNavigate } from "@tanstack/react-router";
|
||||
import { toast } from "sonner";
|
||||
import {
|
||||
CardContent,
|
||||
@@ -11,6 +11,7 @@ import { useAppForm } from "@/lib/formStuff";
|
||||
import { LstCard } from "@/routes/_old/old/-components/extendedUi/LstCard";
|
||||
|
||||
export default function SignupForm() {
|
||||
const navigate = useNavigate();
|
||||
const form = useAppForm({
|
||||
defaultValues: {
|
||||
username: "",
|
||||
@@ -31,7 +32,13 @@ export default function SignupForm() {
|
||||
password: value.password,
|
||||
});
|
||||
|
||||
if (res.status === 200) {
|
||||
if (!res.data.success) {
|
||||
toast.error(res.data.message);
|
||||
}
|
||||
|
||||
if (res.data.success) {
|
||||
form.reset();
|
||||
navigate({ to: "/" });
|
||||
toast.success(`Welcome ${value.username}, to lst.`);
|
||||
}
|
||||
} catch (error) {
|
||||
|
||||
@@ -12,7 +12,7 @@ import {
|
||||
userAccess,
|
||||
useUserRoles,
|
||||
} from "../../../../../lib/authClient";
|
||||
import { useModuleStore } from "../../-lib/store/useModuleStore";
|
||||
|
||||
import { AdminSideBar } from "./side-components/admin";
|
||||
import { EomSideBar } from "./side-components/eom";
|
||||
import { ForkliftSideBar } from "./side-components/forklift";
|
||||
@@ -24,20 +24,13 @@ import { QualitySideBar } from "./side-components/quality";
|
||||
export function AppSidebar() {
|
||||
const { session } = useAuth();
|
||||
const { userRoles } = useUserRoles();
|
||||
const { modules } = useModuleStore();
|
||||
|
||||
return (
|
||||
<Sidebar collapsible="icon">
|
||||
<SidebarContent>
|
||||
<Header />
|
||||
|
||||
<ProductionSideBar
|
||||
user={session?.user as any}
|
||||
moduleID={
|
||||
modules.filter((n) => n.name === "production")[0]
|
||||
?.module_id as string
|
||||
}
|
||||
/>
|
||||
<ProductionSideBar user={session?.user as any} userRoles={userRoles} />
|
||||
|
||||
{/* userAccess("logistics", ["systemAdmin", "admin","manager","viewer"]) */}
|
||||
<LogisticsSideBar user={session?.user as any} userRoles={userRoles} />
|
||||
|
||||
@@ -1,4 +1,13 @@
|
||||
import { Barcode, Command, Cylinder, Package, Truck } from "lucide-react";
|
||||
import {
|
||||
Barcode,
|
||||
Cat,
|
||||
Command,
|
||||
Cylinder,
|
||||
Database,
|
||||
Package,
|
||||
Truck,
|
||||
} from "lucide-react";
|
||||
|
||||
import type { UserRoles } from "@/lib/authClient";
|
||||
import {
|
||||
SidebarGroup,
|
||||
@@ -8,7 +17,7 @@ import {
|
||||
SidebarMenuButton,
|
||||
SidebarMenuItem,
|
||||
} from "../../../../../../components/ui/sidebar";
|
||||
import { useSubModuleStore } from "../../../-lib/store/useSubModuleStore";
|
||||
import { useModuleStore } from "../../../-lib/store/useModuleStore";
|
||||
import type { User } from "../../../-types/users";
|
||||
import { hasPageAccess } from "../../../-utils/userAccess";
|
||||
|
||||
@@ -18,6 +27,8 @@ const iconMap: any = {
|
||||
Cylinder: Cylinder,
|
||||
Barcode: Barcode,
|
||||
Command: Command,
|
||||
Cat: Cat,
|
||||
Database: Database,
|
||||
};
|
||||
|
||||
export function LogisticsSideBar({
|
||||
@@ -27,9 +38,9 @@ export function LogisticsSideBar({
|
||||
user: User | null;
|
||||
userRoles: UserRoles[] | null;
|
||||
}) {
|
||||
const { subModules } = useSubModuleStore();
|
||||
const { modules } = useModuleStore();
|
||||
|
||||
const items = subModules?.filter((m) => m.moduleName === "logistics");
|
||||
const items = modules?.filter((m) => m.category === "logistics");
|
||||
const userUpdate = { ...user, roles: userRoles };
|
||||
|
||||
return (
|
||||
@@ -38,9 +49,10 @@ export function LogisticsSideBar({
|
||||
<SidebarGroupContent>
|
||||
<SidebarMenu>
|
||||
{items.map((item) => {
|
||||
const Icon = iconMap[item.icon];
|
||||
const Icon = iconMap[item.icon === "" ? "Cat" : item.icon];
|
||||
if (!item.active) return;
|
||||
return (
|
||||
<SidebarMenuItem key={item.submodule_id}>
|
||||
<SidebarMenuItem key={item.module_id}>
|
||||
{hasPageAccess(userUpdate as any, item.roles, item.name) && (
|
||||
<>
|
||||
<SidebarMenuButton asChild>
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
import { Printer, Tag } from "lucide-react";
|
||||
import { Cat, Printer, Tag } from "lucide-react";
|
||||
import type { UserRoles } from "@/lib/authClient";
|
||||
import {
|
||||
SidebarGroup,
|
||||
SidebarGroupContent,
|
||||
@@ -7,54 +8,53 @@ import {
|
||||
SidebarMenuButton,
|
||||
SidebarMenuItem,
|
||||
} from "../../../../../../components/ui/sidebar";
|
||||
import { useModuleStore } from "../../../-lib/store/useModuleStore";
|
||||
import type { User } from "../../../-types/users";
|
||||
import { hasPageAccess } from "../../../-utils/userAccess";
|
||||
|
||||
const iconMap: any = {
|
||||
Printer: Printer,
|
||||
Tag: Tag,
|
||||
|
||||
Cat: Cat,
|
||||
};
|
||||
|
||||
export function ProductionSideBar({
|
||||
user,
|
||||
moduleID,
|
||||
userRoles,
|
||||
}: {
|
||||
user: User | null;
|
||||
moduleID: string;
|
||||
userRoles: UserRoles[] | null;
|
||||
}) {
|
||||
const url: string = window.location.host.split(":")[0];
|
||||
const items = [
|
||||
{
|
||||
title: "One Click Print",
|
||||
url: "/lst/app/old/ocp",
|
||||
icon: Printer,
|
||||
role: ["viewer"],
|
||||
module: "ocp",
|
||||
active: true,
|
||||
},
|
||||
{
|
||||
title: "Rfid Readers",
|
||||
url: "/lst/app/old/rfid",
|
||||
icon: Tag,
|
||||
role: ["viewer"],
|
||||
module: "production",
|
||||
active: url === "usday1vms006" || url === "localhost" ? true : false,
|
||||
},
|
||||
];
|
||||
//const url: string = window.location.host.split(":")[0];
|
||||
const { modules } = useModuleStore();
|
||||
|
||||
const items = modules?.filter((m) => m.category === "production");
|
||||
const userUpdate = { ...user, roles: userRoles };
|
||||
|
||||
return (
|
||||
<SidebarGroup>
|
||||
<SidebarGroupLabel>Production</SidebarGroupLabel>
|
||||
<SidebarGroupContent>
|
||||
<SidebarMenu>
|
||||
{items.map((item) => (
|
||||
<SidebarMenuItem key={item.title}>
|
||||
<>
|
||||
{hasPageAccess(user, item.role, moduleID) && item.active && (
|
||||
<SidebarMenuButton asChild>
|
||||
<a href={item.url}>
|
||||
<item.icon />
|
||||
<span>{item.title}</span>
|
||||
</a>
|
||||
</SidebarMenuButton>
|
||||
)}
|
||||
</>
|
||||
</SidebarMenuItem>
|
||||
))}
|
||||
{items.map((item) => {
|
||||
if (!item.active) return;
|
||||
const Icon = iconMap[item.icon === "" ? "Cat" : item.icon];
|
||||
return (
|
||||
<SidebarMenuItem key={item.module_id}>
|
||||
<>
|
||||
{hasPageAccess(userUpdate as any, item.roles, item.name) && (
|
||||
<SidebarMenuButton asChild>
|
||||
<a href={item.link}>
|
||||
<Icon />
|
||||
<span>{item.name}</span>
|
||||
</a>
|
||||
</SidebarMenuButton>
|
||||
)}
|
||||
</>
|
||||
</SidebarMenuItem>
|
||||
);
|
||||
})}
|
||||
</SidebarMenu>
|
||||
</SidebarGroupContent>
|
||||
</SidebarGroup>
|
||||
|
||||
@@ -77,6 +77,7 @@ export default function Lots() {
|
||||
"technician",
|
||||
"admin",
|
||||
"manager",
|
||||
"supervisor",
|
||||
]);
|
||||
|
||||
if (session?.user && accessRoles) {
|
||||
|
||||
@@ -18,7 +18,7 @@ export const useModuleStore = create<SettingState>()((set) => ({
|
||||
fetchModules: async () => {
|
||||
try {
|
||||
//const response = await axios.get<{data: Setting[]}>(`${process.env.NEXT_PUBLIC_URL}/api/settings/client`);
|
||||
const response = await axios.get(`/lst/old/api/server/modules`, {});
|
||||
const response = await axios.get(`/lst/api/system/modules`, {});
|
||||
const data: FetchModulesResponse = response.data; //await response.json();
|
||||
//console.log(data);
|
||||
set({ modules: data.data });
|
||||
|
||||
@@ -1,20 +1,23 @@
|
||||
export interface Modules {
|
||||
module_id: string;
|
||||
name: string;
|
||||
active: boolean;
|
||||
roles: string;
|
||||
add_user: string;
|
||||
add_date: Date;
|
||||
upd_user: string;
|
||||
upd_date: Date;
|
||||
module_id: string;
|
||||
icon: string;
|
||||
name: string;
|
||||
link: string;
|
||||
active: boolean;
|
||||
roles: string;
|
||||
category: string;
|
||||
add_user: string;
|
||||
add_date: Date;
|
||||
upd_user: string;
|
||||
upd_date: Date;
|
||||
}
|
||||
|
||||
export interface SubModules {
|
||||
submodule_id: string;
|
||||
name: string;
|
||||
link: string;
|
||||
icon: string;
|
||||
moduleName: string;
|
||||
active: boolean;
|
||||
roles: string[];
|
||||
submodule_id: string;
|
||||
name: string;
|
||||
link: string;
|
||||
icon: string;
|
||||
moduleName: string;
|
||||
active: boolean;
|
||||
roles: string[];
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user