Skip to content

Instantly share code, notes, and snippets.

@javierguzman
Created September 30, 2021 05:01
Show Gist options
  • Save javierguzman/64e590d32e32a38324cc5cbb6239b6e4 to your computer and use it in GitHub Desktop.
Save javierguzman/64e590d32e32a38324cc5cbb6239b6e4 to your computer and use it in GitHub Desktop.
Storybook Gist with addon-links
export const withMemoryRouter = (StoryComponent, { parameters }) => {
const { currentURL } = parameters;
if (!currentURL) {
return <StoryComponent />;
}
const { path, route, redirectComponent, redirectStory } = currentURL;
if (!redirectComponent && !redirectStory) {
return (
<MemoryRouter initialEntries={[encodeURI(route)]}>
<Route path={path}>
<StoryComponent />
</Route>
</MemoryRouter>
);
}
return (
<MemoryRouter initialEntries={[encodeURI(route)]}>
<Route path={path}>
<LinkTo kind={redirectComponent} story={redirectStory} />
</Route>
</MemoryRouter>
);
};
import React from 'react';
import Landing from '../Landing';
import notes from './readme.md';
export default {
title: 'Common/Landing',
component: Landing,
parameters: {
notes: { notes }
}
}
export const defaultStory = (args) => <Landing {...args} />;
import React from 'react';
import Login from './Login';
import notes from './readme.md';
import { withMemoryRouter } from '@StoryDecorator';
export default {
title: 'Auth/Login',
component: Login,
parameters: {
notes: { notes }
}
}
const Template = (args) => <Login {...args} />;
export const defaultStory = Template.bind({});
export const successLogin = Template.bind({});
successLogin.args = {
store: {
initialState: {
auth: {
correctLogin: true,
user: {
id: '12345',
roles: [],
sightVotes: []
},
errorMessage: ''
}
}
}
};
successLogin.decorators = [ withMemoryRouter ];
successLogin.parameters = {
currentURL: {
route: '/',
path: '/',
redirectComponent: 'Common/Landing',
redirectStory: 'defaultStory'
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment