refactor(old app): login migration to new app
This commit is contained in:
@@ -1,28 +1,26 @@
|
||||
import { getUsers } from "@/utils/querys/admin/users";
|
||||
import { useQuery } from "@tanstack/react-query";
|
||||
import UserCard from "./components/UserCard";
|
||||
|
||||
export default function UserPage() {
|
||||
const { data, isError, error, isLoading } = useQuery(getUsers());
|
||||
//const { data, isError, error, isLoading } = useQuery(getUsers());
|
||||
|
||||
if (isLoading) return <div className="m-auto">Loading users...</div>;
|
||||
// if (isLoading) return <div className="m-auto">Loading users...</div>;
|
||||
|
||||
if (isError)
|
||||
return (
|
||||
<div className="m-auto">
|
||||
There was an error getting the users.... {JSON.stringify(error)}
|
||||
</div>
|
||||
);
|
||||
// if (isError)
|
||||
// return (
|
||||
// <div className="m-auto">
|
||||
// There was an error getting the users.... {JSON.stringify(error)}
|
||||
// </div>
|
||||
// );
|
||||
|
||||
return (
|
||||
<div className="m-2 w-dvw">
|
||||
{data.map((u: any) => {
|
||||
return (
|
||||
<div>
|
||||
<UserCard user={u} />
|
||||
</div>
|
||||
);
|
||||
})}
|
||||
</div>
|
||||
);
|
||||
return (
|
||||
<div className="m-2 w-dvw">
|
||||
<span>This has been moved to the new system</span>
|
||||
{/* {data.map((u: any) => {
|
||||
return (
|
||||
<div>
|
||||
<UserCard user={u} />
|
||||
|
||||
</div>
|
||||
);
|
||||
})} */}
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
@@ -1,172 +1,177 @@
|
||||
import { useSessionStore } from "../../lib/store/sessionStore";
|
||||
import { LstCard } from "../extendedUI/LstCard";
|
||||
import { CardHeader } from "../ui/card";
|
||||
import { toast } from "sonner";
|
||||
import { z } from "zod";
|
||||
import { zodResolver } from "@hookform/resolvers/zod";
|
||||
import { useRouter, useSearch } from "@tanstack/react-router";
|
||||
import { Controller, useForm } from "react-hook-form";
|
||||
import { zodResolver } from "@hookform/resolvers/zod";
|
||||
import { Label } from "../ui/label";
|
||||
import { Input } from "../ui/input";
|
||||
import { Checkbox } from "../ui/checkbox";
|
||||
import { toast } from "sonner";
|
||||
import { z } from "zod";
|
||||
import { useAuthStore } from "@/lib/store/useAuthStore";
|
||||
import { useSessionStore } from "../../lib/store/sessionStore";
|
||||
import { LstCard } from "../extendedUI/LstCard";
|
||||
import { Button } from "../ui/button";
|
||||
import { CardHeader } from "../ui/card";
|
||||
import { Checkbox } from "../ui/checkbox";
|
||||
import { Input } from "../ui/input";
|
||||
import { Label } from "../ui/label";
|
||||
|
||||
const FormSchema = z.object({
|
||||
username: z.string().min(1, "You must enter a valid username"),
|
||||
password: z.string().min(4, "You must enter a valid password"),
|
||||
rememberMe: z.boolean(),
|
||||
username: z.string().min(1, "You must enter a valid username"),
|
||||
password: z.string().min(4, "You must enter a valid password"),
|
||||
rememberMe: z.boolean(),
|
||||
});
|
||||
|
||||
const LoginForm = () => {
|
||||
const { setSession } = useSessionStore();
|
||||
const rememeberMe = localStorage.getItem("rememberMe") === "true";
|
||||
const username = localStorage.getItem("username") || "";
|
||||
const router = useRouter();
|
||||
const search = useSearch({ from: "/login" });
|
||||
const { setSession } = useSessionStore();
|
||||
const { setUserInfo } = useAuthStore();
|
||||
const rememeberMe = localStorage.getItem("rememberMe") === "true";
|
||||
const username = localStorage.getItem("username") || "";
|
||||
const router = useRouter();
|
||||
const search = useSearch({ from: "/login" });
|
||||
|
||||
const {
|
||||
register,
|
||||
handleSubmit,
|
||||
control,
|
||||
formState: { errors },
|
||||
} = useForm<z.infer<typeof FormSchema>>({
|
||||
resolver: zodResolver(FormSchema),
|
||||
defaultValues: {
|
||||
username: username || "",
|
||||
password: "",
|
||||
rememberMe: rememeberMe,
|
||||
},
|
||||
});
|
||||
const {
|
||||
register,
|
||||
handleSubmit,
|
||||
control,
|
||||
formState: { errors },
|
||||
} = useForm<z.infer<typeof FormSchema>>({
|
||||
resolver: zodResolver(FormSchema),
|
||||
defaultValues: {
|
||||
username: username || "",
|
||||
password: "",
|
||||
rememberMe: rememeberMe,
|
||||
},
|
||||
});
|
||||
|
||||
const onSubmitLogin = async (value: z.infer<typeof FormSchema>) => {
|
||||
// Do something with form data
|
||||
const onSubmitLogin = async (value: z.infer<typeof FormSchema>) => {
|
||||
// Do something with form data
|
||||
|
||||
// first update the rememberMe incase it was selected
|
||||
if (value.rememberMe) {
|
||||
localStorage.setItem("rememberMe", value.rememberMe.toString());
|
||||
localStorage.setItem("username", value.username);
|
||||
} else {
|
||||
localStorage.removeItem("rememberMe");
|
||||
localStorage.removeItem("username");
|
||||
}
|
||||
// first update the rememberMe incase it was selected
|
||||
if (value.rememberMe) {
|
||||
localStorage.setItem("rememberMe", value.rememberMe.toString());
|
||||
localStorage.setItem("username", value.username);
|
||||
} else {
|
||||
localStorage.removeItem("rememberMe");
|
||||
localStorage.removeItem("username");
|
||||
}
|
||||
|
||||
try {
|
||||
const response = await fetch("/api/auth/login", {
|
||||
method: "POST",
|
||||
headers: {
|
||||
"Content-Type": "application/json",
|
||||
},
|
||||
body: JSON.stringify({
|
||||
username: value.username,
|
||||
password: value.password,
|
||||
}),
|
||||
});
|
||||
try {
|
||||
const response = await fetch("/api/auth/login", {
|
||||
method: "POST",
|
||||
headers: {
|
||||
"Content-Type": "application/json",
|
||||
},
|
||||
body: JSON.stringify({
|
||||
username: value.username,
|
||||
password: value.password,
|
||||
}),
|
||||
});
|
||||
|
||||
const data = await response.json();
|
||||
const data = await response.json();
|
||||
|
||||
// Store token in localStorage
|
||||
// localStorage.setItem("auth_token", data.data.token);
|
||||
if (data.success) {
|
||||
const prod = btoa(
|
||||
`${value.username.toLowerCase()}:${value.password}`
|
||||
);
|
||||
const prodUser = { ...data.user, prod: prod };
|
||||
// Store token in localStorage
|
||||
// localStorage.setItem("auth_token", data.data.token);
|
||||
if (data.success) {
|
||||
const prod = btoa(`${value.username.toLowerCase()}:${value.password}`);
|
||||
const prodUser = { ...data.user, prod: prod };
|
||||
setUserInfo(value.username.toLowerCase(), value.password);
|
||||
setSession(prodUser, data.data.token);
|
||||
toast.success(`You are logged in as ${data.data.user.username}`);
|
||||
|
||||
setSession(prodUser, data.token);
|
||||
toast.success(`You are logged in as ${data.user.username}`);
|
||||
//console.log(search.redirect ? search.redirect : "oops");
|
||||
router.history.push(search.redirect ? search.redirect : "/");
|
||||
}
|
||||
|
||||
console.log(search.redirect ? search.redirect : "oops");
|
||||
router.history.push(search.redirect ? search.redirect : "/");
|
||||
}
|
||||
if (!data.success) {
|
||||
toast.error(`${data.message}`);
|
||||
}
|
||||
|
||||
if (!data.success) {
|
||||
toast.error(`${data.message}`);
|
||||
}
|
||||
//console.log(data);
|
||||
} catch (err) {
|
||||
// @ts-ignore
|
||||
if (!err.response.success) {
|
||||
// @ts-ignore
|
||||
toast.error(err.response.data.message);
|
||||
} else {
|
||||
// @ts-ignore
|
||||
toast.error(err?.message);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
//console.log(data);
|
||||
} catch (err) {
|
||||
toast.error("Invalid credentials");
|
||||
}
|
||||
};
|
||||
return (
|
||||
<div className="ml-[25%]">
|
||||
<LstCard className="p-3 w-96">
|
||||
<CardHeader>
|
||||
<div>
|
||||
<p className="text-2xl">Login to LST</p>
|
||||
</div>
|
||||
</CardHeader>
|
||||
<hr className="rounded"></hr>
|
||||
<form onSubmit={handleSubmit(onSubmitLogin)}>
|
||||
<div>
|
||||
<Label htmlFor="username" className="m-1">
|
||||
Username
|
||||
</Label>
|
||||
<Input
|
||||
placeholder="smith001"
|
||||
{...register("username")}
|
||||
className={errors.username ? "border-red-500" : ""}
|
||||
aria-invalid={!!errors.username}
|
||||
/>
|
||||
{errors.username && (
|
||||
<p className="text-red-500 text-sm mt-1">
|
||||
{errors.username.message}
|
||||
</p>
|
||||
)}
|
||||
</div>
|
||||
<div>
|
||||
<>
|
||||
<Label htmlFor={"password"} className="m-1">
|
||||
Password
|
||||
</Label>
|
||||
<Input
|
||||
type="password"
|
||||
{...register("password")}
|
||||
className={errors.password ? "border-red-500" : ""}
|
||||
aria-invalid={!!errors.password}
|
||||
/>
|
||||
{errors.password && (
|
||||
<p className="text-red-500 text-sm mt-1">
|
||||
{errors.password.message}
|
||||
</p>
|
||||
)}
|
||||
</>
|
||||
</div>
|
||||
<div className="flex justify-between pt-2">
|
||||
<div className="flex">
|
||||
<Controller
|
||||
render={({ field }) => (
|
||||
<>
|
||||
<Checkbox
|
||||
id="remember"
|
||||
checked={field.value}
|
||||
onCheckedChange={field.onChange}
|
||||
/>
|
||||
<label
|
||||
htmlFor="remember"
|
||||
className="pl-2 text-sm font-medium leading-none peer-disabled:cursor-not-allowed peer-disabled:opacity-70"
|
||||
>
|
||||
remember me
|
||||
</label>
|
||||
</>
|
||||
)}
|
||||
control={control}
|
||||
name="rememberMe"
|
||||
defaultValue={rememeberMe}
|
||||
/>
|
||||
</div>
|
||||
|
||||
return (
|
||||
<div className="ml-[25%]">
|
||||
<LstCard className="p-3 w-96">
|
||||
<CardHeader>
|
||||
<div>
|
||||
<p className="text-2xl">Login to LST</p>
|
||||
</div>
|
||||
</CardHeader>
|
||||
<hr className="rounded"></hr>
|
||||
<form onSubmit={handleSubmit(onSubmitLogin)}>
|
||||
<div>
|
||||
<Label htmlFor="username" className="m-1">
|
||||
Username
|
||||
</Label>
|
||||
<Input
|
||||
placeholder="smith001"
|
||||
{...register("username")}
|
||||
className={errors.username ? "border-red-500" : ""}
|
||||
aria-invalid={!!errors.username}
|
||||
/>
|
||||
{errors.username && (
|
||||
<p className="text-red-500 text-sm mt-1">
|
||||
{errors.username.message}
|
||||
</p>
|
||||
)}
|
||||
</div>
|
||||
<div>
|
||||
<>
|
||||
<Label htmlFor={"password"} className="m-1">
|
||||
Password
|
||||
</Label>
|
||||
<Input
|
||||
type="password"
|
||||
{...register("password")}
|
||||
className={
|
||||
errors.password ? "border-red-500" : ""
|
||||
}
|
||||
aria-invalid={!!errors.password}
|
||||
/>
|
||||
{errors.password && (
|
||||
<p className="text-red-500 text-sm mt-1">
|
||||
{errors.password.message}
|
||||
</p>
|
||||
)}
|
||||
</>
|
||||
</div>
|
||||
<div className="flex justify-between pt-2">
|
||||
<div className="flex">
|
||||
<Controller
|
||||
render={({ field }) => (
|
||||
<>
|
||||
<Checkbox
|
||||
id="remember"
|
||||
checked={field.value}
|
||||
onCheckedChange={field.onChange}
|
||||
/>
|
||||
<label
|
||||
htmlFor="remember"
|
||||
className="pl-2 text-sm font-medium leading-none peer-disabled:cursor-not-allowed peer-disabled:opacity-70"
|
||||
>
|
||||
remember me
|
||||
</label>
|
||||
</>
|
||||
)}
|
||||
control={control}
|
||||
name="rememberMe"
|
||||
defaultValue={rememeberMe}
|
||||
/>
|
||||
</div>
|
||||
|
||||
<div className="flex justify-end">
|
||||
<Button type="submit">Submit</Button>
|
||||
</div>
|
||||
</div>
|
||||
</form>
|
||||
</LstCard>
|
||||
</div>
|
||||
);
|
||||
<div className="flex justify-end">
|
||||
<Button type="submit">Submit</Button>
|
||||
</div>
|
||||
</div>
|
||||
</form>
|
||||
</LstCard>
|
||||
</div>
|
||||
);
|
||||
};
|
||||
|
||||
export default LoginForm;
|
||||
|
||||
@@ -1,61 +1,55 @@
|
||||
import { useSessionStore } from "../../lib/store/sessionStore";
|
||||
import { useModuleStore } from "../../lib/store/useModuleStore";
|
||||
import { moduleActive } from "../../utils/moduleActive";
|
||||
import { hasAccess } from "../../utils/userAccess";
|
||||
import {
|
||||
Sidebar,
|
||||
SidebarContent,
|
||||
SidebarFooter,
|
||||
SidebarTrigger,
|
||||
Sidebar,
|
||||
SidebarContent,
|
||||
SidebarFooter,
|
||||
SidebarTrigger,
|
||||
} from "../ui/sidebar";
|
||||
import { ProductionSideBar } from "./side-components/production";
|
||||
import { AdminSideBar } from "./side-components/admin";
|
||||
import { EomSideBar } from "./side-components/eom";
|
||||
import { ForkliftSideBar } from "./side-components/forklift";
|
||||
import { Header } from "./side-components/header";
|
||||
import { LogisticsSideBar } from "./side-components/logistics";
|
||||
import { ProductionSideBar } from "./side-components/production";
|
||||
import { QualitySideBar } from "./side-components/quality";
|
||||
import { ForkliftSideBar } from "./side-components/forklift";
|
||||
import { EomSideBar } from "./side-components/eom";
|
||||
import { AdminSideBar } from "./side-components/admin";
|
||||
import { useSessionStore } from "../../lib/store/sessionStore";
|
||||
import { hasAccess } from "../../utils/userAccess";
|
||||
import { moduleActive } from "../../utils/moduleActive";
|
||||
import { useModuleStore } from "../../lib/store/useModuleStore";
|
||||
|
||||
export function AppSidebar() {
|
||||
const { user } = useSessionStore();
|
||||
const { modules } = useModuleStore();
|
||||
const { user } = useSessionStore();
|
||||
const { modules } = useModuleStore();
|
||||
|
||||
return (
|
||||
<Sidebar collapsible="icon">
|
||||
<SidebarContent>
|
||||
<Header />
|
||||
{moduleActive("production") && (
|
||||
<ProductionSideBar
|
||||
user={user}
|
||||
moduleID={
|
||||
modules.filter((n) => n.name === "production")[0]
|
||||
.module_id as string
|
||||
}
|
||||
/>
|
||||
)}
|
||||
{moduleActive("logistics") && (
|
||||
<LogisticsSideBar
|
||||
user={user}
|
||||
moduleID={
|
||||
modules.filter((n) => n.name === "logistics")[0]
|
||||
.module_id as string
|
||||
}
|
||||
/>
|
||||
)}
|
||||
{moduleActive("forklift") &&
|
||||
hasAccess(user, "forklift", modules) && <ForkliftSideBar />}
|
||||
{moduleActive("eom") && hasAccess(user, "eom", modules) && (
|
||||
<EomSideBar />
|
||||
)}
|
||||
{moduleActive("quality") &&
|
||||
hasAccess(user, "quality", modules) && <QualitySideBar />}
|
||||
{moduleActive("admin") && hasAccess(user, "admin", modules) && (
|
||||
<AdminSideBar />
|
||||
)}
|
||||
</SidebarContent>
|
||||
<SidebarFooter>
|
||||
<SidebarTrigger />
|
||||
</SidebarFooter>
|
||||
</Sidebar>
|
||||
);
|
||||
return (
|
||||
<Sidebar collapsible="icon">
|
||||
<SidebarContent>
|
||||
<Header />
|
||||
{moduleActive("production") && (
|
||||
<ProductionSideBar
|
||||
user={user}
|
||||
moduleID={
|
||||
modules.filter((n) => n.name === "production")[0]
|
||||
.module_id as string
|
||||
}
|
||||
/>
|
||||
)}
|
||||
{moduleActive("logistics") && hasAccess(user, "logistics") && (
|
||||
<LogisticsSideBar user={user} />
|
||||
)}
|
||||
{moduleActive("forklift") && hasAccess(user, "forklift") && (
|
||||
<ForkliftSideBar />
|
||||
)}
|
||||
{moduleActive("eom") && hasAccess(user, "eom") && <EomSideBar />}
|
||||
{moduleActive("quality") && hasAccess(user, "quality") && (
|
||||
<QualitySideBar />
|
||||
)}
|
||||
{moduleActive("admin") && hasAccess(user || [], "admin") && (
|
||||
<AdminSideBar />
|
||||
)}
|
||||
</SidebarContent>
|
||||
<SidebarFooter>
|
||||
<SidebarTrigger />
|
||||
</SidebarFooter>
|
||||
</Sidebar>
|
||||
);
|
||||
}
|
||||
|
||||
@@ -1,64 +1,53 @@
|
||||
import { Barcode, Cylinder, Package, Truck, Command } from "lucide-react";
|
||||
import {
|
||||
SidebarGroup,
|
||||
SidebarGroupContent,
|
||||
SidebarGroupLabel,
|
||||
SidebarMenu,
|
||||
SidebarMenuButton,
|
||||
SidebarMenuItem,
|
||||
} from "../../ui/sidebar";
|
||||
import { hasPageAccess } from "@/utils/userAccess";
|
||||
import { User } from "@/types/users";
|
||||
import { Barcode, Command, Cylinder, Package, Truck } from "lucide-react";
|
||||
import { useSubModuleStore } from "@/lib/store/useSubModuleStore";
|
||||
import { User } from "@/types/users";
|
||||
import { hasPageAccess } from "@/utils/userAccess";
|
||||
import {
|
||||
SidebarGroup,
|
||||
SidebarGroupContent,
|
||||
SidebarGroupLabel,
|
||||
SidebarMenu,
|
||||
SidebarMenuButton,
|
||||
SidebarMenuItem,
|
||||
} from "../../ui/sidebar";
|
||||
|
||||
const iconMap: any = {
|
||||
Package: Package,
|
||||
Truck: Truck,
|
||||
Cylinder: Cylinder,
|
||||
Barcode: Barcode,
|
||||
Command: Command,
|
||||
Package: Package,
|
||||
Truck: Truck,
|
||||
Cylinder: Cylinder,
|
||||
Barcode: Barcode,
|
||||
Command: Command,
|
||||
};
|
||||
|
||||
export function LogisticsSideBar({
|
||||
user,
|
||||
moduleID,
|
||||
}: {
|
||||
user: User | null;
|
||||
moduleID: string;
|
||||
}) {
|
||||
const { subModules } = useSubModuleStore();
|
||||
export function LogisticsSideBar({ user }: { user: User | null }) {
|
||||
const { subModules } = useSubModuleStore();
|
||||
|
||||
const items = subModules.filter((m) => m.moduleName === "logistics");
|
||||
const items = subModules.filter((m) => m.moduleName === "logistics");
|
||||
|
||||
return (
|
||||
<SidebarGroup>
|
||||
<SidebarGroupLabel>Logistics</SidebarGroupLabel>
|
||||
<SidebarGroupContent>
|
||||
<SidebarMenu>
|
||||
{items.map((item) => {
|
||||
const Icon = iconMap[item.icon];
|
||||
return (
|
||||
<SidebarMenuItem key={item.submodule_id}>
|
||||
<>
|
||||
{hasPageAccess(
|
||||
user,
|
||||
item.roles,
|
||||
moduleID
|
||||
) &&
|
||||
item.active && (
|
||||
<SidebarMenuButton asChild>
|
||||
<a href={item.link}>
|
||||
<Icon />
|
||||
<span>{item.name}</span>
|
||||
</a>
|
||||
</SidebarMenuButton>
|
||||
)}
|
||||
</>
|
||||
</SidebarMenuItem>
|
||||
);
|
||||
})}
|
||||
</SidebarMenu>
|
||||
</SidebarGroupContent>
|
||||
</SidebarGroup>
|
||||
);
|
||||
return (
|
||||
<SidebarGroup>
|
||||
<SidebarGroupLabel>Logistics</SidebarGroupLabel>
|
||||
<SidebarGroupContent>
|
||||
<SidebarMenu>
|
||||
{items.map((item) => {
|
||||
const Icon = iconMap[item.icon];
|
||||
return (
|
||||
<SidebarMenuItem key={item.submodule_id}>
|
||||
<>
|
||||
{hasPageAccess(user, item.roles, item.name) && (
|
||||
<SidebarMenuButton asChild>
|
||||
<a href={item.link}>
|
||||
<Icon />
|
||||
<span>{item.name}</span>
|
||||
</a>
|
||||
</SidebarMenuButton>
|
||||
)}
|
||||
</>
|
||||
</SidebarMenuItem>
|
||||
);
|
||||
})}
|
||||
</SidebarMenu>
|
||||
</SidebarGroupContent>
|
||||
</SidebarGroup>
|
||||
);
|
||||
}
|
||||
|
||||
@@ -1,15 +1,3 @@
|
||||
import { LstCard } from "@/components/extendedUI/LstCard";
|
||||
import { Button } from "@/components/ui/button";
|
||||
import { CardHeader } from "@/components/ui/card";
|
||||
import { Input } from "@/components/ui/input";
|
||||
import { Label } from "@/components/ui/label";
|
||||
import {
|
||||
Tooltip,
|
||||
TooltipContent,
|
||||
TooltipProvider,
|
||||
TooltipTrigger,
|
||||
} from "@/components/ui/tooltip";
|
||||
import { getStockSilo } from "@/utils/querys/logistics/siloAdjustments/getStockSilo";
|
||||
import { useForm } from "@tanstack/react-form";
|
||||
import { useQuery } from "@tanstack/react-query";
|
||||
import { Link } from "@tanstack/react-router";
|
||||
@@ -18,246 +6,222 @@ import { format } from "date-fns";
|
||||
import { CircleAlert } from "lucide-react";
|
||||
import { useState } from "react";
|
||||
import { toast } from "sonner";
|
||||
import ChartData from "./ChartData";
|
||||
import { AttachSilo } from "./AttachSilo";
|
||||
import { DetachSilo } from "./DetachSilo";
|
||||
import { LstCard } from "@/components/extendedUI/LstCard";
|
||||
import { Button } from "@/components/ui/button";
|
||||
import { CardHeader } from "@/components/ui/card";
|
||||
import { Input } from "@/components/ui/input";
|
||||
import { Label } from "@/components/ui/label";
|
||||
import {
|
||||
Tooltip,
|
||||
TooltipContent,
|
||||
TooltipProvider,
|
||||
TooltipTrigger,
|
||||
} from "@/components/ui/tooltip";
|
||||
import { useSessionStore } from "@/lib/store/sessionStore";
|
||||
import { useModuleStore } from "@/lib/store/useModuleStore";
|
||||
import { useGetUserRoles } from "@/lib/store/useGetRoles";
|
||||
import { useModuleStore } from "@/lib/store/useModuleStore";
|
||||
import { getStockSilo } from "@/utils/querys/logistics/siloAdjustments/getStockSilo";
|
||||
import { AttachSilo } from "./AttachSilo";
|
||||
import ChartData from "./ChartData";
|
||||
import { DetachSilo } from "./DetachSilo";
|
||||
|
||||
export default function SiloCard(data: any) {
|
||||
const token = localStorage.getItem("auth_token");
|
||||
const [submitting, setSubmitting] = useState(false);
|
||||
const { refetch } = useQuery(getStockSilo());
|
||||
const { user } = useSessionStore();
|
||||
const { userRoles } = useGetUserRoles();
|
||||
const { modules } = useModuleStore();
|
||||
const silo = data.silo;
|
||||
const [submitting, setSubmitting] = useState(false);
|
||||
const { refetch } = useQuery(getStockSilo());
|
||||
const { user } = useSessionStore();
|
||||
const { userRoles } = useGetUserRoles();
|
||||
const { modules } = useModuleStore();
|
||||
const silo = data.silo;
|
||||
|
||||
// roles that can do the silo adjustments
|
||||
const roles = ["systemAdmin", "technician", "admin", "manager"];
|
||||
// roles that can do the silo adjustments
|
||||
const roles = ["systemAdmin", "technician", "admin", "manager"];
|
||||
|
||||
const module = modules.filter((n) => n.name === "logistics");
|
||||
const module = modules.filter((n) => n.name === "logistics");
|
||||
|
||||
const accessRoles = userRoles.filter(
|
||||
(n) => n.module_id === module[0]?.module_id
|
||||
) as any;
|
||||
const accessRoles = userRoles.filter(
|
||||
(n: any) => n.module === module[0]?.name,
|
||||
) as any;
|
||||
|
||||
const form = useForm({
|
||||
defaultValues: {
|
||||
newLevel: "",
|
||||
},
|
||||
onSubmit: async ({ value }) => {
|
||||
setSubmitting(true);
|
||||
const dataToSubmit = {
|
||||
quantity: parseFloat(value.newLevel),
|
||||
warehouseId: silo.WarehouseID,
|
||||
laneId: silo.LocationID,
|
||||
};
|
||||
const form = useForm({
|
||||
defaultValues: {
|
||||
newLevel: "",
|
||||
},
|
||||
onSubmit: async ({ value }) => {
|
||||
setSubmitting(true);
|
||||
const dataToSubmit = {
|
||||
quantity: parseFloat(value.newLevel),
|
||||
warehouseId: silo.WarehouseID,
|
||||
laneId: silo.LocationID,
|
||||
};
|
||||
|
||||
try {
|
||||
const res = await axios.post(
|
||||
"/api/logistics/createsiloadjustment",
|
||||
dataToSubmit,
|
||||
{ headers: { Authorization: `Bearer ${token}` } }
|
||||
);
|
||||
//console.log(res.data);
|
||||
try {
|
||||
const res = await axios.post(
|
||||
"/api/logistics/createsiloadjustment",
|
||||
dataToSubmit,
|
||||
{ withCredentials: true },
|
||||
);
|
||||
//console.log(res.data);
|
||||
|
||||
if (res.data.success) {
|
||||
toast.success(res.data.message);
|
||||
refetch();
|
||||
form.reset();
|
||||
}
|
||||
if (!res.data.success && res.data.data?.status === 400) {
|
||||
if (res.data.data.status === 400) {
|
||||
toast.error(res.data.data.data.errors[0].message);
|
||||
}
|
||||
} else if (!res.data.success) {
|
||||
toast.error(res.data.message);
|
||||
}
|
||||
setSubmitting(false);
|
||||
} catch (error: any) {
|
||||
//console.log(error);
|
||||
if (error.status === 401) {
|
||||
toast.error(error.response.statusText);
|
||||
setSubmitting(false);
|
||||
}
|
||||
}
|
||||
},
|
||||
});
|
||||
if (res.data.success) {
|
||||
toast.success(res.data.message);
|
||||
refetch();
|
||||
form.reset();
|
||||
}
|
||||
if (!res.data.success && res.data.data?.status === 400) {
|
||||
if (res.data.data.status === 400) {
|
||||
toast.error(res.data.data.data.errors[0].message);
|
||||
}
|
||||
} else if (!res.data.success) {
|
||||
toast.error(res.data.message);
|
||||
}
|
||||
setSubmitting(false);
|
||||
} catch (error: any) {
|
||||
//console.log(error);
|
||||
if (error.status === 401) {
|
||||
toast.error(error.response.statusText);
|
||||
setSubmitting(false);
|
||||
}
|
||||
}
|
||||
},
|
||||
});
|
||||
|
||||
console.log(accessRoles);
|
||||
return (
|
||||
<LstCard>
|
||||
<div className="flex flex-row">
|
||||
<LstCard className="grow m-1 max-w-[400px]">
|
||||
<CardHeader>{silo.Description}</CardHeader>
|
||||
<div className="m-1">
|
||||
<hr className="m-2" />
|
||||
<span>Current Stock: </span>
|
||||
{silo.Stock_Total}
|
||||
<hr className="m-2" />
|
||||
<span>Last date adjusted </span>
|
||||
{format(silo.LastAdjustment, "M/dd/yyyy")}
|
||||
<hr className="m-2" />
|
||||
</div>
|
||||
<div>
|
||||
{silo.Stock_Total === 0 ? (
|
||||
<div className="flex justify-center flex-col">
|
||||
<span>
|
||||
The silo is currently empty you will not be
|
||||
able to do an adjustment until you have
|
||||
received material in.
|
||||
</span>
|
||||
<hr />
|
||||
<ul>
|
||||
<li>
|
||||
-Someone click "Take inventory on a
|
||||
empty location" in stock.
|
||||
</li>
|
||||
<li>
|
||||
-Silo virtualy ran empty due to
|
||||
production over consumption.
|
||||
</li>
|
||||
<li>
|
||||
-Someone forgot to move a railcar
|
||||
compartment over to this location.
|
||||
</li>
|
||||
</ul>
|
||||
</div>
|
||||
) : (
|
||||
<>
|
||||
{user &&
|
||||
roles.includes(accessRoles[0]?.role) && (
|
||||
<form
|
||||
onSubmit={(e) => {
|
||||
e.preventDefault();
|
||||
e.stopPropagation();
|
||||
}}
|
||||
>
|
||||
<form.Field
|
||||
name="newLevel"
|
||||
validators={{
|
||||
// We can choose between form-wide and field-specific validators
|
||||
onChange: ({ value }) =>
|
||||
value.length > 1
|
||||
? undefined
|
||||
: "You must enter a value greate than 1",
|
||||
}}
|
||||
children={(field) => {
|
||||
return (
|
||||
<div className="m-2 min-w-48 max-w-96 p-2">
|
||||
<div className="flex flex-row">
|
||||
<Label htmlFor="newLevel">
|
||||
New level
|
||||
</Label>
|
||||
<div>
|
||||
<Disclaimer />
|
||||
</div>
|
||||
</div>
|
||||
<div className="flex flex-row">
|
||||
<Input
|
||||
name={
|
||||
field.name
|
||||
}
|
||||
value={
|
||||
field
|
||||
.state
|
||||
.value
|
||||
}
|
||||
onBlur={
|
||||
field.handleBlur
|
||||
}
|
||||
type="decimal"
|
||||
onChange={(
|
||||
e
|
||||
) =>
|
||||
field.handleChange(
|
||||
e
|
||||
.target
|
||||
.value
|
||||
)
|
||||
}
|
||||
/>
|
||||
<Button
|
||||
className="ml-1"
|
||||
variant="outline"
|
||||
type="submit"
|
||||
onClick={
|
||||
form.handleSubmit
|
||||
}
|
||||
disabled={
|
||||
submitting
|
||||
}
|
||||
>
|
||||
{submitting ? (
|
||||
<span className="w-24">
|
||||
Submitting...
|
||||
</span>
|
||||
) : (
|
||||
<span className="w-24">
|
||||
Submit
|
||||
</span>
|
||||
)}
|
||||
</Button>
|
||||
</div>
|
||||
return (
|
||||
<LstCard>
|
||||
<div className="flex flex-row">
|
||||
<LstCard className="grow m-1 max-w-[400px]">
|
||||
<CardHeader>{silo.Description}</CardHeader>
|
||||
<div className="m-1">
|
||||
<hr className="m-2" />
|
||||
<span>Current Stock: </span>
|
||||
{silo.Stock_Total}
|
||||
<hr className="m-2" />
|
||||
<span>Last date adjusted </span>
|
||||
{format(silo.LastAdjustment, "M/dd/yyyy")}
|
||||
<hr className="m-2" />
|
||||
</div>
|
||||
<div>
|
||||
{silo.Stock_Total === 0 ? (
|
||||
<div className="flex justify-center flex-col">
|
||||
<span>
|
||||
The silo is currently empty you will not be able to do an
|
||||
adjustment until you have received material in.
|
||||
</span>
|
||||
<hr />
|
||||
<ul>
|
||||
<li>
|
||||
-Someone click "Take inventory on a empty location" in
|
||||
stock.
|
||||
</li>
|
||||
<li>
|
||||
-Silo virtualy ran empty due to production over consumption.
|
||||
</li>
|
||||
<li>
|
||||
-Someone forgot to move a railcar compartment over to this
|
||||
location.
|
||||
</li>
|
||||
</ul>
|
||||
</div>
|
||||
) : (
|
||||
<>
|
||||
{user && roles.includes(accessRoles[0]?.role) && (
|
||||
<form
|
||||
onSubmit={(e) => {
|
||||
e.preventDefault();
|
||||
e.stopPropagation();
|
||||
}}
|
||||
>
|
||||
<form.Field
|
||||
name="newLevel"
|
||||
validators={{
|
||||
// We can choose between form-wide and field-specific validators
|
||||
onChange: ({ value }) =>
|
||||
value.length > 1
|
||||
? undefined
|
||||
: "You must enter a value greate than 1",
|
||||
}}
|
||||
children={(field) => {
|
||||
return (
|
||||
<div className="m-2 min-w-48 max-w-96 p-2">
|
||||
<div className="flex flex-row">
|
||||
<Label htmlFor="newLevel">New level</Label>
|
||||
<div>
|
||||
<Disclaimer />
|
||||
</div>
|
||||
</div>
|
||||
<div className="flex flex-row">
|
||||
<Input
|
||||
name={field.name}
|
||||
value={field.state.value}
|
||||
onBlur={field.handleBlur}
|
||||
type="decimal"
|
||||
onChange={(e) =>
|
||||
field.handleChange(e.target.value)
|
||||
}
|
||||
/>
|
||||
<Button
|
||||
className="ml-1"
|
||||
variant="outline"
|
||||
type="submit"
|
||||
onClick={form.handleSubmit}
|
||||
disabled={submitting}
|
||||
>
|
||||
{submitting ? (
|
||||
<span className="w-24">Submitting...</span>
|
||||
) : (
|
||||
<span className="w-24">Submit</span>
|
||||
)}
|
||||
</Button>
|
||||
</div>
|
||||
|
||||
{field.state.meta
|
||||
.errors
|
||||
.length ? (
|
||||
<em>
|
||||
{field.state.meta.errors.join(
|
||||
","
|
||||
)}
|
||||
</em>
|
||||
) : null}
|
||||
</div>
|
||||
);
|
||||
}}
|
||||
/>
|
||||
</form>
|
||||
)}
|
||||
</>
|
||||
)}
|
||||
</div>
|
||||
</LstCard>
|
||||
<div className="grow max-w-[600px]">
|
||||
<ChartData laneId={silo.LocationID} />
|
||||
{field.state.meta.errors.length ? (
|
||||
<em>{field.state.meta.errors.join(",")}</em>
|
||||
) : null}
|
||||
</div>
|
||||
);
|
||||
}}
|
||||
/>
|
||||
</form>
|
||||
)}
|
||||
</>
|
||||
)}
|
||||
</div>
|
||||
</LstCard>
|
||||
<div className="grow max-w-[600px]">
|
||||
<ChartData laneId={silo.LocationID} />
|
||||
|
||||
<div className="flex justify-end m-1 gap-3">
|
||||
<AttachSilo silo={silo} />
|
||||
<DetachSilo silo={silo} />
|
||||
<Button variant="outline">
|
||||
<Link
|
||||
to={"/siloAdjustments/$hist"}
|
||||
params={{ hist: silo.LocationID }}
|
||||
>
|
||||
Historical Data
|
||||
</Link>
|
||||
</Button>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</LstCard>
|
||||
);
|
||||
<div className="flex justify-end m-1 gap-3">
|
||||
<AttachSilo silo={silo} />
|
||||
<DetachSilo silo={silo} />
|
||||
<Button variant="outline">
|
||||
<Link
|
||||
to={"/siloAdjustments/$hist"}
|
||||
params={{ hist: silo.LocationID }}
|
||||
>
|
||||
Historical Data
|
||||
</Link>
|
||||
</Button>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</LstCard>
|
||||
);
|
||||
}
|
||||
|
||||
const Disclaimer = () => {
|
||||
return (
|
||||
<TooltipProvider>
|
||||
<Tooltip>
|
||||
<TooltipTrigger asChild>
|
||||
<CircleAlert className="ml-1 w-[14px]" />
|
||||
</TooltipTrigger>
|
||||
<TooltipContent className="max-w-48">
|
||||
<p className="text-pretty">
|
||||
If you have had this page open for a period of time
|
||||
before submitting your data, there is a chance that the
|
||||
stock levels will be different from the ones you see
|
||||
above
|
||||
</p>
|
||||
</TooltipContent>
|
||||
</Tooltip>
|
||||
</TooltipProvider>
|
||||
);
|
||||
return (
|
||||
<TooltipProvider>
|
||||
<Tooltip>
|
||||
<TooltipTrigger asChild>
|
||||
<CircleAlert className="ml-1 w-[14px]" />
|
||||
</TooltipTrigger>
|
||||
<TooltipContent className="max-w-48">
|
||||
<p className="text-pretty">
|
||||
If you have had this page open for a period of time before
|
||||
submitting your data, there is a chance that the stock levels will
|
||||
be different from the ones you see above
|
||||
</p>
|
||||
</TooltipContent>
|
||||
</Tooltip>
|
||||
</TooltipProvider>
|
||||
);
|
||||
};
|
||||
|
||||
@@ -1,273 +1,256 @@
|
||||
import { useQuery } from "@tanstack/react-query";
|
||||
import { LstCard } from "@/components/extendedUI/LstCard";
|
||||
|
||||
import { ScrollArea } from "@/components/ui/scroll-area";
|
||||
import { Skeleton } from "@/components/ui/skeleton";
|
||||
import {
|
||||
Table,
|
||||
TableBody,
|
||||
TableCell,
|
||||
TableHead,
|
||||
TableHeader,
|
||||
TableRow,
|
||||
Table,
|
||||
TableBody,
|
||||
TableCell,
|
||||
TableHead,
|
||||
TableHeader,
|
||||
TableRow,
|
||||
} from "@/components/ui/table";
|
||||
import { useSessionStore } from "@/lib/store/sessionStore";
|
||||
import { useGetUserRoles } from "@/lib/store/useGetRoles";
|
||||
import { useModuleStore } from "@/lib/store/useModuleStore";
|
||||
import { useSettingStore } from "@/lib/store/useSettings";
|
||||
import { LotType } from "@/types/lots";
|
||||
import { getlots } from "@/utils/querys/production/lots";
|
||||
import { useQuery } from "@tanstack/react-query";
|
||||
import ManualPrint from "./ManualPrinting/ManualPrint";
|
||||
import ManualPrintForm from "./ManualPrinting/ManualPrintForm";
|
||||
import { ScrollArea } from "@/components/ui/scroll-area";
|
||||
import { useGetUserRoles } from "@/lib/store/useGetRoles";
|
||||
import { useModuleStore } from "@/lib/store/useModuleStore";
|
||||
|
||||
let lotColumns = [
|
||||
{
|
||||
key: "MachineDescription",
|
||||
label: "Machine",
|
||||
},
|
||||
{
|
||||
key: "AV",
|
||||
label: "AV",
|
||||
},
|
||||
{
|
||||
key: "Alias",
|
||||
label: "AvDescription",
|
||||
},
|
||||
{
|
||||
key: "lot",
|
||||
label: "LotNumber",
|
||||
},
|
||||
{
|
||||
key: "ProlinkLot",
|
||||
label: "ProlinkLot",
|
||||
},
|
||||
{
|
||||
key: "PlannedQTY",
|
||||
label: "PlannedQTY",
|
||||
},
|
||||
{
|
||||
key: "Produced",
|
||||
label: "Produced",
|
||||
},
|
||||
{
|
||||
key: "Remaining",
|
||||
label: "Remaining",
|
||||
},
|
||||
{
|
||||
key: "overPrinting",
|
||||
label: "Overprinting",
|
||||
},
|
||||
// {
|
||||
// key: "lastProlinkUpdate",
|
||||
// label: "Last ProlinkCheck",
|
||||
// },
|
||||
// {
|
||||
// key: "printLabel",
|
||||
// label: "Print Label",
|
||||
// },
|
||||
{
|
||||
key: "MachineDescription",
|
||||
label: "Machine",
|
||||
},
|
||||
{
|
||||
key: "AV",
|
||||
label: "AV",
|
||||
},
|
||||
{
|
||||
key: "Alias",
|
||||
label: "AvDescription",
|
||||
},
|
||||
{
|
||||
key: "lot",
|
||||
label: "LotNumber",
|
||||
},
|
||||
{
|
||||
key: "ProlinkLot",
|
||||
label: "ProlinkLot",
|
||||
},
|
||||
{
|
||||
key: "PlannedQTY",
|
||||
label: "PlannedQTY",
|
||||
},
|
||||
{
|
||||
key: "Produced",
|
||||
label: "Produced",
|
||||
},
|
||||
{
|
||||
key: "Remaining",
|
||||
label: "Remaining",
|
||||
},
|
||||
{
|
||||
key: "overPrinting",
|
||||
label: "Overprinting",
|
||||
},
|
||||
// {
|
||||
// key: "lastProlinkUpdate",
|
||||
// label: "Last ProlinkCheck",
|
||||
// },
|
||||
// {
|
||||
// key: "printLabel",
|
||||
// label: "Print Label",
|
||||
// },
|
||||
];
|
||||
export default function Lots() {
|
||||
const { data, isError, isLoading } = useQuery(getlots());
|
||||
const { user } = useSessionStore();
|
||||
const { settings } = useSettingStore();
|
||||
const { userRoles } = useGetUserRoles();
|
||||
const { modules } = useModuleStore();
|
||||
const { data, isError, isLoading } = useQuery(getlots());
|
||||
const { user } = useSessionStore();
|
||||
const { settings } = useSettingStore();
|
||||
const { userRoles } = useGetUserRoles();
|
||||
const { modules } = useModuleStore();
|
||||
|
||||
const server = settings.filter((n) => n.name === "server")[0]?.value || "";
|
||||
const server = settings.filter((n) => n.name === "server")[0]?.value || "";
|
||||
|
||||
const roles = ["systemAdmin", "technician", "admin", "manager", "operator"];
|
||||
const lotdata = data ? data : [];
|
||||
const roles = ["systemAdmin", "technician", "admin", "manager", "operator"];
|
||||
const lotdata = data ? data : [];
|
||||
|
||||
const module = modules.filter((n) => n.name === "logistics");
|
||||
const module = modules.filter((n) => n.name === "logistics");
|
||||
|
||||
const accessRoles = userRoles.filter(
|
||||
(n) => n.module_id === module[0]?.module_id
|
||||
) as any;
|
||||
const accessRoles = userRoles.filter(
|
||||
(n: any) => n.module === module[0]?.name,
|
||||
) as any;
|
||||
|
||||
if (user && roles.includes(accessRoles[0]?.role)) {
|
||||
//width = 1280;
|
||||
const checkCol = lotColumns.some((l) => l.key === "printLabel");
|
||||
if (!checkCol) {
|
||||
lotColumns = [
|
||||
...lotColumns,
|
||||
{
|
||||
key: "printLabel",
|
||||
label: "Print Label",
|
||||
},
|
||||
];
|
||||
}
|
||||
}
|
||||
if (user && roles.includes(accessRoles[0]?.role)) {
|
||||
//width = 1280;
|
||||
const checkCol = lotColumns.some((l) => l.key === "printLabel");
|
||||
if (!checkCol) {
|
||||
lotColumns = [
|
||||
...lotColumns,
|
||||
{
|
||||
key: "printLabel",
|
||||
label: "Print Label",
|
||||
},
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
if (isError) {
|
||||
return (
|
||||
<div className="m-2 p-2 min-h-2/5">
|
||||
<ScrollArea className="max-h-1/2 rounded-md border p-4">
|
||||
<LstCard>
|
||||
<p className="text-center">Current Assigned lots</p>
|
||||
<Table>
|
||||
<TableHeader>
|
||||
<TableRow>
|
||||
{lotColumns.map((l) => (
|
||||
<TableHead key={l.key}>
|
||||
{l.label}
|
||||
</TableHead>
|
||||
))}
|
||||
</TableRow>
|
||||
</TableHeader>
|
||||
if (isError) {
|
||||
return (
|
||||
<div className="m-2 p-2 min-h-2/5">
|
||||
<ScrollArea className="max-h-1/2 rounded-md border p-4">
|
||||
<LstCard>
|
||||
<p className="text-center">Current Assigned lots</p>
|
||||
<Table>
|
||||
<TableHeader>
|
||||
<TableRow>
|
||||
{lotColumns.map((l) => (
|
||||
<TableHead key={l.key}>{l.label}</TableHead>
|
||||
))}
|
||||
</TableRow>
|
||||
</TableHeader>
|
||||
|
||||
<TableBody>
|
||||
{Array(10)
|
||||
.fill(0)
|
||||
.map((_, i) => (
|
||||
<TableRow key={i}>
|
||||
<TableCell className="font-medium">
|
||||
<Skeleton className="h-4" />
|
||||
</TableCell>
|
||||
<TableCell>
|
||||
<Skeleton className="h-4" />
|
||||
</TableCell>
|
||||
<TableCell>
|
||||
<Skeleton className="h-4" />
|
||||
</TableCell>
|
||||
<TableCell>
|
||||
<Skeleton className="h-4" />
|
||||
</TableCell>
|
||||
<TableCell>
|
||||
<Skeleton className="h-4" />
|
||||
</TableCell>
|
||||
<TableCell>
|
||||
<Skeleton className="h-4" />
|
||||
</TableCell>
|
||||
<TableCell>
|
||||
<Skeleton className="h-4" />
|
||||
</TableCell>
|
||||
<TableCell>
|
||||
<Skeleton className="h-4" />
|
||||
</TableCell>
|
||||
<TableCell>
|
||||
<Skeleton className="h-4" />
|
||||
</TableCell>
|
||||
</TableRow>
|
||||
))}
|
||||
</TableBody>
|
||||
</Table>
|
||||
</LstCard>
|
||||
</ScrollArea>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
return (
|
||||
<LstCard className="m-2 p-2 min-h-2/5">
|
||||
<ScrollArea className="h-[400px]">
|
||||
<p className="text-center">Current Assigned lots</p>
|
||||
<Table>
|
||||
<TableHeader>
|
||||
<TableRow>
|
||||
{lotColumns.map((l) => (
|
||||
<TableHead key={l.key}>{l.label}</TableHead>
|
||||
))}
|
||||
</TableRow>
|
||||
</TableHeader>
|
||||
{isLoading ? (
|
||||
<>
|
||||
<TableBody>
|
||||
{Array(10)
|
||||
.fill(0)
|
||||
.map((_, i) => (
|
||||
<TableRow key={i}>
|
||||
<TableCell className="font-medium">
|
||||
<Skeleton className="h-4" />
|
||||
</TableCell>
|
||||
<TableCell>
|
||||
<Skeleton className="h-4" />
|
||||
</TableCell>
|
||||
<TableCell>
|
||||
<Skeleton className="h-4" />
|
||||
</TableCell>
|
||||
<TableCell>
|
||||
<Skeleton className="h-4" />
|
||||
</TableCell>
|
||||
<TableCell>
|
||||
<Skeleton className="h-4" />
|
||||
</TableCell>
|
||||
<TableCell>
|
||||
<Skeleton className="h-4" />
|
||||
</TableCell>
|
||||
<TableCell>
|
||||
<Skeleton className="h-4" />
|
||||
</TableCell>
|
||||
<TableCell>
|
||||
<Skeleton className="h-4" />
|
||||
</TableCell>
|
||||
<TableCell>
|
||||
<Skeleton className="h-4" />
|
||||
</TableCell>
|
||||
</TableRow>
|
||||
))}
|
||||
</TableBody>
|
||||
</>
|
||||
) : (
|
||||
<TableBody>
|
||||
{lotdata.map((lot: LotType) => (
|
||||
<TableRow key={lot.LabelOnlineID}>
|
||||
<TableCell className="font-medium">
|
||||
{lot.MachineLocation}
|
||||
</TableCell>
|
||||
<TableCell className="font-medium">
|
||||
{lot.AV}
|
||||
</TableCell>
|
||||
<TableCell className="font-medium">
|
||||
{lot.Alias}
|
||||
</TableCell>
|
||||
<TableCell
|
||||
className={`font-medium ${lot.ProlinkLot != lot.lot ? "text-red-500" : ""}`}
|
||||
>
|
||||
{lot.lot}
|
||||
</TableCell>
|
||||
<TableCell
|
||||
className={`font-medium ${lot.ProlinkLot != lot.lot ? "text-red-500" : ""}`}
|
||||
>
|
||||
{lot.ProlinkLot}
|
||||
</TableCell>
|
||||
<TableCell className="font-medium">
|
||||
{lot.PlannedQTY}
|
||||
</TableCell>
|
||||
<TableCell className="font-medium">
|
||||
{lot.Produced}
|
||||
</TableCell>
|
||||
<TableCell className="font-medium">
|
||||
{lot.Remaining}
|
||||
</TableCell>
|
||||
<TableCell className="font-medium">
|
||||
{lot.overPrinting}
|
||||
</TableCell>
|
||||
{user &&
|
||||
roles.includes(
|
||||
accessRoles[0]?.role
|
||||
) && (
|
||||
<>
|
||||
{server === "usday1vms006" ||
|
||||
server === "localhost" ? (
|
||||
<>
|
||||
<TableCell className="flex justify-center">
|
||||
<ManualPrintForm />
|
||||
</TableCell>
|
||||
</>
|
||||
) : (
|
||||
<TableCell className="flex justify-center">
|
||||
<ManualPrint
|
||||
lot={lot}
|
||||
/>
|
||||
</TableCell>
|
||||
)}
|
||||
</>
|
||||
)}
|
||||
</TableRow>
|
||||
))}
|
||||
</TableBody>
|
||||
)}
|
||||
</Table>
|
||||
</ScrollArea>
|
||||
</LstCard>
|
||||
);
|
||||
<TableBody>
|
||||
{Array(10)
|
||||
.fill(0)
|
||||
.map((_, i) => (
|
||||
<TableRow key={i}>
|
||||
<TableCell className="font-medium">
|
||||
<Skeleton className="h-4" />
|
||||
</TableCell>
|
||||
<TableCell>
|
||||
<Skeleton className="h-4" />
|
||||
</TableCell>
|
||||
<TableCell>
|
||||
<Skeleton className="h-4" />
|
||||
</TableCell>
|
||||
<TableCell>
|
||||
<Skeleton className="h-4" />
|
||||
</TableCell>
|
||||
<TableCell>
|
||||
<Skeleton className="h-4" />
|
||||
</TableCell>
|
||||
<TableCell>
|
||||
<Skeleton className="h-4" />
|
||||
</TableCell>
|
||||
<TableCell>
|
||||
<Skeleton className="h-4" />
|
||||
</TableCell>
|
||||
<TableCell>
|
||||
<Skeleton className="h-4" />
|
||||
</TableCell>
|
||||
<TableCell>
|
||||
<Skeleton className="h-4" />
|
||||
</TableCell>
|
||||
</TableRow>
|
||||
))}
|
||||
</TableBody>
|
||||
</Table>
|
||||
</LstCard>
|
||||
</ScrollArea>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
return (
|
||||
<LstCard className="m-2 p-2 min-h-2/5">
|
||||
<ScrollArea className="h-[400px]">
|
||||
<p className="text-center">Current Assigned lots</p>
|
||||
<Table>
|
||||
<TableHeader>
|
||||
<TableRow>
|
||||
{lotColumns.map((l) => (
|
||||
<TableHead key={l.key}>{l.label}</TableHead>
|
||||
))}
|
||||
</TableRow>
|
||||
</TableHeader>
|
||||
{isLoading ? (
|
||||
<>
|
||||
<TableBody>
|
||||
{Array(10)
|
||||
.fill(0)
|
||||
.map((_, i) => (
|
||||
<TableRow key={i}>
|
||||
<TableCell className="font-medium">
|
||||
<Skeleton className="h-4" />
|
||||
</TableCell>
|
||||
<TableCell>
|
||||
<Skeleton className="h-4" />
|
||||
</TableCell>
|
||||
<TableCell>
|
||||
<Skeleton className="h-4" />
|
||||
</TableCell>
|
||||
<TableCell>
|
||||
<Skeleton className="h-4" />
|
||||
</TableCell>
|
||||
<TableCell>
|
||||
<Skeleton className="h-4" />
|
||||
</TableCell>
|
||||
<TableCell>
|
||||
<Skeleton className="h-4" />
|
||||
</TableCell>
|
||||
<TableCell>
|
||||
<Skeleton className="h-4" />
|
||||
</TableCell>
|
||||
<TableCell>
|
||||
<Skeleton className="h-4" />
|
||||
</TableCell>
|
||||
<TableCell>
|
||||
<Skeleton className="h-4" />
|
||||
</TableCell>
|
||||
</TableRow>
|
||||
))}
|
||||
</TableBody>
|
||||
</>
|
||||
) : (
|
||||
<TableBody>
|
||||
{lotdata.map((lot: LotType) => (
|
||||
<TableRow key={lot.LabelOnlineID}>
|
||||
<TableCell className="font-medium">
|
||||
{lot.MachineLocation}
|
||||
</TableCell>
|
||||
<TableCell className="font-medium">{lot.AV}</TableCell>
|
||||
<TableCell className="font-medium">{lot.Alias}</TableCell>
|
||||
<TableCell
|
||||
className={`font-medium ${lot.ProlinkLot != lot.lot ? "text-red-500" : ""}`}
|
||||
>
|
||||
{lot.lot}
|
||||
</TableCell>
|
||||
<TableCell
|
||||
className={`font-medium ${lot.ProlinkLot != lot.lot ? "text-red-500" : ""}`}
|
||||
>
|
||||
{lot.ProlinkLot}
|
||||
</TableCell>
|
||||
<TableCell className="font-medium">
|
||||
{lot.PlannedQTY}
|
||||
</TableCell>
|
||||
<TableCell className="font-medium">{lot.Produced}</TableCell>
|
||||
<TableCell className="font-medium">{lot.Remaining}</TableCell>
|
||||
<TableCell className="font-medium">
|
||||
{lot.overPrinting}
|
||||
</TableCell>
|
||||
{user && roles.includes(accessRoles[0]?.role) && (
|
||||
<>
|
||||
{server === "usday1vms006" || server === "localhost" ? (
|
||||
<>
|
||||
<TableCell className="flex justify-center">
|
||||
<ManualPrintForm />
|
||||
</TableCell>
|
||||
</>
|
||||
) : (
|
||||
<TableCell className="flex justify-center">
|
||||
<ManualPrint lot={lot} />
|
||||
</TableCell>
|
||||
)}
|
||||
</>
|
||||
)}
|
||||
</TableRow>
|
||||
))}
|
||||
</TableBody>
|
||||
)}
|
||||
</Table>
|
||||
</ScrollArea>
|
||||
</LstCard>
|
||||
);
|
||||
}
|
||||
|
||||
@@ -1,32 +1,67 @@
|
||||
import { QueryClient, QueryClientProvider } from "@tanstack/react-query";
|
||||
import { useModuleStore } from "../../lib/store/useModuleStore";
|
||||
import { useEffect } from "react";
|
||||
import { useSettingStore } from "@/lib/store/useSettings";
|
||||
import { useSessionStore } from "@/lib/store/sessionStore";
|
||||
import { useAuthStore } from "@/lib/store/useAuthStore";
|
||||
import { useGetUserRoles } from "@/lib/store/useGetRoles";
|
||||
import { useSettingStore } from "@/lib/store/useSettings";
|
||||
import { useSubModuleStore } from "@/lib/store/useSubModuleStore";
|
||||
import { useModuleStore } from "../../lib/store/useModuleStore";
|
||||
|
||||
const queryClient = new QueryClient();
|
||||
|
||||
export const SessionProvider = ({
|
||||
children,
|
||||
}: {
|
||||
children: React.ReactNode;
|
||||
}) => {
|
||||
const { fetchModules } = useModuleStore();
|
||||
const { fetchSettings } = useSettingStore();
|
||||
const { fetchUserRoles } = useGetUserRoles();
|
||||
const { fetchSubModules } = useSubModuleStore();
|
||||
const reAuth = async (username: string, password: string) => {
|
||||
const { setSession } = useSessionStore();
|
||||
try {
|
||||
const response = await fetch("/api/auth/login", {
|
||||
method: "POST",
|
||||
headers: {
|
||||
"Content-Type": "application/json",
|
||||
},
|
||||
body: JSON.stringify({
|
||||
username: username,
|
||||
password: password,
|
||||
}),
|
||||
});
|
||||
|
||||
useEffect(() => {
|
||||
fetchModules();
|
||||
fetchSettings();
|
||||
fetchUserRoles();
|
||||
fetchSubModules();
|
||||
}, []);
|
||||
const data = await response.json();
|
||||
|
||||
return (
|
||||
<QueryClientProvider client={queryClient}>
|
||||
{children}
|
||||
</QueryClientProvider>
|
||||
);
|
||||
// Store token in localStorage
|
||||
// localStorage.setItem("auth_token", data.data.token);
|
||||
if (data.success) {
|
||||
const prod = btoa(`${username.toLowerCase()}:${password}`);
|
||||
const prodUser = { ...data.user, prod: prod };
|
||||
|
||||
setSession(prodUser, data.data.token);
|
||||
}
|
||||
|
||||
//console.log(data);
|
||||
} catch (err) {
|
||||
console.log(err);
|
||||
}
|
||||
};
|
||||
export const SessionProvider = ({
|
||||
children,
|
||||
}: {
|
||||
children: React.ReactNode;
|
||||
}) => {
|
||||
const { fetchModules } = useModuleStore();
|
||||
const { fetchSettings } = useSettingStore();
|
||||
const { fetchUserRoles } = useGetUserRoles();
|
||||
const { fetchSubModules } = useSubModuleStore();
|
||||
const { username, password } = useAuthStore();
|
||||
|
||||
useEffect(() => {
|
||||
if (username !== "") {
|
||||
reAuth(username, password);
|
||||
}
|
||||
|
||||
fetchModules();
|
||||
fetchSettings();
|
||||
fetchUserRoles();
|
||||
fetchSubModules();
|
||||
}, []);
|
||||
|
||||
return (
|
||||
<QueryClientProvider client={queryClient}>{children}</QueryClientProvider>
|
||||
);
|
||||
};
|
||||
|
||||
@@ -1,16 +1,19 @@
|
||||
import {useSessionStore} from "@/lib/store/sessionStore";
|
||||
import {useRouter} from "@tanstack/react-router";
|
||||
import { useRouter } from "@tanstack/react-router";
|
||||
import { useSessionStore } from "@/lib/store/sessionStore";
|
||||
import { useAuthStore } from "@/lib/store/useAuthStore";
|
||||
|
||||
export const useLogout = () => {
|
||||
const {clearSession} = useSessionStore();
|
||||
const router = useRouter();
|
||||
const logout = async () => {
|
||||
router.invalidate();
|
||||
router.clearCache();
|
||||
clearSession();
|
||||
const { clearSession } = useSessionStore();
|
||||
const { clearUser } = useAuthStore();
|
||||
const router = useRouter();
|
||||
const logout = async () => {
|
||||
router.invalidate();
|
||||
router.clearCache();
|
||||
clearSession();
|
||||
clearUser();
|
||||
|
||||
window.location.reload();
|
||||
};
|
||||
window.location.reload();
|
||||
};
|
||||
|
||||
return logout;
|
||||
return logout;
|
||||
};
|
||||
|
||||
@@ -1,60 +1,87 @@
|
||||
import { useQuery } from "@tanstack/react-query";
|
||||
import { useSessionStore } from "../lib/store/sessionStore";
|
||||
import axios from "axios";
|
||||
import { useEffect } from "react";
|
||||
import { useSessionStore } from "../lib/store/sessionStore";
|
||||
|
||||
const fetchSession = async () => {
|
||||
const token = localStorage.getItem("auth_token");
|
||||
const token = localStorage.getItem("auth_token");
|
||||
|
||||
if (!token) {
|
||||
throw new Error("No token found");
|
||||
}
|
||||
if (!token) {
|
||||
throw new Error("No token found");
|
||||
}
|
||||
try {
|
||||
const res = await axios.get("/api/auth/session", {
|
||||
headers: {
|
||||
"Content-Type": "application/json",
|
||||
},
|
||||
withCredentials: true,
|
||||
});
|
||||
|
||||
const res = await fetch("/api/auth/session", {
|
||||
headers: {
|
||||
"Content-Type": "application/json",
|
||||
Authorization: `Bearer ${token}`,
|
||||
},
|
||||
});
|
||||
// console.log(res);
|
||||
if (!res.ok) {
|
||||
localStorage.removeItem("auth_token");
|
||||
// remove these for a while if no session just until fully to 2.0 and clearly no one has ran lstv1 in a long time
|
||||
localStorage.removeItem("ally-supports-cache");
|
||||
localStorage.removeItem("auth-storage");
|
||||
localStorage.removeItem("nextauth.message");
|
||||
localStorage.removeItem("prod");
|
||||
if (res.status === 401) {
|
||||
localStorage.removeItem("auth_token");
|
||||
// remove these for a while if no session just until fully to 2.0 and clearly no one has ran lstv1 in a long time
|
||||
localStorage.removeItem("ally-supports-cache");
|
||||
localStorage.removeItem("auth-storage");
|
||||
localStorage.removeItem("nextauth.message");
|
||||
localStorage.removeItem("prod");
|
||||
|
||||
throw new Error("Session not found");
|
||||
}
|
||||
throw new Error("Session not found");
|
||||
}
|
||||
|
||||
return res.json();
|
||||
const userRoles = await axios.get("/api/auth/getuseraccess", {
|
||||
withCredentials: true,
|
||||
});
|
||||
|
||||
const userData = {
|
||||
...res.data,
|
||||
data: {
|
||||
...res.data.data,
|
||||
user: {
|
||||
...res.data.data.user,
|
||||
roles: userRoles.data.data,
|
||||
},
|
||||
token: "Just a token as this will be removed in the future",
|
||||
},
|
||||
};
|
||||
|
||||
return userData;
|
||||
} catch (error) {
|
||||
localStorage.removeItem("auth_token");
|
||||
// remove these for a while if no session just until fully to 2.0 and clearly no one has ran lstv1 in a long time
|
||||
localStorage.removeItem("ally-supports-cache");
|
||||
localStorage.removeItem("auth-storage");
|
||||
localStorage.removeItem("nextauth.message");
|
||||
localStorage.removeItem("prod");
|
||||
|
||||
throw new Error("Session not found");
|
||||
}
|
||||
};
|
||||
|
||||
export const useSession = () => {
|
||||
const { setSession, clearSession, token } = useSessionStore();
|
||||
const { setSession, clearSession, token } = useSessionStore();
|
||||
|
||||
// Fetch session only if token is available
|
||||
const { data, status, error } = useQuery({
|
||||
queryKey: ["session"],
|
||||
queryFn: fetchSession,
|
||||
enabled: !!token, // Prevents query if token is null
|
||||
staleTime: 60 * 1000,
|
||||
gcTime: 10 * 60 * 1000, // 10 mins
|
||||
refetchOnWindowFocus: true,
|
||||
//refetchInterval: 1000 * 60 * 2, // Auto-refetch every 2 minutes
|
||||
});
|
||||
useEffect(() => {
|
||||
if (data) {
|
||||
setSession(data.data.user, data.data.token);
|
||||
}
|
||||
if (error) {
|
||||
clearSession();
|
||||
}
|
||||
}, [data, error]);
|
||||
// Fetch session only if token is available
|
||||
const { data, status, error } = useQuery({
|
||||
queryKey: ["session"],
|
||||
queryFn: fetchSession,
|
||||
enabled: !!token, // Prevents query if token is null
|
||||
staleTime: 60 * 1000,
|
||||
//gcTime: 10 * 60 * 1000, // 10 mins
|
||||
refetchOnWindowFocus: true,
|
||||
refetchInterval: 1000 * 60 * 2, // Auto-refetch every 2 minutes
|
||||
});
|
||||
useEffect(() => {
|
||||
if (data) {
|
||||
setSession(data.data.user, data.data.token);
|
||||
}
|
||||
if (error) {
|
||||
clearSession();
|
||||
}
|
||||
}, [data, error]);
|
||||
|
||||
return {
|
||||
session: data && token ? { user: data.user, token: data.token } : null,
|
||||
status,
|
||||
error,
|
||||
};
|
||||
return {
|
||||
session: data && token ? { user: data.user, token: data.token } : null,
|
||||
status,
|
||||
error,
|
||||
};
|
||||
};
|
||||
|
||||
@@ -1,43 +1,52 @@
|
||||
import {User} from "@/types/users";
|
||||
import axios from "axios";
|
||||
import {create} from "zustand";
|
||||
import { create } from "zustand";
|
||||
import { User } from "@/types/users";
|
||||
|
||||
export type SessionState = {
|
||||
user: User | null;
|
||||
token: string | null;
|
||||
setSession: (user: User | null, token: string | null) => void;
|
||||
clearSession: () => void;
|
||||
user: User | null;
|
||||
token: string | null;
|
||||
setSession: (user: User | null, token: string | null) => void;
|
||||
clearSession: () => void;
|
||||
};
|
||||
|
||||
export const useSessionStore = create<SessionState>((set) => {
|
||||
// Initialize token from localStorage, but user remains in memory only
|
||||
const storedToken = localStorage.getItem("auth_token");
|
||||
// Initialize token from localStorage, but user remains in memory only
|
||||
const storedToken = localStorage.getItem("auth_token");
|
||||
|
||||
return {
|
||||
user: null, // User is NOT stored in localStorage
|
||||
token: storedToken || null,
|
||||
return {
|
||||
user: null, // User is NOT stored in localStorage
|
||||
token: storedToken || null,
|
||||
|
||||
setSession: async (user: any, token) => {
|
||||
if (token) {
|
||||
localStorage.setItem("auth_token", token);
|
||||
const response = await axios.get("/api/auth/getuseraccess", {
|
||||
headers: {Authorization: `Bearer ${token}`},
|
||||
});
|
||||
const data = response.data; //await response.json();
|
||||
user = {...user, roles: data.data};
|
||||
} else {
|
||||
localStorage.removeItem("auth_token");
|
||||
}
|
||||
setSession: async (user: any, token) => {
|
||||
if (token) {
|
||||
localStorage.setItem("auth_token", token);
|
||||
const response = await axios.get("/api/auth/session", {
|
||||
withCredentials: true,
|
||||
});
|
||||
const userRoles = await axios.get("/api/auth/getuseraccess", {
|
||||
withCredentials: true,
|
||||
});
|
||||
const data = response.data; //await response.json();
|
||||
|
||||
//console.log("Setting session:", {user, token});
|
||||
set({user, token});
|
||||
},
|
||||
const rawUser = data.data.user;
|
||||
// user.map((u: any) => ({
|
||||
// ...u,
|
||||
// roles: userRoles.data.data,
|
||||
// }));
|
||||
user = { ...rawUser, roles: userRoles.data.data };
|
||||
} else {
|
||||
localStorage.removeItem("auth_token");
|
||||
}
|
||||
|
||||
clearSession: () => {
|
||||
localStorage.removeItem("auth_token");
|
||||
set({user: null, token: null});
|
||||
},
|
||||
};
|
||||
//console.log("Setting session:", {user, token});
|
||||
set({ user, token });
|
||||
},
|
||||
|
||||
clearSession: () => {
|
||||
localStorage.removeItem("auth_token");
|
||||
set({ user: null, token: null });
|
||||
},
|
||||
};
|
||||
});
|
||||
|
||||
export type SessionType = ReturnType<typeof useSessionStore>;
|
||||
|
||||
15
lstV2/frontend/src/lib/store/useAuthStore.ts
Normal file
15
lstV2/frontend/src/lib/store/useAuthStore.ts
Normal file
@@ -0,0 +1,15 @@
|
||||
import { create } from "zustand";
|
||||
|
||||
interface SettingState {
|
||||
username: string;
|
||||
password: string;
|
||||
clearUser: () => void;
|
||||
setUserInfo: (username: string, password: string) => void;
|
||||
}
|
||||
|
||||
export const useAuthStore = create<SettingState>()((set) => ({
|
||||
username: "",
|
||||
password: "",
|
||||
setUserInfo: (username, password) => set({ username, password }),
|
||||
clearUser: () => set({ username: "", password: "" }),
|
||||
}));
|
||||
@@ -1,38 +1,41 @@
|
||||
import axios from "axios";
|
||||
import { create } from "zustand";
|
||||
import { Modules } from "@/types/modules";
|
||||
import axios from "axios";
|
||||
|
||||
interface SettingState {
|
||||
userRoles: Modules[];
|
||||
userRoles: Modules[];
|
||||
|
||||
fetchUserRoles: () => Promise<void>;
|
||||
setUserRoles: (userRoles: Modules[]) => void;
|
||||
fetchUserRoles: () => Promise<void>;
|
||||
setUserRoles: (userRoles: Modules[]) => void;
|
||||
}
|
||||
interface FetchModulesResponse {
|
||||
data: Modules[];
|
||||
data: Modules[];
|
||||
}
|
||||
|
||||
export const useGetUserRoles = create<SettingState>()((set) => ({
|
||||
userRoles: [],
|
||||
setUserRoles: (userRoles) => set({ userRoles }),
|
||||
fetchUserRoles: async () => {
|
||||
try {
|
||||
//const response = await axios.get<{data: Setting[]}>(`${process.env.NEXT_PUBLIC_URL}/api/settings/client`);
|
||||
const token = localStorage.getItem("auth_token");
|
||||
if (token) {
|
||||
const response = await axios.get("/api/auth/getuseraccess", {
|
||||
headers: { Authorization: `Bearer ${token}` },
|
||||
});
|
||||
const data: FetchModulesResponse = response.data; //await response.json();
|
||||
userRoles: [],
|
||||
setUserRoles: (userRoles) => set({ userRoles }),
|
||||
fetchUserRoles: async () => {
|
||||
try {
|
||||
//const response = await axios.get<{data: Setting[]}>(`${process.env.NEXT_PUBLIC_URL}/api/settings/client`);
|
||||
const token = localStorage.getItem("auth_token");
|
||||
if (token) {
|
||||
const response = await axios.get("/api/auth/getuseraccess", {
|
||||
withCredentials: true,
|
||||
});
|
||||
const data: FetchModulesResponse = response?.data; //await response.json();
|
||||
|
||||
//console.log(data);
|
||||
set({ userRoles: data.data });
|
||||
} else {
|
||||
//console.log(data);
|
||||
set({ userRoles: [] });
|
||||
}
|
||||
} catch (error) {
|
||||
console.error("Failed to fetch settings:", error);
|
||||
}
|
||||
},
|
||||
if (response.status === 401) {
|
||||
set({ userRoles: [] });
|
||||
}
|
||||
set({ userRoles: data?.data });
|
||||
} else {
|
||||
//console.log(data);
|
||||
set({ userRoles: [] });
|
||||
}
|
||||
} catch (error) {
|
||||
set({ userRoles: [] });
|
||||
console.error("Failed to fetch settings:", error);
|
||||
}
|
||||
},
|
||||
}));
|
||||
|
||||
@@ -1,44 +1,52 @@
|
||||
import { Modules } from "@/types/modules";
|
||||
import { User } from "@/types/users";
|
||||
|
||||
// user will need access to the module.
|
||||
// users role will determine there visual access
|
||||
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[0]);
|
||||
// userroles and filter out by the module id,
|
||||
export function hasAccess(user: any, moduleName: string | null): boolean {
|
||||
//console.log("has access user", user, moduleName);
|
||||
// get the modules for the id
|
||||
|
||||
const roleCheck: any = user?.roles.find(
|
||||
(role) => role.module_id === filteredModule[0].module_id
|
||||
);
|
||||
const filteredModule = user?.roles?.filter(
|
||||
(f: any) => f.module === moduleName,
|
||||
);
|
||||
//console.log(filteredModule[0]);
|
||||
// userroles and filter out by the module id,
|
||||
//console.log("Has Module access", filteredModule);
|
||||
// const roleCheck: any = user?.roles.find(
|
||||
// (role) => role.module_id === filteredModule[0].module_id,
|
||||
// );
|
||||
|
||||
if (filteredModule[0].roles.includes(roleCheck?.role)) {
|
||||
return true;
|
||||
}
|
||||
//if(filteredModule[0].roles.includes(roleCheck.))
|
||||
return false;
|
||||
if (filteredModule && filteredModule.length > 0) {
|
||||
return true;
|
||||
}
|
||||
//if(filteredModule[0].roles.includes(roleCheck.))
|
||||
return false;
|
||||
}
|
||||
|
||||
export function hasPageAccess(
|
||||
user: User | null,
|
||||
role: any,
|
||||
module_id: string
|
||||
user: User | null,
|
||||
role: any,
|
||||
moduleName: string,
|
||||
): boolean {
|
||||
if (role.includes("viewer")) return true;
|
||||
if (!user) return false;
|
||||
if (role.includes("viewer")) return true;
|
||||
if (!user) return false;
|
||||
|
||||
// get only the module in the user profile
|
||||
//console.log(user);
|
||||
const userRole = user?.roles.filter((role) => role.module_id === module_id);
|
||||
//console.log(userRole[0]?.role);
|
||||
// if (role.includes(userRole[0]?.role)) {
|
||||
const userRole = user?.roles.filter(
|
||||
(role: any) => role.module === moduleName,
|
||||
);
|
||||
|
||||
// return true};
|
||||
if (userRole.length !== 0) return true;
|
||||
return false;
|
||||
//console.log(user);
|
||||
|
||||
// if (role.includes(userRole[0]?.role)) {
|
||||
|
||||
// return true};
|
||||
//if (userRole.length > 0) return true;
|
||||
if (userRole.length >= 1) {
|
||||
//console.log(userRole);
|
||||
return true;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
|
||||
//return false;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user