Skip to content

persisting selected theme to localStorage #35

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,8 @@
"language": "typescriptreact",
"autoFix": true
}
]
],
"editor.codeActionsOnSave": {
"source.fixAll.eslint": true
}
}
17,952 changes: 17,952 additions & 0 deletions package-lock.json

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion src/components/layout/Header.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ const ThemeSwitcherButton = styled('button')`
background-color: transparent;
color: ${props => props.theme.colors.white};
}
`
`

const Header: React.SFC<HeaderProps> = ({ title }) => (
<Wrapper>
Expand Down
2 changes: 1 addition & 1 deletion src/configureStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ export default function configureStore(history: History, initialState: Applicati
// we'll be passing from our entry point.
const store = createStore(
createRootReducer(history),
initialState,
// initialState,
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is this commented out?

composeEnhancers(applyMiddleware(routerMiddleware(history), sagaMiddleware))
)

Expand Down
2 changes: 1 addition & 1 deletion src/containers/LayoutContainer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ const LayoutContainer: React.FC<LayoutContainerRenderProps> = ({ render, childre

// Create the `setTheme` handler. We use the `dispatch` we got from `useDispatch()` to create said selector.
const setTheme = (color: ThemeColors) => dispatch(layoutActions.setTheme(color))

console.log(localStorage.getItem('theme'))
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
console.log(localStorage.getItem('theme'))

Please remove console.log calls.

// Create a render/children props wrapper with the above variables set as a callback.
if (render) {
return render({ theme, setTheme })
Expand Down
5 changes: 4 additions & 1 deletion src/store/layout/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,7 @@ import { LayoutActionTypes, ThemeColors } from './types'
// Remember, you can also pass parameters into an action creator. Make sure to
// type them properly as well.

export const setTheme = (theme: ThemeColors) => action(LayoutActionTypes.SET_THEME, theme)
export const setTheme = (theme: ThemeColors) => {
localStorage.setItem('theme', JSON.stringify(theme)) // setting the theme to localState
return action(LayoutActionTypes.SET_THEME, theme)
}
5 changes: 3 additions & 2 deletions src/store/layout/reducer.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import { Reducer } from 'redux'
import { LayoutState, LayoutActionTypes } from './types'

// checking if localStorage is !null
const persistTheme = localStorage.getItem('theme') !== null ? JSON.parse(localStorage.getItem('theme') || '{}') : 'light'
// Type-safe initialState!
export const initialState: LayoutState = {
theme: 'light'
theme: persistTheme
}

// Thanks to Redux 4's much simpler typings, we can take away a lot of typings on the reducer side,
// everything will remain type-safe.
const reducer: Reducer<LayoutState> = (state = initialState, action) => {
Expand Down
2 changes: 1 addition & 1 deletion src/store/layout/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,5 @@ export enum LayoutActionTypes {
// Declare state types with `readonly` modifier to get compile time immutability.
// https://github.com/piotrwitek/react-redux-typescript-guide#state-with-type-level-immutability
export interface LayoutState {
readonly theme: ThemeColors
theme: ThemeColors
Copy link
Owner

@resir014 resir014 Jul 15, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
theme: ThemeColors
theme: ThemeColors

}