Skip to content

Commit 28f389d

Browse files
committed
settings: add nested routes for settings screens
1 parent bf530d0 commit 28f389d

File tree

12 files changed

+51
-44
lines changed

12 files changed

+51
-44
lines changed

app/src/__stories__/SettingsPage.stories.tsx

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,18 +10,20 @@ export default {
1010
};
1111

1212
export const Default = () => {
13+
const { uiStore } = useStore();
14+
uiStore.showSettings('');
1315
return <SettingsPage />;
1416
};
1517

1618
export const BitcoinUnit = () => {
1719
const { uiStore } = useStore();
18-
uiStore.selectedSetting = 'unit';
20+
uiStore.showSettings('unit');
1921
return <SettingsPage />;
2022
};
2123

2224
export const BalanceMode = () => {
2325
const { uiStore } = useStore();
24-
uiStore.selectedSetting = 'balance';
26+
uiStore.showSettings('balance');
2527
return <SettingsPage />;
2628
};
2729

app/src/__stories__/StoryWrapper.tsx

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import React, { CSSProperties, useMemo } from 'react';
2+
import { Router } from 'react-router';
23
import { observer } from 'mobx-react-lite';
34
import { BalanceMode, Unit } from 'util/constants';
45
import { AuthenticationError } from 'util/errors';
@@ -67,11 +68,13 @@ const StoryWrapper: React.FC<{
6768
return (
6869
<StoreProvider store={store}>
6970
<ThemeProvider>
70-
{/* modify the bg styles so it isn't too big in docs mode */}
71-
<Background style={{ minHeight: 'inherit', height: '100%' }}>
72-
{/* render the Story after the store has been initialized */}
73-
{store.initialized ? <div style={style}>{children}</div> : null}
74-
</Background>
71+
<Router history={store.router.history}>
72+
{/* modify the bg styles so it isn't too big in docs mode */}
73+
<Background style={{ minHeight: 'inherit', height: '100%' }}>
74+
{/* render the Story after the store has been initialized */}
75+
{store.initialized ? <div style={style}>{children}</div> : null}
76+
</Background>
77+
</Router>
7578
</ThemeProvider>
7679
</StoreProvider>
7780
);

app/src/__tests__/components/settings/BalanceSettings.spec.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ describe('BalanceSettings', () => {
4141
it('should navigate back to the Settings screen', () => {
4242
const { getByText } = render();
4343
fireEvent.click(getByText('Settings'));
44-
expect(store.uiStore.selectedSetting).toEqual('general');
44+
expect(store.router.location.pathname).toEqual('/settings');
4545
});
4646

4747
it('should update the Balance Mode to receive', () => {

app/src/__tests__/components/settings/SettingsPage.spec.tsx

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ describe('SettingsPage', () => {
99

1010
beforeEach(() => {
1111
store = createStore();
12+
store.uiStore.showSettings('');
1213
});
1314

1415
const render = () => {
@@ -36,12 +37,12 @@ describe('SettingsPage', () => {
3637
it('should navigate to the Bitcoin Unit screen', () => {
3738
const { getByText } = render();
3839
fireEvent.click(getByText('Bitcoin Unit'));
39-
expect(store.uiStore.selectedSetting).toEqual('unit');
40+
expect(store.router.location.pathname).toEqual('/settings/unit');
4041
});
4142

4243
it('should navigate to the Channel Balance Mode screen', () => {
4344
const { getByText } = render();
4445
fireEvent.click(getByText('Channel Balance Mode'));
45-
expect(store.uiStore.selectedSetting).toEqual('balance');
46+
expect(store.router.location.pathname).toEqual('/settings/balance');
4647
});
4748
});

app/src/__tests__/components/settings/UnitSettings.spec.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ describe('UnitSettings', () => {
4242
it('should navigate back to the Settings screen', () => {
4343
const { getByText } = render();
4444
fireEvent.click(getByText('Settings'));
45-
expect(store.uiStore.selectedSetting).toEqual('general');
45+
expect(store.router.location.pathname).toEqual('/settings');
4646
});
4747

4848
it('should update the Bitcoin Unit to sats', () => {

app/src/components/settings/BalanceSettings.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ const BalanceSettings: React.FC = () => {
3838
const { l } = usePrefixedTranslation('cmps.settings.BalanceSettings');
3939
const { uiStore } = useStore();
4040

41-
const handleBack = useCallback(() => uiStore.showSettings('general'), [uiStore]);
41+
const handleBack = useCallback(() => uiStore.showSettings(''), [uiStore]);
4242

4343
const { Wrapper, Content } = Styled;
4444
return (

app/src/components/settings/SettingItem.tsx

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import React from 'react';
2-
import { observer } from 'mobx-react-lite';
32
import { ArrowRight, RadioButton } from 'components/base';
43
import { styled } from 'components/theme';
54

@@ -51,4 +50,4 @@ const SettingItem: React.FC<Props> = ({ name, value, icon, checked, onClick }) =
5150
);
5251
};
5352

54-
export default observer(SettingItem);
53+
export default SettingItem;
Lines changed: 12 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
import React, { ReactNode } from 'react';
1+
import React from 'react';
2+
import { Route, Switch } from 'react-router';
23
import { observer } from 'mobx-react-lite';
3-
import { useStore } from 'store';
44
import { styled } from 'components/theme';
55
import BalanceSettings from './BalanceSettings';
66
import GeneralSettings from './GeneralSettings';
@@ -13,23 +13,17 @@ const Styled = {
1313
};
1414

1515
const SettingsPage: React.FC = () => {
16-
const { uiStore } = useStore();
17-
18-
let cmp: ReactNode;
19-
switch (uiStore.selectedSetting) {
20-
case 'unit':
21-
cmp = <UnitSettings />;
22-
break;
23-
case 'balance':
24-
cmp = <BalanceSettings />;
25-
break;
26-
case 'general':
27-
default:
28-
cmp = <GeneralSettings />;
29-
}
30-
3116
const { Wrapper } = Styled;
32-
return <Wrapper>{cmp}</Wrapper>;
17+
18+
return (
19+
<Wrapper>
20+
<Switch>
21+
<Route path="/settings" exact component={GeneralSettings} />
22+
<Route path="/settings/unit" component={UnitSettings} />
23+
<Route path="/settings/balance" component={BalanceSettings} />
24+
</Switch>
25+
</Wrapper>
26+
);
3327
};
3428

3529
export default observer(SettingsPage);

app/src/components/settings/UnitSettings.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ const UnitSettings: React.FC = () => {
3737
const { l } = usePrefixedTranslation('cmps.settings.UnitSettings');
3838
const { uiStore } = useStore();
3939

40-
const handleBack = useCallback(() => uiStore.showSettings('general'), [uiStore]);
40+
const handleBack = useCallback(() => uiStore.showSettings(''), [uiStore]);
4141

4242
const { Wrapper, Content } = Styled;
4343
return (

app/src/store/store.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ export class Store {
8989
// entering a password or from loading the credentials from storage.
9090
// only do this if the auth page is currently being viewed, otherwise
9191
// stay on the current page (ex: history, settings)
92-
if (this.router.location.pathname === '/') {
92+
if (document.location.pathname === '/') {
9393
this.uiStore.goToLoop();
9494
}
9595
// also fetch all the data we need

0 commit comments

Comments
 (0)