Skip to content

Commit

Permalink
feat(project): add uistate provider
Browse files Browse the repository at this point in the history
  • Loading branch information
royschut committed May 6, 2021
1 parent 2ccf363 commit 59def36
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 6 deletions.
13 changes: 7 additions & 6 deletions src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { BrowserRouter as Router } from 'react-router-dom';
import Root from './components/Root/Root';
import ConfigProvider from './providers/configProvider';
import QueryProvider from './providers/QueryProvider';
import UIStateProvider from './providers/uiStateProvider';
import './styles/main.scss';

interface State {
Expand All @@ -24,14 +25,14 @@ class App extends Component {
<QueryProvider>
<ConfigProvider
configLocation={window.configLocation}
onLoading={(isLoading: boolean) =>
console.info(`Loading config: ${isLoading}`)
}
onLoading={(isLoading: boolean) => console.info(`Loading config: ${isLoading}`)}
onValidationError={(error: Error) => console.error(`Config ${error}`)}
>
<Router>
<Root error={this.state.error} />
</Router>
<UIStateProvider>
<Router>
<Root error={this.state.error} />
</Router>
</UIStateProvider>
</ConfigProvider>
</QueryProvider>
);
Expand Down
25 changes: 25 additions & 0 deletions src/providers/uiStateProvider.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import React, { createContext, FunctionComponent, ReactNode, useState } from 'react';

export type UpdateBlurImage = (image: string) => void;
export type BlurImage = string;
export type UIContext = { blurImage: BlurImage; updateBlurImage: UpdateBlurImage };

const defaultContext: UIContext = {
blurImage: '',
updateBlurImage: () => '',
};

export const UIStateContext = createContext<UIContext>(defaultContext);

export type ProviderProps = {
children: ReactNode;
};

const UIStateProvider: FunctionComponent<ProviderProps> = ({ children }) => {
const [blurImage, setBlurImage] = useState<BlurImage>(() => defaultContext.blurImage);
const updateBlurImage: UpdateBlurImage = (image: BlurImage) => setBlurImage(image);

return <UIStateContext.Provider value={{ blurImage, updateBlurImage }}>{children}</UIStateContext.Provider>;
};

export default UIStateProvider;

0 comments on commit 59def36

Please sign in to comment.