From 20e5cf58b3449f577a3840b2ac7cf16172b4cb33 Mon Sep 17 00:00:00 2001 From: Josh Date: Tue, 26 Jul 2022 11:34:48 -0400 Subject: [PATCH] refactored withRequiredAuthInfo --- package-lock.json | 8 ++++---- src/withRequiredAuthInfo.tsx | 38 ++++++++++-------------------------- 2 files changed, 14 insertions(+), 32 deletions(-) diff --git a/package-lock.json b/package-lock.json index db2ea589..6efa13f2 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "@propelauth/react", - "version": "1.2.7", + "version": "1.2.9", "lockfileVersion": 2, "requires": true, "packages": { "": { "name": "@propelauth/react", - "version": "1.2.7", + "version": "1.2.9", "license": "MIT", "dependencies": { "@propelauth/javascript": "^1.2.3", @@ -41,8 +41,8 @@ "uuid": "^8.3.2" }, "peerDependencies": { - "react": "^16.8.0 || ^17.0.0", - "react-dom": "^16.8.0 || ^17.0.0" + "react": "^16.8.0 || ^17.0.0 || ^18.0.0", + "react-dom": "^16.8.0 || ^17.0.0 || ^18.0.0" } }, "node_modules/@babel/cli": { diff --git a/src/withRequiredAuthInfo.tsx b/src/withRequiredAuthInfo.tsx index 7e4d65d3..b005aba7 100644 --- a/src/withRequiredAuthInfo.tsx +++ b/src/withRequiredAuthInfo.tsx @@ -2,9 +2,9 @@ import hoistNonReactStatics from "hoist-non-react-statics" import React, { useContext } from "react" import { Subtract } from "utility-types" import { AuthContext } from "./AuthContext" -import { getOrgHelper } from "./OrgHelper" import { RedirectToLogin } from "./useRedirectFunctions" import { WithLoggedInAuthInfoProps } from "./withAuthInfo" +import { withAuthInfo } from "./withAuthInfo" export interface WithRequiredAuthInfoArgs { displayWhileLoading?: React.ReactElement @@ -15,22 +15,12 @@ export function withRequiredAuthInfo

( Component: React.ComponentType

, args?: WithRequiredAuthInfoArgs ): React.ComponentType> { - const displayName = `withRequiredAuthInfo(${Component.displayName || Component.name || "Component"})` - - const WithRequiredAuthInfoWrapper = (props: Subtract) => { + const WithRequiredAuthInfoWrapper = (props: Subtract) => { const context = useContext(AuthContext) if (context === undefined) { throw new Error("withRequiredAuthInfo must be used within an AuthProvider or RequiredAuthProvider") } - function displayLoading() { - if (args && args.displayWhileLoading) { - return args.displayWhileLoading - } else { - return - } - } - function displayLoggedOut() { if (args && args.displayIfLoggedOut) { return args.displayIfLoggedOut @@ -39,25 +29,17 @@ export function withRequiredAuthInfo

( } } - const { loading, authInfo, selectOrgId, userSelectedOrgId } = context - if (loading) { - return displayLoading() - } else if (authInfo) { - const orgHelper = getOrgHelper(authInfo.orgHelper, selectOrgId, userSelectedOrgId) - const loggedInProps: P = { - ...(props as P), - accessToken: authInfo.accessToken, - isLoggedIn: !!authInfo.accessToken, - orgHelper: orgHelper, - user: authInfo.user, - } - return + const { authInfo } = context + if (!authInfo) { + return displayLoggedOut() } else { - return displayLoggedOut() + const ComponentWithAuthInfo = withAuthInfo(Component, args) + return } + } - WithRequiredAuthInfoWrapper.displayName = displayName - WithRequiredAuthInfoWrapper.WrappedComponent = Component + WithRequiredAuthInfoWrapper.displayName = `withRequiredAuthInfo(${Component.displayName || Component.name || "Component"})` + WithRequiredAuthInfoWrapper.WrappedComponent = Component return hoistNonReactStatics(WithRequiredAuthInfoWrapper, Component) }