Skip to content

Commit

Permalink
feat: onsave storyblok event
Browse files Browse the repository at this point in the history
  • Loading branch information
BJvdA committed May 7, 2021
1 parent 410a20c commit e51ada7
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 1 deletion.
5 changes: 4 additions & 1 deletion src/bridge/context.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ interface ContextProps {

interface ProviderProps {
children: ReactNode;
/** Fired on Storyblok published & changed event */
onSave?(event: StoryblokEventPayload): void;
/** Storyblok API token (only necessary if resolveRelations is set) */
token?: string;
/**
Expand All @@ -33,6 +35,7 @@ const StoryProvider = ({
children,
token,
resolveRelations,
onSave,
}: ProviderProps) => {
const [, setStoryState] = useState(undefined);
const storyRef = useRef<Story | undefined>(undefined);
Expand All @@ -52,7 +55,7 @@ const StoryProvider = ({

useEffect(() => {
if (window?.location?.search?.includes('_storyblok=')) {
init(storyRef.current, onStoryInput, token, resolveRelations);
init(storyRef.current, onStoryInput, token, resolveRelations, onSave);
}
}, []);

Expand Down
5 changes: 5 additions & 0 deletions src/bridge/init.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ export const init = (
onStoryInput: (story: Story) => void,
token: string,
resolveRelations: string[] = [],
onSave?: (event: StoryblokEventPayload) => void,
) => {
loadBridge(() => {
if (window.StoryblokBridge) {
Expand All @@ -30,6 +31,10 @@ export const init = (
onStoryInput(event.story);
}
});

window.storyblok.on(['published', 'change'], (event) => {
onSave(event);
});
}
});
};

0 comments on commit e51ada7

Please sign in to comment.