Skip to content

Commit

Permalink
fix: sync up component context with story change
Browse files Browse the repository at this point in the history
  • Loading branch information
atanasster committed May 2, 2020
1 parent a38cf2f commit 42944f2
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 2 deletions.
6 changes: 6 additions & 0 deletions ui/blocks/src/context/block/BlockDataContext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,11 @@ export interface BlockDataContextProps {
* add an observer for onChange events
*/
removeObserver: (observer: StoreObserver) => void;

/**
* current story id
*/
storyId: string;
}
//@ts-ignore
export const BlockDataContext = React.createContext<BlockDataContextProps>({});
Expand Down Expand Up @@ -107,6 +112,7 @@ export const BlockDataContextProvider: React.FC<BlockDataContextInoutProps> = ({
return (
<BlockDataContext.Provider
value={{
storyId,
getStoryData,
storyIdFromName,
getComponents,
Expand Down
9 changes: 7 additions & 2 deletions ui/blocks/src/context/components/ComponentsContext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,19 +28,24 @@ export const useComponentsContext = ({
of = CURRENT_STORY,
}: ComponentInputProps): ComponentContextProps => {
const {
storyId,
getStoryData,
getComponents,
addObserver,
removeObserver,
} = React.useContext(BlockDataContext);
const [{ story, kind, component }, setStoryData] = useState(getStoryData());
const [{ story, kind, component }, setStoryData] = useState(
getStoryData(storyId),
);

if (!story) {
return {
components: {},
};
}

useEffect(() => {
setStoryData(getStoryData(storyId));
}, [storyId]);
useEffect(() => {
const onChange = () => {
//force refresh of context
Expand Down

0 comments on commit 42944f2

Please sign in to comment.