-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
After playing with this and getting some internal feedback, the following changes will be made to improve this wrapper before release: * Make it more react-y by: * Using the term `Properties` instead of `Options` * Using the term `Provider` in the class and file name of a `React.Provider` type * Set the state in the `useEffect` hook * Eliminate useless `authState` variable * Make `useNylas` act as a wrapper of the `nylas-js` package instead of re-defining the functions in the provider * Safeguard the setting of the client in the provider * Readme improvements * Update package.json for publishing the package on npm
- Loading branch information
1 parent
18fbca5
commit a079ac3
Showing
6 changed files
with
129 additions
and
74 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
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 was deleted.
Oops, something went wrong.
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 |
---|---|---|
@@ -1,18 +1,8 @@ | ||
import * as React from 'react'; | ||
import Nylas, { AuthUrlOptions, ExchangeCodeOptions } from '@nylas/nylas-js'; | ||
import Nylas from '@nylas/nylas-js'; | ||
|
||
export interface NylasContextInterface { | ||
client: Nylas; | ||
authState: boolean; | ||
authWithRedirect(opts: AuthUrlOptions): Promise<void | boolean>; | ||
exchangeCodeFromUrlForToken( | ||
opts?: ExchangeCodeOptions | ||
): Promise<string | boolean>; | ||
} | ||
const NylasContext = React.createContext<Nylas | null>(null); | ||
|
||
const NylasContext = React.createContext<NylasContextInterface | null>(null); | ||
|
||
export const useNylas = (): NylasContextInterface => | ||
React.useContext(NylasContext) as NylasContextInterface; | ||
export const useNylas = (): Nylas => React.useContext(NylasContext) as Nylas; | ||
|
||
export default NylasContext; |
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,38 @@ | ||
import React, {useEffect, useState} from "react"; | ||
import NylasContext from "./nylas-context"; | ||
import Nylas from "@nylas/nylas-js"; | ||
|
||
export interface NylasProviderProperties { | ||
serverBaseUrl: string; | ||
children?: React.ReactNode; | ||
} | ||
|
||
const NylasProvider = (props: NylasProviderProperties): JSX.Element => { | ||
const {children, ...nylasProps} = props; | ||
const [client, setClient] = useState(new Nylas(nylasProps)); | ||
|
||
const safeSetState = (state: Nylas) => { | ||
if(client) { | ||
console.warn("Client already exists."); | ||
return; | ||
} | ||
|
||
setClient(state); | ||
}; | ||
|
||
useEffect(() => { | ||
if(!nylasProps || !nylasProps.serverBaseUrl) { | ||
return; | ||
} | ||
|
||
safeSetState(new Nylas(nylasProps)); | ||
}, [nylasProps]) | ||
|
||
return ( | ||
<NylasContext.Provider value={client}> | ||
{children} | ||
</NylasContext.Provider> | ||
) | ||
} | ||
|
||
export default NylasProvider; |