Skip to content

Commit

Permalink
feat(app): adapt story reducer to handle new story type
Browse files Browse the repository at this point in the history
  • Loading branch information
Immo-Be committed Feb 14, 2024
1 parent b55a948 commit 8831c1a
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 14 deletions.
23 changes: 11 additions & 12 deletions src/scripts/actions/fetch-story.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {languageSelector} from '../selectors/language';
import {State} from '../reducers/index';

import {LegacyStory} from '../types/legacy-story';
import {Story} from '../types/story';

export const FETCH_STORY_SUCCESS = 'FETCH_STORY_SUCCESS';
export const FETCH_STORY_ERROR = 'FETCH_STORY_ERROR';
Expand All @@ -13,7 +14,7 @@ interface FetchStorySuccessAction {
type: typeof FETCH_STORY_SUCCESS;
id: string;
language: string;
story: LegacyStory;
story: LegacyStory | Story;
}

interface FetchStoryErrorAction {
Expand Down Expand Up @@ -50,17 +51,15 @@ function fetchStoryErrorAction(
};
}

const fetchStory = (id: string) => (
dispatch: Dispatch,
getState: () => State
) => {
const language = languageSelector(getState());
const fetchStory =
(id: string) => (dispatch: Dispatch, getState: () => State) => {
const language = languageSelector(getState());

return fetchStoryApi(id, language)
.then(story => dispatch(fetchStorySuccessAction(id, language, story)))
.catch(error =>
dispatch(fetchStoryErrorAction(id, language, error.message))
);
};
return fetchStoryApi(id, language)
.then(story => dispatch(fetchStorySuccessAction(id, language, story)))
.catch(error =>
dispatch(fetchStoryErrorAction(id, language, error.message))
);
};

export default fetchStory;
9 changes: 7 additions & 2 deletions src/scripts/reducers/story/selected.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
} from '../../actions/fetch-story';

import {convertLegacyStory} from '../../libs/convert-legacy-story';
import {LegacyStory} from '../../types/legacy-story';

import {Story} from '../../types/story';

Expand All @@ -12,8 +13,12 @@ function selectedStoryReducer(
action: FetchStoryActions
): Story | null {
switch (action.type) {
case FETCH_STORY_SUCCESS:
return convertLegacyStory(action.story);
case FETCH_STORY_SUCCESS: {
const isLegacyStory = !action.story.isCurrentStory;
return isLegacyStory
? convertLegacyStory(action.story as LegacyStory)
: (action.story as Story);
}
default:
return storyState;
}
Expand Down
1 change: 1 addition & 0 deletions src/scripts/types/legacy-story.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,5 @@ export interface LegacySlide {
export interface LegacyStory {
id: string;
slides: LegacySlide[];
isCurrentStory?: boolean;
}
1 change: 1 addition & 0 deletions src/scripts/types/story.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ export interface Slide {
}

export interface Story {
isCurrentStory?: boolean;
id: string;
slides: Slide[];
}

0 comments on commit 8831c1a

Please sign in to comment.