Skip to content

Commit 3a596ec

Browse files
committed
♻️ refactor <MobilePreferences />, create PreferenceCreators
1 parent f349199 commit 3a596ec

File tree

2 files changed

+34
-61
lines changed

2 files changed

+34
-61
lines changed
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
export const optionsOnOff = (name, onLabel = 'On', offLabel = 'Off') => [
2+
{
3+
value: true, label: onLabel, ariaLabel: `${name} on`, name: `${name}`, id: `${name}-on`.replace(' ', '-')
4+
},
5+
{
6+
value: false, label: offLabel, ariaLabel: `${name} off`, name: `${name}`, id: `${name}-off`.replace(' ', '-')
7+
},
8+
];
9+
10+
export const optionsPickOne = (name, ...options) => options.map(option => ({
11+
value: option,
12+
label: option,
13+
ariaLabel: `${option} ${name} on`,
14+
name: `${option} ${name}`,
15+
id: `${option}-${name}-on`.replace(' ', '-')
16+
}));
17+
18+
const nameToValueName = x => (x && x.toLowerCase().replace(/#|_|-/g, ' '));
19+
20+
// preferenceOnOff: name, value and onSelect are mandatory. propname is optional
21+
export const preferenceOnOff = (name, value, onSelect, propname) => ({
22+
title: name,
23+
value,
24+
options: optionsOnOff(propname || nameToValueName(name)),
25+
onSelect
26+
});

client/modules/Mobile/MobilePreferences.jsx

Lines changed: 8 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import Header from '../../components/mobile/Header';
1414
import PreferencePicker from '../../components/mobile/PreferencePicker';
1515
import { ExitIcon } from '../../common/icons';
1616
import { remSize, prop } from '../../theme';
17+
import { optionsOnOff, optionsPickOne, preferenceOnOff } from '../../common/helpers/PreferenceCreators';
1718

1819
const Content = styled.div`
1920
z-index: 0;
@@ -29,23 +30,6 @@ const SectionSubeader = styled.h3`
2930
color: ${prop('primaryTextColor')};
3031
`;
3132

32-
const optionsOnOff = (name, onLabel = 'On', offLabel = 'Off') => [
33-
{
34-
value: true, label: onLabel, ariaLabel: `${name} on`, name: `${name}`, id: `${name}-on`.replace(' ', '-')
35-
},
36-
{
37-
value: false, label: offLabel, ariaLabel: `${name} off`, name: `${name}`, id: `${name}-off`.replace(' ', '-')
38-
},
39-
];
40-
41-
const optionsPickOne = (name, ...options) => options.map(option => ({
42-
value: option,
43-
label: option,
44-
ariaLabel: `${option} ${name} on`,
45-
name: `${option} ${name}`,
46-
id: `${option}-${name}-on`.replace(' ', '-')
47-
}));
48-
4933

5034
const MobilePreferences = (props) => {
5135
const {
@@ -62,56 +46,19 @@ const MobilePreferences = (props) => {
6246
options: optionsPickOne('theme', 'light', 'dark', 'contrast'),
6347
onSelect: x => setTheme(x) // setTheme
6448
},
65-
66-
{
67-
title: 'Autosave',
68-
value: autosave,
69-
options: optionsOnOff('autosave'),
70-
onSelect: x => setAutosave(x) // setAutosave
71-
},
72-
73-
{
74-
title: 'Word Wrap',
75-
value: linewrap,
76-
options: optionsOnOff('linewrap'),
77-
onSelect: x => setLinewrap(x)
78-
}
49+
preferenceOnOff('Autosave', autosave, setAutosave, 'autosave'),
50+
preferenceOnOff('Word Wrap', linewrap, setLinewrap, 'linewrap')
7951
];
8052

8153
const outputSettings = [
82-
{
83-
title: 'Plain-text',
84-
value: textOutput,
85-
options: optionsOnOff('text output'),
86-
onSelect: x => setTextOutput(x)
87-
},
88-
{
89-
title: 'Table-text',
90-
value: gridOutput,
91-
options: optionsOnOff('table output'),
92-
onSelect: x => setGridOutput(x)
93-
},
94-
{
95-
title: 'Sound',
96-
value: soundOutput,
97-
options: optionsOnOff('sound output'),
98-
onSelect: x => setSoundOutput(x)
99-
},
54+
preferenceOnOff('Plain-text', textOutput, setTextOutput, 'text output'),
55+
preferenceOnOff('Table-text', gridOutput, setGridOutput, 'table output'),
56+
preferenceOnOff('Lint Warning Sound', soundOutput, setSoundOutput, 'sound output')
10057
];
10158

10259
const accessibilitySettings = [
103-
{
104-
title: 'Line Numbers',
105-
value: lineNumbers,
106-
options: optionsOnOff('line numbers'),
107-
onSelect: x => setLineNumbers(x)
108-
},
109-
{
110-
title: 'Lint Warning Sound',
111-
value: lintWarning,
112-
options: optionsOnOff('lint warning'),
113-
onSelect: x => setLintWarning(x)
114-
},
60+
preferenceOnOff('Line Numbers', lineNumbers, setLineNumbers),
61+
preferenceOnOff('Lint Warning Sound', lintWarning, setLintWarning)
11562
];
11663

11764
return (

0 commit comments

Comments
 (0)