2022-07-11 01:00:17 +00:00
|
|
|
import {
|
|
|
|
NavigationProp,
|
|
|
|
useFocusEffect,
|
|
|
|
useNavigation,
|
|
|
|
} from '@react-navigation/native';
|
2022-09-04 02:05:25 +00:00
|
|
|
import React, {useCallback, useEffect, useState} from 'react';
|
2022-09-18 06:30:37 +00:00
|
|
|
import {FlatList, Image} from 'react-native';
|
2022-09-16 09:07:02 +00:00
|
|
|
import {List} from 'react-native-paper';
|
2022-09-04 04:56:46 +00:00
|
|
|
import {getBestReps, getBestWeights} from './best.service';
|
2022-07-11 01:00:17 +00:00
|
|
|
import {BestPageParams} from './BestPage';
|
2022-10-23 06:13:58 +00:00
|
|
|
import DrawerHeader from './DrawerHeader';
|
2022-09-16 09:07:02 +00:00
|
|
|
import Page from './Page';
|
2022-09-04 03:28:21 +00:00
|
|
|
import Set from './set';
|
2022-10-01 03:01:07 +00:00
|
|
|
import {useSettings} from './use-settings';
|
2022-07-11 01:00:17 +00:00
|
|
|
|
|
|
|
export default function BestList() {
|
2022-10-13 03:30:02 +00:00
|
|
|
const [bests, setBests] = useState<Set[]>();
|
2022-07-11 01:00:17 +00:00
|
|
|
const [search, setSearch] = useState('');
|
|
|
|
const navigation = useNavigation<NavigationProp<BestPageParams>>();
|
2022-10-01 03:01:07 +00:00
|
|
|
const {settings} = useSettings();
|
2022-07-11 01:00:17 +00:00
|
|
|
|
|
|
|
const refresh = useCallback(async () => {
|
2022-09-04 03:28:21 +00:00
|
|
|
const weights = await getBestWeights(search);
|
2022-09-18 06:08:09 +00:00
|
|
|
console.log(`${BestList.name}.refresh:`, {length: weights.length});
|
2022-09-04 03:28:21 +00:00
|
|
|
let newBest: Set[] = [];
|
2022-09-04 04:35:40 +00:00
|
|
|
for (const set of weights) {
|
|
|
|
const reps = await getBestReps(set.name, set.weight);
|
|
|
|
newBest.push(...reps);
|
|
|
|
}
|
2022-07-11 01:00:17 +00:00
|
|
|
setBests(newBest);
|
2022-09-04 02:05:25 +00:00
|
|
|
}, [search]);
|
2022-07-11 01:00:17 +00:00
|
|
|
|
|
|
|
useFocusEffect(
|
|
|
|
useCallback(() => {
|
|
|
|
refresh();
|
2022-10-23 06:13:58 +00:00
|
|
|
}, [refresh]),
|
2022-07-11 01:00:17 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
refresh();
|
|
|
|
}, [search, refresh]);
|
|
|
|
|
2022-09-04 03:28:21 +00:00
|
|
|
const renderItem = ({item}: {item: Set}) => (
|
2022-07-11 01:00:17 +00:00
|
|
|
<List.Item
|
|
|
|
key={item.name}
|
|
|
|
title={item.name}
|
2022-09-18 06:30:37 +00:00
|
|
|
description={`${item.reps} x ${item.weight}${item.unit || 'kg'}`}
|
2022-07-11 01:00:17 +00:00
|
|
|
onPress={() => navigation.navigate('ViewBest', {best: item})}
|
2022-09-18 06:30:37 +00:00
|
|
|
left={() =>
|
2022-09-21 01:48:45 +00:00
|
|
|
(settings.images && item.image && (
|
2022-09-18 06:30:37 +00:00
|
|
|
<Image source={{uri: item.image}} style={{height: 75, width: 75}} />
|
|
|
|
)) ||
|
|
|
|
null
|
|
|
|
}
|
2022-07-11 01:00:17 +00:00
|
|
|
/>
|
|
|
|
);
|
|
|
|
|
|
|
|
return (
|
2022-10-22 23:35:58 +00:00
|
|
|
<>
|
2022-10-23 06:13:58 +00:00
|
|
|
<DrawerHeader name="Best" />
|
2022-10-22 23:35:58 +00:00
|
|
|
<Page search={search} setSearch={setSearch}>
|
|
|
|
{bests?.length === 0 ? (
|
|
|
|
<List.Item
|
|
|
|
title="No exercises yet"
|
|
|
|
description="Once sets have been added, this will highlight your personal bests."
|
|
|
|
/>
|
|
|
|
) : (
|
|
|
|
<FlatList style={{flex: 1}} renderItem={renderItem} data={bests} />
|
|
|
|
)}
|
|
|
|
</Page>
|
|
|
|
</>
|
2022-07-11 01:00:17 +00:00
|
|
|
);
|
|
|
|
}
|