@@ -11,7 +11,7 @@ import {api} from '../api';
1111
1212import type { SettingKey } from './constants' ;
1313import { DEFAULT_USER_SETTINGS , SETTINGS_OPTIONS } from './constants' ;
14- import { readSettingValueFromLS } from './utils' ;
14+ import { parseSettingValue , readSettingValueFromLS , setSettingValueToLS } from './utils' ;
1515
1616export const settingsApi = api . injectEndpoints ( {
1717 endpoints : ( builder ) => ( {
@@ -23,7 +23,7 @@ export const settingsApi = api.injectEndpoints({
2323 return { data : undefined } ;
2424 }
2525
26- const defaultValue = DEFAULT_USER_SETTINGS [ name as SettingKey ] ;
26+ const defaultValue = DEFAULT_USER_SETTINGS [ name as SettingKey ] as unknown ;
2727
2828 // Do not load LS value from always sync values.
2929 // In case there is no settings service
@@ -34,14 +34,31 @@ export const settingsApi = api.injectEndpoints({
3434
3535 const savedValue = readSettingValueFromLS ( name ) ;
3636
37- return { data : savedValue ?? defaultValue } ;
37+ const data = ( savedValue ?? defaultValue ) as unknown ;
38+
39+ return { data} ;
3840 } ,
3941 } ) ,
4042 getSingleSetting : builder . query ( {
41- queryFn : async ( { name, user} : GetSingleSettingParams , baseApi ) => {
43+ queryFn : async ( { name, user} : Partial < GetSingleSettingParams > , baseApi ) => {
4244 try {
43- if ( ! window . api . metaSettings ) {
44- throw new Error ( 'MetaSettings API is not available' ) ;
45+ if ( ! name ) {
46+ throw new Error ( 'Setting name is not provided' ) ;
47+ }
48+ const dispatch = baseApi . dispatch as AppDispatch ;
49+
50+ if ( ! window . api . metaSettings || ! user ) {
51+ if ( SETTINGS_OPTIONS [ name ] ?. preventSyncWithLS ) {
52+ const savedValue = readSettingValueFromLS ( name ) ;
53+ const value = savedValue ?? DEFAULT_USER_SETTINGS [ name as SettingKey ] ;
54+ dispatch ( settingsApi . util . upsertQueryData ( 'setting' , { name} , value ) ) ;
55+ }
56+
57+ if ( ! user ) {
58+ throw new Error ( 'User is not provided' ) ;
59+ } else {
60+ throw new Error ( 'MetaSettings API is not available' ) ;
61+ }
4562 }
4663 const data = await window . api . metaSettings . getSingleSetting ( {
4764 name,
@@ -50,25 +67,38 @@ export const settingsApi = api.injectEndpoints({
5067 preventBatching : SETTINGS_OPTIONS [ name ] ?. preventBatching ,
5168 } ) ;
5269
53- const dispatch = baseApi . dispatch as AppDispatch ;
70+ if ( isNil ( data . value ) ) {
71+ // Try to sync local value if there is no backend value
72+ syncLocalValueToMetaIfNoData ( data , dispatch ) ;
73+ } else {
74+ setSettingValueToLS ( name , data . value ) ;
5475
55- // Try to sync local value if there is no backend value
56- syncLocalValueToMetaIfNoData ( data , dispatch ) ;
76+ const parsedValue = parseSettingValue ( data . value ) ;
77+ dispatch ( settingsApi . util . upsertQueryData ( 'setting' , { name} , parsedValue ) ) ;
78+ }
5779
58- return { data} ;
80+ return { data : data } ;
5981 } catch ( error ) {
6082 return { error} ;
6183 }
6284 } ,
6385 } ) ,
6486 setSingleSetting : builder . mutation ( {
65- queryFn : async ( params : SetSingleSettingParams ) => {
87+ queryFn : async ( { name , user , value } : Partial < SetSingleSettingParams > ) => {
6688 try {
67- if ( ! window . api . metaSettings ) {
68- throw new Error ( 'MetaSettings API is not available' ) ;
89+ if ( ! name || isNil ( value ) ) {
90+ throw new Error ( 'Setting name is not provided' ) ;
91+ }
92+ if ( ! window . api . metaSettings || ! user ) {
93+ // throw new Error('MetaSettings API is not available');
94+ return { data : undefined } ;
6995 }
7096
71- const data = await window . api . metaSettings . setSingleSetting ( params ) ;
97+ const data = await window . api . metaSettings . setSingleSetting ( {
98+ name,
99+ user,
100+ value,
101+ } ) ;
72102
73103 if ( data . status !== 'SUCCESS' ) {
74104 throw new Error ( 'Setting status is not SUCCESS' ) ;
@@ -80,25 +110,30 @@ export const settingsApi = api.injectEndpoints({
80110 }
81111 } ,
82112 async onQueryStarted ( args , { dispatch, queryFulfilled} ) {
83- const { name, user , value} = args ;
113+ const { name, value} = args ;
84114
85115 // Optimistically update existing cache entry
86116 const patchResult = dispatch (
87- settingsApi . util . updateQueryData ( 'getSingleSetting ' , { name, user } , ( draft ) => {
88- return { ... draft , name , user , value} ;
117+ settingsApi . util . updateQueryData ( 'setting ' , { name} , ( ) => {
118+ return value ;
89119 } ) ,
90120 ) ;
121+
91122 try {
92123 await queryFulfilled ;
124+ setSettingValueToLS ( name , value ) ;
93125 } catch {
94126 patchResult . undo ( ) ;
95127 }
96128 } ,
97129 } ) ,
98130 getSettings : builder . query ( {
99- queryFn : async ( { name, user} : GetSettingsParams , baseApi ) => {
131+ queryFn : async ( { name, user} : Partial < GetSettingsParams > , baseApi ) => {
100132 try {
101- if ( ! window . api . metaSettings ) {
133+ if ( ! name ) {
134+ throw new Error ( 'Settings names are not provided' ) ;
135+ }
136+ if ( ! window . api . metaSettings || ! user ) {
102137 throw new Error ( 'MetaSettings API is not available' ) ;
103138 }
104139 const data = await window . api . metaSettings . getSettings ( { name, user} ) ;
@@ -123,13 +158,26 @@ export const settingsApi = api.injectEndpoints({
123158 newValue ,
124159 ) ,
125160 ) . then ( ( ) => {
126- // Try to sync local value if there is no backend value
127- // Do it after upsert if finished to ensure proper values update order
128- // 1. New entry added to cache with nil value
129- // 2. Positive entry update - local storage value replace nil in cache
130- // 3.1. Set is successful, local value in cache
131- // 3.2. Set is not successful, cache value reverted to previous nil
132- syncLocalValueToMetaIfNoData ( settingData , dispatch ) ;
161+ if ( isNil ( settingData . value ) ) {
162+ // Try to sync local value if there is no backend value
163+ // Do it after upsert if finished to ensure proper values update order
164+ // 1. New entry added to cache with nil value
165+ // 2. Positive entry update - local storage value replace nil in cache
166+ // 3.1. Set is successful, local value in cache
167+ // 3.2. Set is not successful, cache value reverted to previous nil
168+ syncLocalValueToMetaIfNoData ( settingData , dispatch ) ;
169+ } else {
170+ setSettingValueToLS ( settingName , data . value ) ;
171+
172+ const parsedValue = parseSettingValue ( settingData . value ) ;
173+ dispatch (
174+ settingsApi . util . upsertQueryData (
175+ 'setting' ,
176+ { name : settingName } ,
177+ parsedValue ,
178+ ) ,
179+ ) ;
180+ }
133181 } ) ;
134182
135183 patches . push ( patch ) ;
0 commit comments