From 0d2442aa712b51dcb117b6b33acae29834ddf2bc Mon Sep 17 00:00:00 2001 From: wil Date: Thu, 29 Aug 2024 18:15:17 -0400 Subject: [PATCH] better file structure --- src/components/features/ProfileDropdown.tsx | 7 ++- src/components/hoc/withProtectedRoute.tsx | 10 ++- src/pages/login.tsx | 63 +++++++++---------- src/pages/main.tsx | 11 ++-- .../authentication/enums.ts} | 0 src/signals/authentication/fetchers.ts | 6 +- src/signals/authentication/index.ts | 11 +++- .../{authentication.ts => signals.ts} | 4 +- src/signals/authentication/utilities.ts | 17 +++-- 9 files changed, 69 insertions(+), 60 deletions(-) rename src/{enums/Authentication.ts => signals/authentication/enums.ts} (100%) rename src/signals/authentication/{authentication.ts => signals.ts} (93%) diff --git a/src/components/features/ProfileDropdown.tsx b/src/components/features/ProfileDropdown.tsx index 7a092f1fdc..27c24d57ab 100644 --- a/src/components/features/ProfileDropdown.tsx +++ b/src/components/features/ProfileDropdown.tsx @@ -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; diff --git a/src/components/hoc/withProtectedRoute.tsx b/src/components/hoc/withProtectedRoute.tsx index 9b8abd972d..f935b5a3d4 100644 --- a/src/components/hoc/withProtectedRoute.tsx +++ b/src/components/hoc/withProtectedRoute.tsx @@ -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 =

( Component: ComponentType

, ) => 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 || diff --git a/src/pages/login.tsx b/src/pages/login.tsx index 379e667c45..59979f4456 100644 --- a/src/pages/login.tsx +++ b/src/pages/login.tsx @@ -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(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) => { ev.preventDefault(); @@ -51,7 +68,7 @@ export const Login = () => {

Storyteller Logo
-
+
{
); }; - -// 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..."; -}; diff --git a/src/pages/main.tsx b/src/pages/main.tsx index a06354d83f..de2d532fc8 100644 --- a/src/pages/main.tsx +++ b/src/pages/main.tsx @@ -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(() => {

-

- you are logged in as {authentication.userInfo.value?.display_name} -

+

you are logged in as {userInfo.value?.display_name}

diff --git a/src/enums/Authentication.ts b/src/signals/authentication/enums.ts similarity index 100% rename from src/enums/Authentication.ts rename to src/signals/authentication/enums.ts diff --git a/src/signals/authentication/fetchers.ts b/src/signals/authentication/fetchers.ts index a05e92f1aa..b89a6ac9fb 100644 --- a/src/signals/authentication/fetchers.ts +++ b/src/signals/authentication/fetchers.ts @@ -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(); diff --git a/src/signals/authentication/index.ts b/src/signals/authentication/index.ts index bf6a51e48f..c4211a6134 100644 --- a/src/signals/authentication/index.ts +++ b/src/signals/authentication/index.ts @@ -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, +}; diff --git a/src/signals/authentication/authentication.ts b/src/signals/authentication/signals.ts similarity index 93% rename from src/signals/authentication/authentication.ts rename to src/signals/authentication/signals.ts index 8cb9a9d3e8..2c095bbd82 100644 --- a/src/signals/authentication/authentication.ts +++ b/src/signals/authentication/signals.ts @@ -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.INIT); const userInfo = signal(undefined); @@ -43,7 +43,7 @@ const hasPremium = computed(() => { return false; }); -export const authentication = { +export const signals = { status, userInfo, activeSubs, diff --git a/src/signals/authentication/utilities.ts b/src/signals/authentication/utilities.ts index 3c0e21c3fa..dc9bd3ce0e 100644 --- a/src/signals/authentication/utilities.ts +++ b/src/signals/authentication/utilities.ts @@ -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