Skip to content

Commit 85384df

Browse files
committed
fix(widgets): fix widgets sort order
1 parent 7e421e7 commit 85384df

File tree

2 files changed

+9
-25
lines changed

2 files changed

+9
-25
lines changed

src/components/Widgets.tsx

Lines changed: 5 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,6 @@
11
import { useFocusEffect } from '@react-navigation/native';
22
import { useNavigation } from '@react-navigation/native';
3-
import React, {
4-
ReactElement,
5-
memo,
6-
useCallback,
7-
useMemo,
8-
useState,
9-
} from 'react';
3+
import React, { ReactElement, memo, useCallback, useState } from 'react';
104
import { useTranslation } from 'react-i18next';
115
import { StyleSheet } from 'react-native';
126
import DraggableFlatList, {
@@ -52,18 +46,6 @@ const Widgets = (): ReactElement => {
5246

5347
useFocusEffect(useCallback(() => setEditing(false), []));
5448

55-
const sortedWidgets = useMemo(() => {
56-
const savedWidgets = Object.keys(widgets);
57-
58-
const sorted = savedWidgets.sort((a, b) => {
59-
const indexA = sortOrder.indexOf(a);
60-
const indexB = sortOrder.indexOf(b);
61-
return indexA - indexB;
62-
});
63-
64-
return sorted;
65-
}, [widgets, sortOrder]);
66-
6749
const onDragEnd = useCallback(
6850
({ data }) => {
6951
const order = data.map((id): string => id);
@@ -83,7 +65,7 @@ const Widgets = (): ReactElement => {
8365
({ item: id, drag }: RenderItemParams<string>): ReactElement => {
8466
const initiateDrag = (): void => {
8567
// only allow dragging if there are more than 1 widget
86-
if (sortedWidgets.length > 1 && editing) {
68+
if (sortOrder.length > 1 && editing) {
8769
drag();
8870
}
8971
};
@@ -168,14 +150,14 @@ const Widgets = (): ReactElement => {
168150
/>
169151
);
170152
},
171-
[editing, widgets, sortedWidgets.length],
153+
[editing, widgets, sortOrder.length],
172154
);
173155

174156
return (
175157
<View style={styles.root}>
176158
<View style={styles.title} testID="WidgetsTitle">
177159
<Caption13Up color="secondary">{t('widgets')}</Caption13Up>
178-
{sortedWidgets.length > 0 && (
160+
{sortOrder.length > 0 && (
179161
<TouchableOpacity
180162
hitSlop={{ top: 15, right: 15, bottom: 15, left: 15 }}
181163
testID="WidgetsEdit"
@@ -190,7 +172,7 @@ const Widgets = (): ReactElement => {
190172
</View>
191173

192174
<DraggableFlatList
193-
data={sortedWidgets}
175+
data={sortOrder}
194176
keyExtractor={(id): string => id}
195177
renderItem={(params): ReactElement => (
196178
<ScaleDecorator>{renderItem(params)}</ScaleDecorator>

src/store/slices/widgets.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ export const initialWidgetsState: TWidgetsState = {
1515
blocks: getDefaultOptions('blocks'),
1616
},
1717
onboardedWidgets: false,
18-
sortOrder: [],
18+
sortOrder: ['price', 'news', 'blocks'],
1919
};
2020

2121
export const widgetsSlice = createSlice({
@@ -30,7 +30,9 @@ export const widgetsSlice = createSlice({
3030
action: PayloadAction<{ id: string; options?: any }>,
3131
) => {
3232
const { id, options } = action.payload;
33-
state.sortOrder.push(id);
33+
if (!state.sortOrder.includes(id)) {
34+
state.sortOrder.push(id);
35+
}
3436
state.widgets[id] = options ?? null;
3537
},
3638
deleteWidget: (state, action: PayloadAction<string>) => {

0 commit comments

Comments
 (0)