Skip to content

Commit

Permalink
feat: persist locale choice
Browse files Browse the repository at this point in the history
  • Loading branch information
ALCC01 committed Aug 12, 2024
1 parent 0e66891 commit 16a8ec6
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 2 deletions.
6 changes: 6 additions & 0 deletions src/app.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,14 @@ import StatsModal from './components/modals/StatsModal'
import Footer from './components/Footer'
import InfoModal from './components/modals/InfoModal'
import FeedbackCard from './components/FeedbackCard'
import { useEffect } from 'preact/hooks'
import i18n from './i18n'

export const App: FunctionComponent = () => {
useEffect(() => {
void i18n.changeLanguage(store.getState().ui.locale)
})

return (
<Provider store={store}>
<div className="flex h-screen p-4">
Expand Down
6 changes: 4 additions & 2 deletions src/store/index.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
import { configureStore } from '@reduxjs/toolkit'
import { type TypedUseSelectorHook, useDispatch, useSelector } from 'react-redux'
import { LS_PATIENTS, persistMiddleware } from './persistMiddleware'
import { LS_PATIENTS, LS_UI, persistMiddleware } from './persistMiddleware'
import patients from './patients'
import ui from './ui'

const patientsState = JSON.parse(localStorage.getItem(LS_PATIENTS) ?? 'null') ?? undefined
const uiState = JSON.parse(localStorage.getItem(LS_UI) ?? 'null') ?? undefined

export const store = configureStore({
preloadedState: {
patients: patientsState
patients: patientsState,
ui: uiState
},
reducer: {
patients,
Expand Down
10 changes: 10 additions & 0 deletions src/store/persistMiddleware.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
import { type TypedStartListening, createListenerMiddleware, isAnyOf } from '@reduxjs/toolkit'
import { type AppDispatch, type RootState } from '.'
import { addPatient, checkCapillaryRefill, checkMentalStatus, checkRespiratoryRate, checkWalking, clearAirway, clearPatients, controlBleeding, removePatient, setCode } from './patients'
import { setLocale } from './ui'
import { pick } from '../utils'

export const LS_PATIENTS = 'patients'
export const LS_UI = 'ui'

export const persistMiddleware = createListenerMiddleware()

Expand All @@ -16,3 +19,10 @@ startListening({
localStorage.setItem(LS_PATIENTS, JSON.stringify(listener.getState().patients))
}
})

startListening({
matcher: isAnyOf(setLocale),
effect: (_, listener) => {
localStorage.setItem(LS_UI, JSON.stringify(pick(['locale'], listener.getState().ui)))
}
})
3 changes: 3 additions & 0 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,3 +49,6 @@ export const localeToFlag = (cc: string): string => {
.map(ch => String.fromCodePoint(ch.charCodeAt(0) + 0x1F1A5))
.join('')
}

export const pick = (keys: string[], object: Record<string, any>): Record<string, any> =>
keys.reduce((acc, key) => ({ ...acc, [key]: object[key] }), {})

0 comments on commit 16a8ec6

Please sign in to comment.