Skip to content

Commit

Permalink
Merge branch 'develop' into develop
Browse files Browse the repository at this point in the history
  • Loading branch information
victorbalssa authored May 31, 2024
2 parents f88eb92 + e1c4028 commit b16ca78
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 4 deletions.
54 changes: 53 additions & 1 deletion src/components/Screens/HomeScreen.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import React, {
useMemo,
useRef,
useCallback,
useState,
} from 'react';
import { useSelector, useDispatch } from 'react-redux';
import { useFocusEffect, useScrollToTop } from '@react-navigation/native';
Expand All @@ -12,6 +13,7 @@ import {
Animated,
Switch,
View, Pressable,
TouchableOpacity,
} from 'react-native';
import axios from 'axios';
import type { PagerViewOnPageScrollEventData } from 'react-native-pager-view';
Expand Down Expand Up @@ -48,6 +50,35 @@ function AssetsAccounts() {
dispatch.accounts.getAccounts();
return Promise.resolve();
};
const [nameSortOrder, setNameSortOrder] = useState('asc');
const [balanceSortOrder, setBalanceSortOrder] = useState('desc');
const [lastPressed, setLastPressed] = useState(null);

const handleSortPress = (position) => {
setLastPressed(position);
if (position === 'left') {
setNameSortOrder(nameSortOrder === 'asc' ? 'desc' : 'asc');
setBalanceSortOrder(null);
} else if (position === 'right') {
setBalanceSortOrder(balanceSortOrder === 'desc' ? 'asc' : 'desc');
setNameSortOrder(null);
}
};

const sortedAccounts = accounts
.filter((a) => a.display || displayAllAccounts)
.sort((a, b) => {
if (nameSortOrder) {
return nameSortOrder === 'asc'
? a.attributes.name.localeCompare(b.attributes.name)
: b.attributes.name.localeCompare(a.attributes.name);
} else if (balanceSortOrder) {
return balanceSortOrder === 'asc'
? parseFloat(a.attributes.currentBalance) - parseFloat(b.attributes.currentBalance)
: parseFloat(b.attributes.currentBalance) - parseFloat(a.attributes.currentBalance);
}
return 0;
});

return (
<AScrollView
Expand All @@ -69,7 +100,28 @@ function AssetsAccounts() {
</AText>
<Switch style={{ marginHorizontal: 10 }} thumbColor="white" trackColor={{ false: '#767577', true: colors.brandStyle }} onValueChange={onSwitch} value={displayAllAccounts} />
</AStack>
{accounts && accounts.filter((a) => a.display || displayAllAccounts).map((account, index) => (
<AStack px={5} row justifyContent="space-between">
<View style={{ flex: 1, alignItems: 'flex-start', paddingLeft: '5%' }}>
<TouchableOpacity onPress={() => handleSortPress('left')}>
<MaterialCommunityIcons
name="sort"
size={22}
color={lastPressed === 'left' ? colors.primary : colors.text}
/>
</TouchableOpacity>
</View>
<View style={{ flex: 1, alignItems: 'flex-end', paddingRight: '5%' }}>
<TouchableOpacity onPress={() => handleSortPress('right')}>
<MaterialCommunityIcons
name="sort"
size={22}
color={lastPressed === 'right' ? colors.primary : colors.text}
style={{ transform: [{ scaleX: -1 }] }}
/>
</TouchableOpacity>
</View>
</AStack>
{sortedAccounts.map((account, index) => (
<AStack
key={account.id}
row
Expand Down
6 changes: 3 additions & 3 deletions src/models/accounts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -138,11 +138,11 @@ export default createModel<RootModel>()({
dispatch.configuration.apiFetch({ url: `/api/v1/currencies/${currentCode}/accounts?${displayAllAccounts ? '' : 'type=asset'}&date=${end}` }) as Promise<{ data: AccountType[] }>,
dispatch.configuration.apiFetch({ url: '/api/v1/preferences/frontpageAccounts' }) as Promise<{ data: PreferenceType }>,
]);

const filteredAccounts = accounts
.filter((a: AccountType) => a.attributes.active)
.sort((a, b) => (b.attributes.order < a.attributes.order ? 1 : -1));

.filter((a: AccountType) => a.attributes.type != "initial-balance")
.sort((a, b) => (b.attributes.name < a.attributes.name ? 1 : -1));
const accountMap = filteredAccounts.map((account, index) => account.attributes.currentBalance)
if (frontpageAccounts) {
accounts.forEach((a: AccountType, index) => {
accounts[index].display = frontpageAccounts.attributes.data.includes(parseInt(a.id, 10));
Expand Down

0 comments on commit b16ca78

Please sign in to comment.