|
1 | 1 | import React from 'react'; |
2 | 2 | import { storiesOf } from '@storybook/react'; |
3 | 3 | import { action } from '@storybook/addon-actions'; |
4 | | -import StoryDecorator from '../../_support/StoryDecorator'; |
5 | | -import InitialSettings from '../../../../source/renderer/app/components/profile/initial-settings/InitialSettings'; |
| 4 | +import { withState, Store } from '@dump247/storybook-state'; |
6 | 5 | import { |
7 | 6 | DATE_ENGLISH_OPTIONS, |
| 7 | + LANGUAGE_OPTIONS, |
8 | 8 | NUMBER_OPTIONS, |
9 | 9 | TIME_OPTIONS, |
10 | 10 | } from '../../../../source/renderer/app/config/profileConfig'; |
| 11 | +import StoryDecorator from '../../_support/StoryDecorator'; |
| 12 | +import InitialSettings from '../../../../source/renderer/app/components/profile/initial-settings/InitialSettings'; |
| 13 | + |
| 14 | +interface StoryStore { |
| 15 | + currentDateFormat: string; |
| 16 | + currentNumberFormat: string; |
| 17 | + currentTimeFormat: string; |
| 18 | + currentLocale: string; |
| 19 | +} |
| 20 | + |
| 21 | +const mockedLanguageState = { |
| 22 | + currentDateFormat: DATE_ENGLISH_OPTIONS[0].value, |
| 23 | + currentNumberFormat: NUMBER_OPTIONS[0].value, |
| 24 | + currentTimeFormat: TIME_OPTIONS[0].value, |
| 25 | + currentLocale: LANGUAGE_OPTIONS[0].value, |
| 26 | +}; |
| 27 | + |
| 28 | +const onValueChange = ( |
| 29 | + store: Store<StoryStore>, |
| 30 | + id: string, |
| 31 | + value: string |
| 32 | +): void => { |
| 33 | + const fieldIdToStoreKeyMap = { |
| 34 | + dateFormat: 'currentDateFormat', |
| 35 | + numberFormat: 'currentNumberFormat', |
| 36 | + timeFormat: 'currentTimeFormat', |
| 37 | + locale: 'currentLocale', |
| 38 | + }; |
| 39 | + |
| 40 | + store.set({ |
| 41 | + [fieldIdToStoreKeyMap[id]]: value, |
| 42 | + }); |
| 43 | + |
| 44 | + if (id === 'locale') { |
| 45 | + const currentDateFormat = |
| 46 | + value === mockedLanguageState.currentLocale |
| 47 | + ? mockedLanguageState.currentDateFormat |
| 48 | + : 'YYYY年MM月DD日'; |
| 49 | + store.set({ |
| 50 | + currentDateFormat, |
| 51 | + }); |
| 52 | + } |
| 53 | +}; |
11 | 54 |
|
12 | 55 | storiesOf('Settings / Language', module) |
13 | 56 | .addDecorator((story) => <StoryDecorator>{story()}</StoryDecorator>) // ====== Stories ====== |
14 | | - // @ts-ignore ts-migrate(2345) FIXME: Argument of type '({ locale }: { locale: string; }... Remove this comment to see the full error message |
15 | | - .add('Select Language - initial', (_, { locale }: { locale: string }) => ( |
16 | | - <div> |
17 | | - <InitialSettings |
18 | | - onSubmit={action('submit')} |
19 | | - onChangeItem={action('onChangeItem')} |
20 | | - currentDateFormat={DATE_ENGLISH_OPTIONS[0].value} |
21 | | - currentLocale={locale} |
22 | | - currentNumberFormat={NUMBER_OPTIONS[0].value} |
23 | | - currentTimeFormat={TIME_OPTIONS[0].value} |
24 | | - /> |
25 | | - </div> |
26 | | - )) |
27 | | - // @ts-ignore ts-migrate(2345) FIXME: Argument of type '({ locale }: { locale: string; }... Remove this comment to see the full error message |
28 | | - .add('Select Language - submitting', (_, { locale }: { locale: string }) => ( |
29 | | - <div> |
30 | | - <InitialSettings |
31 | | - onSubmit={action('submit')} |
32 | | - onChangeItem={action('onChangeItem')} |
33 | | - currentDateFormat={DATE_ENGLISH_OPTIONS[0].value} |
34 | | - currentLocale={locale} |
35 | | - currentNumberFormat={NUMBER_OPTIONS[0].value} |
36 | | - currentTimeFormat={TIME_OPTIONS[0].value} |
37 | | - isSubmitting |
38 | | - /> |
39 | | - </div> |
40 | | - )); |
| 57 | + .add( |
| 58 | + 'Select Language - initial', |
| 59 | + withState(mockedLanguageState, (store) => ( |
| 60 | + <div> |
| 61 | + <InitialSettings |
| 62 | + onSubmit={action('submit')} |
| 63 | + onChangeItem={(id, value) => onValueChange(store, id, value)} |
| 64 | + {...store.state} |
| 65 | + /> |
| 66 | + </div> |
| 67 | + )) |
| 68 | + ) |
| 69 | + .add( |
| 70 | + 'Select Language - submitting', |
| 71 | + withState(mockedLanguageState, (store) => ( |
| 72 | + <div> |
| 73 | + <InitialSettings |
| 74 | + onSubmit={action('submit')} |
| 75 | + onChangeItem={(id, value) => onValueChange(store, id, value)} |
| 76 | + isSubmitting |
| 77 | + {...store.state} |
| 78 | + /> |
| 79 | + </div> |
| 80 | + )) |
| 81 | + ); |
0 commit comments