Skip to content

Commit

Permalink
Merge pull request #12 from acdvs/url-param-fixes
Browse files Browse the repository at this point in the history
Toggle settings with initial checkbox
  • Loading branch information
acdvs committed Mar 23, 2022
2 parents 90f04e4 + 45d50b9 commit 1cf5978
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 14 deletions.
27 changes: 14 additions & 13 deletions src/components/Home/Settings.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,30 +2,31 @@ import { useReducer } from 'react';
import { Form } from 'react-bootstrap';

import settingsReducer, {
loadSettings,
defaultSettings,
} from '../../util/hooks/useHomeSettings';
import emoteGroups from '../../util/emoteGroups';

const defaults =
loadSettings() ||
emoteGroups.reduce(
(obj, val) => ({
...obj,
[val.paramKey]: val.home.default,
}),
{}
);

const Settings = ({ visible, show }) => {
const [settings, update] = useReducer(settingsReducer, defaults);
const [settings, update] = useReducer(settingsReducer, defaultSettings);

const toggleSettings = () => {
show((x) => !x);

// Reverse condition, state hasn't switched yet
if (!visible) {
localStorage.setItem('groups', JSON.stringify(defaultSettings));
} else {
localStorage.removeItem('groups');
}
};

return (
<>
<Form.Check
type="switch"
label="Preselect emotes by group"
checked={visible}
onChange={() => show((x) => !x)}
onChange={toggleSettings}
className="mb-2"
/>
{visible && (
Expand Down
14 changes: 13 additions & 1 deletion src/util/hooks/useHomeSettings.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import emoteGroups from '../emoteGroups';

const homeSettingsReducer = (state, group) => {
const newState = {
...state,
Expand All @@ -8,7 +10,7 @@ const homeSettingsReducer = (state, group) => {
return newState;
};

export const loadSettings = () => JSON.parse(localStorage.getItem('groups'));
const loadSettings = () => JSON.parse(localStorage.getItem('groups'));

export const getParamString = () => {
const settings = loadSettings();
Expand All @@ -23,4 +25,14 @@ export const getParamString = () => {
}, '');
};

const initialSettings = emoteGroups.reduce(
(obj, val) => ({
...obj,
[val.paramKey]: val.home.default,
}),
{}
);

export const defaultSettings = loadSettings() || initialSettings;

export default homeSettingsReducer;

0 comments on commit 1cf5978

Please sign in to comment.