This commit is contained in:
Rizky 2024-06-23 22:54:20 -07:00
parent f8d69be0df
commit 952bfb6327
2 changed files with 30 additions and 10 deletions

View File

@ -1,5 +1,7 @@
import { FC, useEffect } from "react"; import { FC, useEffect } from "react";
import { loadSession } from "./utils/load"; import { loadSession } from "./utils/load";
import { useLocal } from "lib/utils/use-local";
import { FieldLoading } from "../../..";
const w = window as unknown as { const w = window as unknown as {
user: any; user: any;
@ -7,21 +9,35 @@ const w = window as unknown as {
}; };
export type LGProps = { export type LGProps = {
salt: string; url_home: Record<string, string>;
url_home: Array<Record<string, string>>;
body: any; body: any;
}; };
export const Login: FC<LGProps> = (props) => { export const Login: FC<LGProps> = (props) => {
w.prasi_home = props.url_home[0]; const local = useLocal({ loading: true }, () => {
useEffect(() => {
try {
loadSession(); loadSession();
setTimeout(() => {
if (w.user) { if (w.user) {
const home = w.prasi_home[w.user.m_role.name]; const home = props.url_home[w.user.role];
if (home) {
navigate(home); navigate(home);
return;
} }
} catch (e: any) {} }
}, []); local.loading = false;
local.render();
}, 500);
});
if (local.loading)
return (
<div
className={css`
padding: 10px;
`}
>
<FieldLoading />
</div>
);
return <>{props.body}</>; return <>{props.body}</>;
}; };

View File

@ -12,6 +12,9 @@ export type UserSession = {
expired: Date; // second expired: Date; // second
}; };
export const registerSession = (session: RG) => { export const registerSession = (session: RG) => {
if (session.role) {
session.data.role = session.role;
}
const data = { const data = {
data: session.data, data: session.data,
expired: session.expired ? day().add(session.expired, "seconds") : null, expired: session.expired ? day().add(session.expired, "seconds") : null,
@ -25,4 +28,5 @@ export const registerSession = (session: RG) => {
localStorage.setItem("user" + id_site, JSON.stringify(data)); localStorage.setItem("user" + id_site, JSON.stringify(data));
w.user = session.data; w.user = session.data;
w.user.role = session.role;
}; };