1- import type { Reducer , ThunkAction } from '@reduxjs/toolkit' ;
1+ import type { Store } from '@reduxjs/toolkit' ;
2+ import { createSlice } from '@reduxjs/toolkit' ;
23
3- import type { RootState } from '../..' ;
4- import type { SettingsObject } from '../../../services/settings' ;
54import { DEFAULT_USER_SETTINGS , settingsManager } from '../../../services/settings' ;
5+ import { parseJson } from '../../../utils/utils' ;
6+ import type { AppDispatch } from '../../defaultStore' ;
67
7- import type {
8- ProblemFilterValue ,
9- SetSettingValueAction ,
10- SettingsAction ,
11- SettingsRootStateSlice ,
12- SettingsState ,
13- } from './types' ;
14-
15- const CHANGE_PROBLEM_FILTER = 'settings/CHANGE_PROBLEM_FILTER' ;
16- export const SET_SETTING_VALUE = 'settings/SET_VALUE' ;
17- export const SET_USER_SETTINGS = 'settings/SET_USER_SETTINGS' ;
8+ import type { ProblemFilterValue , SettingsState } from './types' ;
189
1910export const ProblemFilterValues = {
2011 ALL : 'All' ,
@@ -24,71 +15,60 @@ export const ProblemFilterValues = {
2415const userSettings = settingsManager . extractSettingsFromLS ( DEFAULT_USER_SETTINGS ) ;
2516const systemSettings = window . systemSettings || { } ;
2617
27- export const initialState = {
18+ export const initialState : SettingsState = {
2819 problemFilter : ProblemFilterValues . ALL ,
2920 userSettings,
3021 systemSettings,
3122} ;
3223
33- const settings : Reducer < SettingsState , SettingsAction > = ( state = initialState , action ) => {
34- switch ( action . type ) {
35- case CHANGE_PROBLEM_FILTER :
36- return {
37- ...state ,
38- problemFilter : action . data ,
39- } ;
40-
41- case SET_SETTING_VALUE : {
42- const newSettings = {
43- ...state . userSettings ,
44- [ action . data . name ] : action . data . value ,
45- } ;
46-
47- return {
48- ...state ,
49- userSettings : newSettings ,
50- } ;
51- }
52- case SET_USER_SETTINGS : {
53- return {
54- ...state ,
55- userSettings : {
56- ...state . userSettings ,
57- ...action . data ,
58- } ,
59- } ;
60- }
61- default :
62- return state ;
63- }
64- } ;
65-
66- export const setSettingValue = (
67- name : string ,
68- value : unknown ,
69- ) : ThunkAction < void , RootState , unknown , SetSettingValueAction > => {
70- return ( dispatch ) => {
71- dispatch ( { type : SET_SETTING_VALUE , data : { name, value} } ) ;
24+ const settingsSlice = createSlice ( {
25+ name : 'settings' ,
26+ initialState,
27+ reducers : ( create ) => ( {
28+ changeFilter : create . reducer < ProblemFilterValue > ( ( state , action ) => {
29+ state . problemFilter = action . payload ;
30+ } ) ,
31+ setSettingValue : create . reducer < { name : string ; value : unknown } > ( ( state , action ) => {
32+ state . userSettings [ action . payload . name ] = action . payload . value ;
33+ } ) ,
34+ } ) ,
35+ selectors : {
36+ getSettingValue : ( state , name : string ) => state . userSettings [ name ] ,
37+ selectProblemFilter : ( state ) => state . problemFilter ,
38+ } ,
39+ } ) ;
40+
41+ export const { changeFilter} = settingsSlice . actions ;
42+ export const { getSettingValue, selectProblemFilter} = settingsSlice . selectors ;
43+
44+ export const setSettingValue = ( name : string , value : unknown ) => {
45+ return ( dispatch : AppDispatch ) => {
46+ dispatch ( settingsSlice . actions . setSettingValue ( { name, value} ) ) ;
7247
7348 settingsManager . setUserSettingsValue ( name , value ) ;
7449 } ;
7550} ;
7651
77- export const setUserSettings = ( data : SettingsObject ) => {
78- return { type : SET_USER_SETTINGS , data} as const ;
79- } ;
80-
81- export const getSettingValue = ( state : SettingsRootStateSlice , name : string ) => {
82- return state . settings . userSettings [ name ] ;
83- } ;
84-
85- export const changeFilter = ( filter : ProblemFilterValue ) => {
86- return {
87- type : CHANGE_PROBLEM_FILTER ,
88- data : filter ,
89- } as const ;
90- } ;
52+ export function syncUserSettingsFromLS ( store : Store ) {
53+ if ( typeof window === 'undefined' ) {
54+ return ;
55+ }
9156
92- export const selectProblemFilter = ( state : SettingsRootStateSlice ) => state . settings . problemFilter ;
57+ window . addEventListener ( 'storage' , ( event ) => {
58+ if ( event . key && event . key in DEFAULT_USER_SETTINGS ) {
59+ const name = event . key as keyof typeof DEFAULT_USER_SETTINGS ;
60+ let value = DEFAULT_USER_SETTINGS [ name ] ;
61+ if ( event . newValue !== null ) {
62+ value = parseJson ( event . newValue ) ;
63+ }
64+ store . dispatch (
65+ settingsSlice . actions . setSettingValue ( {
66+ name,
67+ value,
68+ } ) ,
69+ ) ;
70+ }
71+ } ) ;
72+ }
9373
94- export default settings ;
74+ export default settingsSlice . reducer ;
0 commit comments