better file structure

This commit is contained in:
wil committed 2024-08-29 18:15:17 -04:00
1 parent 41fdd489ed
commit 0d2442aa71
9 files changed
+69 -60

No files matched your search

+5 -2
View File
@@ -16,11 +16,14 @@ import {
import { Gravatar } from "../ui/Gravatar";
import { twMerge } from "tailwind-merge";
import { authentication, logout } from "~/signals";
import { authentication } from "~/signals";
export function ProfileDropdown() {
useSignals();
const { userInfo } = authentication;
const {
signals: { userInfo },
fetchers: { logout },
} = authentication;
if (!userInfo.value) {
return null;
+7 -3
View File
@@ -2,18 +2,22 @@ import { ComponentType, useEffect, useRef } from "react";
import { useNavigate, useLocation } from "react-router-dom";
import { useSignalEffect } from "@preact/signals-react/runtime";
import { Spinner } from "~/components/ui";
import { authentication, persistLogin } from "~/signals";
import { AUTH_STATUS } from "~/enums/Authentication";
import { authentication } from "~/signals";
export const withProtectionRoute = <P extends object>(
Component: ComponentType<P>,
) =>
function ProtectionRoute(rest: P) {
const {
signals: { status, userInfo },
fetchers: { persistLogin },
enums: { AUTH_STATUS },
} = authentication;
useSignalEffect(() => {
persistLogin();
});
const { status, userInfo } = authentication;
//render according to auth status
if (
status.value === AUTH_STATUS.INIT ||
+29 -34
View File
@@ -3,26 +3,43 @@ import { useNavigate, useSearchParams, Link } from "react-router-dom";
import { useSignalEffect } from "@preact/signals-react/runtime";
import { faKey, faUser } from "@fortawesome/pro-thin-svg-icons";
import { AUTH_STATUS } from "~/enums/Authentication";
import {
authentication,
login,
// logout
} from "~/signals";
import { Button, Input, Loader } from "~/components/ui";
import { authentication } from "~/signals";
export const Login = () => {
const {
signals: { status: authStatus },
fetchers: { login },
enums: { AUTH_STATUS },
} = authentication;
const navigate = useNavigate();
const [searchParams] = useSearchParams();
const formRef = useRef<HTMLFormElement | null>(null);
const { status: authStatus } = authentication;
const authLoaderMessage = getAuthLoaderMessage();
const shouldShowLoader = checkShouldShowLoader();
// const showNoAccessModal = authStatus.value === AUTH_STATUS.NO_ACCESS;
function checkShouldShowLoader() {
return (
authStatus.value === AUTH_STATUS.LOGGING ||
authStatus.value === AUTH_STATUS.LOGGED_IN
);
}
function getAuthLoaderMessage() {
if (authStatus.value === AUTH_STATUS.LOGGED_IN) {
return "Authenticated, Redirecting...";
}
if (authStatus.value === AUTH_STATUS.GET_USER_INFO) {
return "Getting User Info...";
}
return "Getting Session...";
}
// const handleClose = () => {
// if (authentication.status.value !== AUTH_STATUS.LOGGED_OUT) {
// logout();
// }
// };
const handleOnSumbit = (ev: React.FormEvent<HTMLFormElement>) => {
ev.preventDefault();
@@ -51,7 +68,7 @@ export const Login = () => {
<div className="mx-auto my-6 flex w-10/12 max-w-2xl gap-4">
<img src="/brand/Storyteller-Logo.png" alt="Storyteller Logo" />
</div>
<div className="border-ui-border bg-ui-panel relative mx-auto w-10/12 max-w-2xl overflow-hidden rounded-lg border p-6">
<div className="relative mx-auto w-10/12 max-w-2xl overflow-hidden rounded-lg border border-ui-border bg-ui-panel p-6">
<form
ref={formRef}
onSubmit={handleOnSumbit}
@@ -112,25 +129,3 @@ export const Login = () => {
</div>
);
};
// const handleClose = () => {
// if (authentication.status.value !== AUTH_STATUS.LOGGED_OUT) {
// logout();
// }
// };
const checkShouldShowLoader = () => {
return (
authentication.status.value === AUTH_STATUS.LOGGING ||
authentication.status.value === AUTH_STATUS.LOGGED_IN
);
};
const getAuthLoaderMessage = () => {
if (authentication.status.value === AUTH_STATUS.LOGGED_IN) {
return "Authenticated, Redirecting...";
}
if (authentication.status.value === AUTH_STATUS.GET_USER_INFO) {
return "Getting User Info...";
}
return "Getting Session...";
};
+7 -4
View File
@@ -1,11 +1,16 @@
import { useCallback } from "react";
import { withProtectionRoute } from "~/components/hoc";
import { authentication, logout } from "~/signals";
import { authentication } from "~/signals";
import { PanelRight, PanelBottom } from "~/components/ui";
import { ProfileDropdown, KonaContainer } from "~/components/features";
import { KonvaApp } from "~/KonvaApp";
export const Main = withProtectionRoute(() => {
const {
signals: { userInfo },
fetchers: { logout },
} = authentication;
const konaContainerCallbackRef = useCallback((node: HTMLDivElement) => {
if (node !== null) {
KonvaApp(node);
@@ -27,9 +32,7 @@ export const Main = withProtectionRoute(() => {
<ProfileDropdown />
</div>
<hr className="my-2 border-ui-divider" />
<p>
you are logged in as {authentication.userInfo.value?.display_name}
</p>
<p>you are logged in as {userInfo.value?.display_name}</p>
<button onClick={() => logout()}>Logout</button>
</PanelRight>
<PanelBottom>
File renamed without changes.
+3 -3
View File
@@ -1,4 +1,4 @@
import { authentication } from "./authentication";
import { signals } from "./signals";
import { UsersApi } from "@ApiManager/UsersApi";
import { BillingApi } from "@ApiManager/BillingApi";
@@ -8,7 +8,7 @@ import {
updateUserInfo,
setLogoutStates,
} from "./utilities";
import { AUTH_STATUS } from "~/enums/Authentication";
import { AUTH_STATUS } from "./enums";
export const logout = async (
failureCallback?: (errorMessage: string) => void,
@@ -83,7 +83,7 @@ export const signUp = async ({
export const persistLogin = async () => {
//Only run First Load, return if not
if (authentication.status.value !== AUTH_STATUS.INIT) {
if (signals.status.value !== AUTH_STATUS.INIT) {
return;
}
getUserInfoAndSubcriptions();
+9 -2
View File
@@ -1,2 +1,9 @@
export * from "./authentication";
export * from "./fetchers";
import { signals } from "./signals";
import * as fetchers from "./fetchers";
import * as enums from "./enums";
export const authentication = {
signals,
fetchers,
enums,
};
@@ -5,7 +5,7 @@ import { ActiveSubscriptions } from "@ApiManager/models/Billing";
import { LoyaltyProgram } from "@ApiManager/enums/Billing";
import { USER_FEATURE_FLAGS } from "@ApiManager/enums/UserFeatures";
import { AUTH_STATUS } from "~/enums/Authentication";
import { AUTH_STATUS } from "./enums";
const status = signal<AUTH_STATUS>(AUTH_STATUS.INIT);
const userInfo = signal<UserInfo | undefined>(undefined);
@@ -43,7 +43,7 @@ const hasPremium = computed(() => {
return false;
});
export const authentication = {
export const signals = {
status,
userInfo,
activeSubs,
+7 -10
View File
@@ -1,14 +1,11 @@
import { AUTH_STATUS } from "~/enums/Authentication";
import { AUTH_STATUS } from "./enums";
import { UserInfo } from "~/Classes/ApiManager/models/Users";
import { ActiveSubscriptions } from "@ApiManager/models/Billing";
import {
authentication,
// flushAllBackgroundLoadedMedia
} from "~/signals";
import { signals } from "./signals";
export const updateAuthStatus = (newStatus: AUTH_STATUS) => {
authentication.status.value = newStatus;
signals.status.value = newStatus;
};
export const updateUserInfo = (
@@ -17,20 +14,20 @@ export const updateUserInfo = (
) => {
if (newInfo && !flush) {
//case of updating UserInfo partly
authentication.userInfo.value = {
...authentication.userInfo.value,
signals.userInfo.value = {
...signals.userInfo.value,
...newInfo,
};
} else {
//case of setting a new User
//case of deleting userInfo
authentication.userInfo.value = newInfo;
signals.userInfo.value = newInfo;
}
};
export const updateActiveSubscriptions = (
activeSubs: ActiveSubscriptions | undefined,
) => {
authentication.activeSubs.value = activeSubs;
signals.activeSubs.value = activeSubs;
};
// this function is not exposed, only the logout function is