Skip to content

Commit

Permalink
fix: add more documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
hugomrdias committed May 18, 2023
1 parent 34210c3 commit 9e2204e
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 3 deletions.
38 changes: 37 additions & 1 deletion packages/odd-preact/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,46 @@ pnpm install @oddjs/preact
## Usage

```js
// app.jsx
import { OddContextProvider } from '@oddjs/preact'
/** @type {import('@oddjs/odd').Configuration} */
const config = {
namespace: {
creator: document.location.host,
name: 'Passkey auth',
},
}

export function App() {
return (
<>
<OddContextProvider
config={config}
componentsFactory={OddPasskey.createComponents}
>
<main className="App">
<Home path="/" />
</main>
</OddContextProvider>
</>
)
}

// home.jsx
import { useOddContext } from '@oddjs/preact'

// with preact-router
export default function Home(props) {
const { session, isLoading, login, logout, register } = useOddContext()
}

// home.jsx with preact-router
import { useOdd } from '@oddjs/preact/router'

export default function Home(props) {
const { session, isLoading, login, logout, register } = useOdd({
redirectTo: '/login',
})
}
```

## Docs
Expand Down
13 changes: 13 additions & 0 deletions packages/odd-preact/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,19 @@ export function OddContextProvider({
return createElement(OddContext.Provider, { value, children })
}

/**
* Hook to access the ODD program and session from the `OddContextProvider`.
*
* @example
* ```jsx
* // home.jsx
* import { useOddContext } from '@oddjs/preact'
*
export default function Home(props) {
const { session, isLoading, login, logout, register } = useOddContext()
}
* ```
*/
export function useOddContext() {
const context = useContext(OddContext)
if (context === undefined) {
Expand Down
32 changes: 31 additions & 1 deletion packages/odd-preact/src/router.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,37 @@ import { useEffect } from 'preact/hooks'
import { route } from 'preact-router'
import { useOddContext } from './index.js'

export function useOdd({ redirectTo = '', redirectIfFound = false } = {}) {
/**
* Hook to access the ODD program and session from the `OddContextProvider`.
*
* It uses `preact-router` to handle routing, if your are using a different router you can use the `useOddContext` hook directly.
*
* It routes to the `redirectTo` path if the user is not logged in or when `redirectIfFound` is set to `true` routes to `redirectTo` if user is logged in.
*
* @example
* ```jsx
* // login.jsx
* import { useOdd } from '@oddjs/preact/router'
*
* const { isLoading, login } = useOdd({
redirectTo: '/',
redirectIfFound: true,
})
* ```
*
* @example
* ```jsx
* // home.jsx
* import { useOdd } from '@oddjs/preact/router'
*
* const { isLoading, logout } = useOdd({
redirectTo: '/login',
})
* ```
*
* @param {{redirectTo?: string, redirectIfFound?: boolean}} options
*/
export function useOdd({ redirectTo = '/', redirectIfFound = false }) {
const context = useOddContext()
if (context === undefined) {
throw new Error(`useOddContext must be used within a OddContextProvider.`)
Expand Down
2 changes: 1 addition & 1 deletion packages/odd-preact/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
"include": ["src", "scripts", "test", "package.json"],
"exclude": ["node_modules", "dist", "out"],
"typedocOptions": {
"entryPoints": ["src/index.jsx", "src/router.js"],
"entryPoints": ["src/index.js", "src/router.js"],
"includeVersion": true,
"excludeExternals": true,
"internalModule": "<internal>"
Expand Down

0 comments on commit 9e2204e

Please sign in to comment.