Skip to content

Commit 86d8aab

Browse files
committedMar 5, 2025·
solve the unusual behavior with the toggling of the filters
1 parent d86b418 commit 86d8aab

File tree

1 file changed

+45
-26
lines changed

1 file changed

+45
-26
lines changed
 

‎src/components/shared/sidebar/sidebar.tsx

+45-26
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ import ToggleCollapse from '../Seller/ToggleCollapse';
3333

3434
import { AppContext } from '../../../../context/AppContextProvider';
3535
import logger from '../../../../logger.config.mjs';
36+
import { ImSpinner2 } from 'react-icons/im';
3637

3738
interface MenuItem {
3839
id: number;
@@ -60,6 +61,17 @@ function Sidebar(props: any) {
6061
const locale = useLocale();
6162
const router = useRouter();
6263

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+
6375
const { currentUser, autoLoginUser, setReload, showAlert } =
6476
useContext(AppContext);
6577
const [dbUserSettings, setDbUserSettings] = useState<IUserSettings | null>(
@@ -93,6 +105,15 @@ function Sidebar(props: any) {
93105
const [showPrivacyPolicyModel, setShowPrivacyPolicyModel] = useState(false);
94106
const [showTermsOfServiceModel, setShowTermsOfServiceModel] = useState(false);
95107
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+
});
96117

97118
useEffect(() => {
98119
if (!currentUser) {
@@ -232,7 +253,6 @@ function Sidebar(props: any) {
232253
setIsSaveEnabled(isFormFilled);
233254
};
234255

235-
236256
const translateMenuTitle = (title: string): string => {
237257
switch (title) {
238258
case 'Languages':
@@ -281,17 +301,7 @@ function Sidebar(props: any) {
281301
},
282302
];
283303

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-
];
293304

294-
295305
// Function to save data to the database
296306
const handleSave = async () => {
297307
// check if user is authenticated and form is valid
@@ -340,24 +350,27 @@ function Sidebar(props: any) {
340350
};
341351

342352
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+
};
343363

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));
354366

355-
356367
try {
368+
setFilterLoading({...filterLoading, [target]: true});
357369
const data = await createUserSettings(formDataToSend);
358370
if (data.settings) {
359371
setDbUserSettings(data.settings);
360-
setIsSaveEnabled(false);
372+
setFilterLoading({...filterLoading, [target]: false});
373+
// setIsSaveEnabled(false);
361374
logger.info('User Settings saved successfully:', { data });
362375
// showAlert(
363376
// t('SIDE_NAVIGATION.VALIDATION.SUCCESSFUL_PREFERENCES_SUBMISSION'),
@@ -367,12 +380,12 @@ function Sidebar(props: any) {
367380
// }
368381
}
369382
} catch (error) {
383+
setFilterLoading({...filterLoading, [target]: false});
370384
logger.error('Error saving user settings:', error);
371385
showAlert(
372386
t('SIDE_NAVIGATION.VALIDATION.UNSUCCESSFUL_PREFERENCES_SUBMISSION'),
373387
);
374388
}
375-
376389
};
377390

378391
return (
@@ -477,13 +490,19 @@ function Sidebar(props: any) {
477490
key={index}
478491
className="mb-1 flex gap-2 pr-7 items-center cursor-pointer"
479492
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?.[
481498
filter.target as keyof IUserSettings['search_filters']
482499
] ? (
483500
<IoCheckmark />
484501
) : (
485502
<IoClose />
486-
)}
503+
)
504+
)
505+
}
487506
{filter.title}
488507
</div>
489508
))}

0 commit comments

Comments
 (0)
Please sign in to comment.