-
-
Notifications
You must be signed in to change notification settings - Fork 68
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: created AccountProvider and useEnvironment
- Loading branch information
1 parent
78e5498
commit 5099fa6
Showing
7 changed files
with
109 additions
and
66 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,54 @@ | ||
import React, { ReactNode, createContext, useContext, useEffect, useState } from 'react'; | ||
import * as Sentry from '@sentry/react'; | ||
import { useUser } from '@clerk/clerk-react'; | ||
import { useEnvironment } from '../hooks'; | ||
import { DEFAULT_ENV, REVERT_BASE_API_URL } from '../constants'; | ||
|
||
const AccountContext = createContext<any>({}); | ||
|
||
interface Props { | ||
children?: ReactNode; | ||
} | ||
|
||
export function AccountProvider({ children }: Props) { | ||
const [account, setAccount] = useState<any>(); | ||
const { user, isLoaded, isSignedIn } = useUser(); | ||
const { setEnvironment } = useEnvironment(); | ||
|
||
useEffect(() => { | ||
if (isLoaded && isSignedIn) { | ||
const headers = new Headers(); | ||
headers.append('Content-Type', 'application/json'); | ||
|
||
const data = JSON.stringify({ | ||
userId: user.id, | ||
}); | ||
|
||
const requestOptions = { | ||
method: 'POST', | ||
body: data, | ||
headers: headers, | ||
}; | ||
fetch(`${REVERT_BASE_API_URL}/internal/account`, requestOptions) | ||
.then((response) => response.json()) | ||
.then((result) => { | ||
setAccount(result?.account); | ||
const environments: string[] = result?.account?.environments?.map((env) => env.env) || []; | ||
setEnvironment(environments?.[0] || DEFAULT_ENV); | ||
}) | ||
.catch((error) => { | ||
Sentry.captureException(error); | ||
console.error('error', error); | ||
}); | ||
} | ||
}, [account, isLoaded, isSignedIn, setEnvironment, user?.id]); | ||
|
||
return <AccountContext.Provider value={{ account }}>{children}</AccountContext.Provider>; | ||
} | ||
|
||
export function useAccount() { | ||
const context = useContext(AccountContext); | ||
if (context === undefined) throw new Error('Context was used out of Provider'); | ||
|
||
return context; | ||
} |
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 @@ | ||
export * from './useEnvironment'; |
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,17 @@ | ||
import { useEffect, useState } from 'react'; | ||
import { DEFAULT_ENV } from '../constants'; | ||
|
||
export function useEnvironment() { | ||
const [environment, setEnvironment] = useState<string>(function () { | ||
return localStorage.getItem('revert_environment_selected') || DEFAULT_ENV; | ||
}); | ||
|
||
useEffect( | ||
function () { | ||
localStorage.setItem('revert_environment_selected', environment); | ||
}, | ||
[environment] | ||
); | ||
|
||
return { environment, setEnvironment }; | ||
} |
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