@@ -33,6 +33,7 @@ import ToggleCollapse from '../Seller/ToggleCollapse';
33
33
34
34
import { AppContext } from '../../../../context/AppContextProvider' ;
35
35
import logger from '../../../../logger.config.mjs' ;
36
+ import { ImSpinner2 } from 'react-icons/im' ;
36
37
37
38
interface MenuItem {
38
39
id : number ;
@@ -60,6 +61,17 @@ function Sidebar(props: any) {
60
61
const locale = useLocale ( ) ;
61
62
const router = useRouter ( ) ;
62
63
64
+
65
+ const Filters = [
66
+ { target : 'include_active_sellers' , title : 'Include Active Sellers' } ,
67
+ { target : 'include_inactive_sellers' , title : 'Include Inactive Sellers' } ,
68
+ { target : 'include_test_sellers' , title : 'Include Test Sellers' } ,
69
+ { target : 'include_trust_level_100' , title : 'Include Trust 100%' } ,
70
+ { target : 'include_trust_level_80' , title : 'Include Trust 80%' } ,
71
+ { target : 'include_trust_level_50' , title : 'Include Trust 50%' } ,
72
+ { target : 'include_trust_level_0' , title : 'Include Trust 0%' } ,
73
+ ] ;
74
+
63
75
const { currentUser, autoLoginUser, setReload, showAlert } =
64
76
useContext ( AppContext ) ;
65
77
const [ dbUserSettings , setDbUserSettings ] = useState < IUserSettings | null > (
@@ -93,6 +105,15 @@ function Sidebar(props: any) {
93
105
const [ showPrivacyPolicyModel , setShowPrivacyPolicyModel ] = useState ( false ) ;
94
106
const [ showTermsOfServiceModel , setShowTermsOfServiceModel ] = useState ( false ) ;
95
107
const [ isSaveEnabled , setIsSaveEnabled ] = useState ( false ) ;
108
+ const [ filterLoading , setFilterLoading ] = useState ( {
109
+ include_active_sellers : false ,
110
+ include_inactive_sellers : false ,
111
+ include_test_sellers : false ,
112
+ include_trust_level_100 : false ,
113
+ include_trust_level_80 : false ,
114
+ include_trust_level_50 : false ,
115
+ include_trust_level_0 : false ,
116
+ } ) ;
96
117
97
118
useEffect ( ( ) => {
98
119
if ( ! currentUser ) {
@@ -232,7 +253,6 @@ function Sidebar(props: any) {
232
253
setIsSaveEnabled ( isFormFilled ) ;
233
254
} ;
234
255
235
-
236
256
const translateMenuTitle = ( title : string ) : string => {
237
257
switch ( title ) {
238
258
case 'Languages' :
@@ -281,17 +301,7 @@ function Sidebar(props: any) {
281
301
} ,
282
302
] ;
283
303
284
- const Filters = [
285
- { target : 'include_active_sellers' , title : 'Include Active Sellers' } ,
286
- { target : 'include_inactive_sellers' , title : 'Include Inactive Sellers' } ,
287
- { target : 'include_test_sellers' , title : 'Include Test Sellers' } ,
288
- { target : 'include_trust_level_100' , title : 'Include Trust 100%' } ,
289
- { target : 'include_trust_level_80' , title : 'Include Trust 80%' } ,
290
- { target : 'include_trust_level_50' , title : 'Include Trust 50%' } ,
291
- { target : 'include_trust_level_0' , title : 'Include Trust 0%' } ,
292
- ] ;
293
304
294
-
295
305
// Function to save data to the database
296
306
const handleSave = async ( ) => {
297
307
// check if user is authenticated and form is valid
@@ -340,24 +350,27 @@ function Sidebar(props: any) {
340
350
} ;
341
351
342
352
const handleSearchFilter = async ( target : string ) => {
353
+
354
+ if ( ! dbUserSettings ?. search_filters ) return ;
355
+
356
+ const updatedFilters = {
357
+ ...dbUserSettings . search_filters ,
358
+ [ target ] :
359
+ ! dbUserSettings . search_filters [
360
+ target as keyof IUserSettings [ 'search_filters' ]
361
+ ] ,
362
+ } ;
343
363
344
- // check if user is authenticated and form is valid
345
- if ( ! currentUser ) {
346
- logger . warn ( 'Form submission failed: User not authenticated.' ) ;
347
- return toast . error (
348
- t ( 'SHARED.VALIDATION.SUBMISSION_FAILED_USER_NOT_AUTHENTICATED' ) ,
349
- ) ;
350
- }
351
-
352
- const formDataToSend = new FormData ( ) ;
353
- formDataToSend . append ( 'search_filters' , JSON . stringify ( { [ target as keyof IUserSettings [ 'search_filters' ] ] : ! dbUserSettings ?. search_filters ?. [ target as keyof IUserSettings [ 'search_filters' ] ] } ) ) ;
364
+ const formDataToSend = new FormData ( ) ;
365
+ formDataToSend . append ( 'search_filters' , JSON . stringify ( updatedFilters ) ) ;
354
366
355
-
356
367
try {
368
+ setFilterLoading ( { ...filterLoading , [ target ] : true } ) ;
357
369
const data = await createUserSettings ( formDataToSend ) ;
358
370
if ( data . settings ) {
359
371
setDbUserSettings ( data . settings ) ;
360
- setIsSaveEnabled ( false ) ;
372
+ setFilterLoading ( { ...filterLoading , [ target ] : false } ) ;
373
+ // setIsSaveEnabled(false);
361
374
logger . info ( 'User Settings saved successfully:' , { data } ) ;
362
375
// showAlert(
363
376
// t('SIDE_NAVIGATION.VALIDATION.SUCCESSFUL_PREFERENCES_SUBMISSION'),
@@ -367,12 +380,12 @@ function Sidebar(props: any) {
367
380
// }
368
381
}
369
382
} catch ( error ) {
383
+ setFilterLoading ( { ...filterLoading , [ target ] : false } ) ;
370
384
logger . error ( 'Error saving user settings:' , error ) ;
371
385
showAlert (
372
386
t ( 'SIDE_NAVIGATION.VALIDATION.UNSUCCESSFUL_PREFERENCES_SUBMISSION' ) ,
373
387
) ;
374
388
}
375
-
376
389
} ;
377
390
378
391
return (
@@ -477,13 +490,19 @@ function Sidebar(props: any) {
477
490
key = { index }
478
491
className = "mb-1 flex gap-2 pr-7 items-center cursor-pointer"
479
492
onClick = { ( ) => handleSearchFilter ( filter . target ) } >
480
- { dbUserSettings ?. search_filters ?. [
493
+ {
494
+ filterLoading [ filter . target as keyof typeof filterLoading ] ? (
495
+ < ImSpinner2 className = "animate-spin" />
496
+ ) : (
497
+ dbUserSettings ?. search_filters ?. [
481
498
filter . target as keyof IUserSettings [ 'search_filters' ]
482
499
] ? (
483
500
< IoCheckmark />
484
501
) : (
485
502
< IoClose />
486
- ) }
503
+ )
504
+ )
505
+ }
487
506
{ filter . title }
488
507
</ div >
489
508
) ) }
0 commit comments