-
Notifications
You must be signed in to change notification settings - Fork 111
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #113 from Hexastack/112-issue-decouple-frontend-co…
…ntext-logic-from-hooks-logic refactor(frontend): decouple contexts logic from hooks logic
- Loading branch information
Showing
11 changed files
with
213 additions
and
142 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
/* | ||
* Copyright © 2024 Hexastack. All rights reserved. | ||
* | ||
* Licensed under the GNU Affero General Public License v3.0 (AGPLv3) with the following additional terms: | ||
* 1. The name "Hexabot" is a trademark of Hexastack. You may not use this name in derivative works without express written permission. | ||
* 2. All derivative works must include clear attribution to the original creator and software, Hexastack and Hexabot, in a prominent location (e.g., in the software's "About" section, documentation, and README file). | ||
*/ | ||
|
||
import axios from "axios"; | ||
import { createContext, ReactNode, FC } from "react"; | ||
|
||
import { getApiClientByEntity, useAxiosInstance } from "@/hooks/useApiClient"; | ||
import { ApiClient, EntityApiClient } from "@/services/api.class"; | ||
import { EntityType } from "@/services/types"; | ||
import { IBaseSchema } from "@/types/base.types"; | ||
|
||
interface ApiClientContextProps { | ||
children: ReactNode; | ||
} | ||
|
||
export interface ApiClientContext { | ||
apiClient: ApiClient; | ||
getApiClientByEntity: <TAttr, TStub extends IBaseSchema, TFull = never>( | ||
type: EntityType, | ||
) => EntityApiClient<TAttr, TStub, TFull>; | ||
} | ||
|
||
export const ApiClientContext = createContext<ApiClientContext>({ | ||
apiClient: new ApiClient(axios.create()), | ||
getApiClientByEntity: () => { | ||
throw new Error( | ||
"getApiClientByEntity must be used within an ApiClientProvider", | ||
); | ||
}, | ||
}); | ||
|
||
export const ApiClientProvider: FC<ApiClientContextProps> = ({ children }) => { | ||
const axiosInstance = useAxiosInstance(); | ||
const apiClient = new ApiClient(axiosInstance); | ||
|
||
return ( | ||
<ApiClientContext.Provider | ||
value={{ | ||
apiClient, | ||
getApiClientByEntity: (type) => getApiClientByEntity(type, apiClient), | ||
}} | ||
> | ||
{children} | ||
</ApiClientContext.Provider> | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
/* | ||
* Copyright © 2024 Hexastack. All rights reserved. | ||
* | ||
* Licensed under the GNU Affero General Public License v3.0 (AGPLv3) with the following additional terms: | ||
* 1. The name "Hexabot" is a trademark of Hexastack. You may not use this name in derivative works without express written permission. | ||
* 2. All derivative works must include clear attribution to the original creator and software, Hexastack and Hexabot, in a prominent location (e.g., in the software's "About" section, documentation, and README file). | ||
*/ | ||
|
||
import { useRouter } from "next/router"; | ||
import { useContext } from "react"; | ||
|
||
import { AuthContext } from "@/contexts/auth.context"; | ||
import { RouterType } from "@/services/types"; | ||
|
||
export const CURRENT_USER_KEY = "current-user"; | ||
export const PUBLIC_PATHS = [ | ||
"/login/[[...token]]", | ||
"/register/[token]", | ||
"/reset/[token]", | ||
"/reset", | ||
]; | ||
|
||
export const useAuth = () => { | ||
const context = useContext(AuthContext); | ||
|
||
if (!context) { | ||
throw new Error(`useAuth must be used within an AuthProvider`); | ||
} | ||
|
||
return context; | ||
}; | ||
|
||
export const useLogoutRedirection = () => { | ||
const router = useRouter(); | ||
const hasPublicPath = PUBLIC_PATHS.includes(router.pathname); | ||
const logoutRedirection = async (fullReload: boolean = false) => { | ||
if (!hasPublicPath) { | ||
const redirectUrl = `/${RouterType.LOGIN}?redirect=${encodeURIComponent( | ||
router.pathname, | ||
)}`; | ||
|
||
if (fullReload) { | ||
window.location.replace(redirectUrl); | ||
} else { | ||
await router.replace(redirectUrl); | ||
} | ||
} | ||
}; | ||
|
||
return { logoutRedirection }; | ||
}; |
Oops, something went wrong.