Skip to content

Commit

Permalink
fix: allow passing token for resolverelations
Browse files Browse the repository at this point in the history
  • Loading branch information
BJvdA committed May 3, 2021
1 parent 1395890 commit c3559ed
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 3 deletions.
10 changes: 8 additions & 2 deletions 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;
/** Storyblok API token (only necessary if resolveRelations is set) */
token?: string;
/**
* Relations that need to be resolved in preview mode, for example:
* `['Post.author']`
Expand All @@ -27,7 +29,11 @@ interface ProviderProps {

const StoryContext = createContext<ContextProps | undefined>(undefined);

const StoryProvider = ({ children, resolveRelations }: ProviderProps) => {
const StoryProvider = ({
children,
token,
resolveRelations,
}: ProviderProps) => {
const [, setStoryState] = useState(undefined);
const storyRef = useRef<Story | undefined>(undefined);

Expand All @@ -46,7 +52,7 @@ const StoryProvider = ({ children, resolveRelations }: ProviderProps) => {

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

Expand Down
3 changes: 2 additions & 1 deletion src/bridge/init.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,12 @@ const loadBridge = (callback: () => void) => {
export const init = (
story: Story,
onStoryInput: (story: Story) => void,
token: string,
resolveRelations: string[] = [],
) => {
loadBridge(() => {
if (window.storyblok) {
window.storyblok.init();
window.storyblok.init({ accessToken: token });

// Update story on input in Visual Editor
// this will alter the state and replaces the current story with a
Expand Down

0 comments on commit c3559ed

Please sign in to comment.