-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add useGoConfig hook for client configuration
Since we cannot use async in client components we have tor introduce a hook for retrieving goConfig client side. A test is added for proving how it functions.
- Loading branch information
Showing
6 changed files
with
71 additions
and
2 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
import { useEffect, useState } from "react" | ||
|
||
import goConfig from "./goConfig" | ||
import { TConfigKey, resolvers } from "./resolvers" | ||
|
||
const createPromisesFromConfig = (keys: TConfigKey[]) => { | ||
// Get configuration resolvers. | ||
// Turn all configuration resolvers into promises. | ||
// So we can run them all together via Promise.all | ||
return keys.map(key => { | ||
return resolvers[key] instanceof Promise | ||
? goConfig(key as TConfigKey) | ||
: Promise.resolve(goConfig(key as TConfigKey)) | ||
}) | ||
} | ||
|
||
// This functionality (useGoConfig) is used to retrieve configuration values from the client side. | ||
// Only non sensitive configuration values should retrieved from the client side. | ||
|
||
const useGoConfig = <K extends TConfigKey>(keys: K[]) => { | ||
type GoConfigReturnType<K extends TConfigKey> = { | ||
[key in K]: ReturnType<typeof goConfig> | ||
} | ||
const [values, setValues] = useState<GoConfigReturnType<K> | null>(null) | ||
|
||
useEffect(() => { | ||
if (values) return | ||
const promises = createPromisesFromConfig(keys) | ||
|
||
async function retrieveData() { | ||
// Resolve all client configuration resolver promises | ||
// And create an object with the configuration values keyed by the requested keys. | ||
const confData = await Promise.all(promises) | ||
return confData.reduce( | ||
(acc: GoConfigReturnType<K>, value: ReturnType<typeof goConfig>, index: number) => { | ||
return { ...acc, [keys[index]]: value } | ||
}, | ||
{} as GoConfigReturnType<K> | ||
) | ||
} | ||
|
||
retrieveData().then(data => { | ||
if (!data) return | ||
setValues(data) | ||
}) | ||
}, [keys, values]) | ||
|
||
return values | ||
} | ||
|
||
export default useGoConfig |
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