Skip to content

Commit

Permalink
Fix addon api
Browse files Browse the repository at this point in the history
  • Loading branch information
jsomsanith-tlnd committed Jun 17, 2019
1 parent a4fb0de commit ff04534
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 26 deletions.
26 changes: 18 additions & 8 deletions lib/api/src/modules/addons.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,16 @@ interface Panels {

type StateMerger<S> = (input: S) => S;

interface StoryInput {
parameters: {
[parameterName: string]: any;
};
}

export interface SubAPI {
getElements: (type: Types) => Collection;
getPanels: () => Collection;
getStoryPanels: (storyId: string) => Collection;
getStoryPanels: () => Collection;
getSelectedPanel: () => string;
setSelectedPanel: (panelName: string) => void;
setAddonState<S>(
Expand Down Expand Up @@ -74,17 +80,21 @@ export default ({ provider, store }: Module) => {
const api: SubAPI = {
getElements: type => provider.getElements(type),
getPanels: () => api.getElements(types.PANEL),
getStoryPanels: storyId => {
const storyParameters = provider.getParameters(storyId);
const panels = api.getPanels();
if (!panels || !storyParameters) {
return panels;
getStoryPanels: () => {
const allPanels = api.getPanels();
const { storyId, storiesHash } = store.getState();
const storyInput = storyId && (storiesHash[storyId] as StoryInput);

if (!allPanels || !storyInput) {
return allPanels;
}

const { parameters } = storyInput;

const filteredPanels: Collection = {};
Object.entries(panels).forEach(([id, panel]) => {
Object.entries(allPanels).forEach(([id, panel]) => {
const { paramKey } = panel;
if (paramKey && storyParameters[paramKey] && storyParameters[paramKey].disabled) {
if (paramKey && parameters[paramKey] && parameters[paramKey].disabled) {
return;
}
filteredPanels[id] = panel;
Expand Down
33 changes: 16 additions & 17 deletions lib/api/src/tests/addons.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,11 +61,7 @@ describe('Addons API', () => {
describe('#getStoryPanels', () => {
it('should return all panels by default', () => {
// given
const providerWithStoryParameters = {
...provider,
getParameters: () => {},
};
const { api } = initAddons({ provider: providerWithStoryParameters, store });
const { api } = initAddons({ provider, store });

// when
const filteredPanels = api.getStoryPanels();
Expand All @@ -77,21 +73,24 @@ describe('Addons API', () => {
it('should filter disabled addons', () => {
// given
const storyId = 'story 1';
const providerWithStoryParameters = {
...provider,
getParameters: id => {
if (id === storyId) {
return {
a11y: { disabled: true },
};
}
return null;
},
const storeWithStory = {
getState: () => ({
storyId,
storiesHash: {
[storyId]: {
parameters: {
a11y: { disabled: true },
},
},
},
}),
setState: jest.fn(),
};
const { api } = initAddons({ provider: providerWithStoryParameters, store });

const { api } = initAddons({ provider, store: storeWithStory });

// when
const filteredPanels = api.getStoryPanels(storyId);
const filteredPanels = api.getStoryPanels();

// then
expect(filteredPanels).toEqual({
Expand Down
2 changes: 1 addition & 1 deletion lib/ui/src/containers/panel.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ const createPanelActions = memoize(1)(api => ({
}));

const mapper = ({ state, api }) => ({
panels: api.getStoryPanels(state.storyId),
panels: api.getStoryPanels(),
selectedPanel: api.getSelectedPanel(),
panelPosition: state.layout.panelPosition,
actions: createPanelActions(api),
Expand Down

0 comments on commit ff04534

Please sign in to comment.