refactor: set collection order on mounted

This commit is contained in:
Sai K K 2024-11-04 21:20:58 +05:30
parent fc2931855e
commit b58946579c

View File

@ -1,4 +1,4 @@
import React, { useState } from 'react'; import React, { useState, useEffect } from 'react';
import { useDispatch, useSelector } from 'react-redux'; import { useDispatch, useSelector } from 'react-redux';
import { savePreferences } from 'providers/ReduxStore/slices/app'; import { savePreferences } from 'providers/ReduxStore/slices/app';
import { import {
@ -25,7 +25,7 @@ const CollectionsBadge = () => {
const { collectionSortOrder } = useSelector((state) => state.collections); const { collectionSortOrder } = useSelector((state) => state.collections);
const preferences = useSelector((state) => state.app.preferences); const preferences = useSelector((state) => state.app.preferences);
const sortCollectionOrder = () => { const resolveOrderValue = () => {
let order; let order;
switch (collectionSortOrder) { switch (collectionSortOrder) {
case 'default': case 'default':
@ -38,12 +38,24 @@ const CollectionsBadge = () => {
order = 'default'; order = 'default';
break; break;
} }
return order;
};
const sortCollectionOrder = () => {
const order = resolveOrderValue();
dispatch(sortCollections({ order })); dispatch(sortCollections({ order }));
dispatch(savePreferences({ dispatch(savePreferences({
...preferences, ...preferences,
collectionSortOrder: order collectionSortOrder: order
})); }));
}; }
useEffect(() => {
dispatch(sortCollections({ order: preferences.collectionSortOrder }));
}, []);
return ( return (
<div className="items-center mt-2 relative"> <div className="items-center mt-2 relative">
<div className="collections-badge flex items-center justify-between px-2"> <div className="collections-badge flex items-center justify-between px-2">