-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2025 from FaberVitale/fix/alm-update-counter-example
- Loading branch information
Showing
7 changed files
with
77 additions
and
73 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
36 changes: 36 additions & 0 deletions
36
examples/action-listener/counter/src/components/App/App.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import React, { useEffect } from 'react' | ||
import { Provider } from 'react-redux' | ||
import type { Unsubscribe } from '@reduxjs/toolkit' | ||
import { setupThemeListeners } from '../../services/theme/listeners' | ||
import { setupCounterListeners } from '../../services/counter/listeners' | ||
import { ChangeThemeForm } from '../ChangeThemeForm/ChangeThemeForm' | ||
import { CounterList } from '../CounterList/CounterList' | ||
import { CreateCounterForm } from '../CreateCounterForm/CreateCounterForm' | ||
import { store, startAppListening } from '../../store' | ||
|
||
|
||
export function App() { | ||
useEffect(() => { | ||
const subscriptions: Unsubscribe[] = [ | ||
setupCounterListeners(startAppListening), | ||
setupThemeListeners(startAppListening), | ||
] | ||
|
||
return () => subscriptions.forEach((unsubscribe) => unsubscribe()) | ||
}, []) | ||
|
||
return ( | ||
<React.StrictMode> | ||
<Provider store={store}> | ||
<main className={'main'}> | ||
<header className="App-header"> | ||
<h1>Counter example</h1> | ||
</header> | ||
<ChangeThemeForm /> | ||
<CreateCounterForm /> | ||
<CounterList /> | ||
</main> | ||
</Provider> | ||
</React.StrictMode> | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,29 +1,11 @@ | ||
import React from 'react' | ||
import ReactDOM from 'react-dom' | ||
import './index.css' | ||
import { Provider } from 'react-redux' | ||
import { store } from './store' | ||
import { themeActions } from './services/theme/slice' | ||
import { ChangeThemeForm } from './components/ChangeThemeForm/ChangeThemeForm' | ||
import { CounterList } from './components/CounterList/CounterList' | ||
import { CreateCounterForm } from './components/CreateCounterForm/CreateCounterForm' | ||
import { App } from './components/App/App' | ||
|
||
if (window.matchMedia('(prefers-color-scheme: dark)').matches) { | ||
store.dispatch(themeActions.changeColorScheme('dark')) | ||
} | ||
|
||
ReactDOM.render( | ||
<React.StrictMode> | ||
<Provider store={store}> | ||
<main className={'main'}> | ||
<header className="App-header"> | ||
<h1>Counter example</h1> | ||
</header> | ||
<ChangeThemeForm /> | ||
<CreateCounterForm /> | ||
<CounterList /> | ||
</main> | ||
</Provider> | ||
</React.StrictMode>, | ||
document.getElementById('root') | ||
) | ||
ReactDOM.render(<App />, document.getElementById('root')) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
25 changes: 13 additions & 12 deletions
25
examples/action-listener/counter/src/services/theme/listeners.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,21 +1,22 @@ | ||
import { themeActions } from './slice' | ||
import type { AppActionListenerMiddleware } from '../../store' | ||
import type { AppStartListening } from '../../store' | ||
import { Unsubscribe } from '@reduxjs/toolkit' | ||
|
||
function onChangeColorScheme( | ||
action: ReturnType<typeof themeActions.changeColorScheme> | ||
) { | ||
if (action.payload === 'light') { | ||
document.documentElement.classList.remove('dark') | ||
} else { | ||
document.documentElement.classList.add('dark') | ||
} | ||
document.documentElement.classList.toggle('dark', action.payload !== 'light') | ||
} | ||
|
||
export function setupThemeListeners( | ||
actionListener: AppActionListenerMiddleware | ||
) { | ||
return actionListener.addListener({ | ||
actionCreator: themeActions.changeColorScheme, | ||
listener: onChangeColorScheme, | ||
}) | ||
startListening: AppStartListening | ||
): Unsubscribe { | ||
const listeners = [ | ||
startListening({ | ||
actionCreator: themeActions.changeColorScheme, | ||
effect: onChangeColorScheme, | ||
}), | ||
] | ||
|
||
return () => listeners.forEach((unsubscribe) => unsubscribe()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters