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-09-16 09:07:02 +00:00
|
|
|
import Page from './Page';
|
2022-09-04 03:28:21 +00:00
|
|
|
import Set from './set';
|
2022-09-18 06:30:37 +00:00
|
|
|
import Settings from './settings';
|
|
|
|
import {getSettings} from './settings.service';
|
2022-07-11 01:00:17 +00:00
|
|
|
|
|
|
|
export default function BestList() {
|
2022-09-04 03:28:21 +00:00
|
|
|
const [bests, setBests] = useState<Set[]>([]);
|
2022-07-11 01:00:17 +00:00
|
|
|
const [search, setSearch] = useState('');
|
2022-09-18 06:30:37 +00:00
|
|
|
const [settings, setSettings] = useState<Settings>();
|
2022-07-11 01:00:17 +00:00
|
|
|
const navigation = useNavigation<NavigationProp<BestPageParams>>();
|
|
|
|
|
|
|
|
const refresh = useCallback(async () => {
|
2022-09-18 06:30:37 +00:00
|
|
|
getSettings().then(setSettings);
|
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-08-25 01:06:50 +00:00
|
|
|
navigation.getParent()?.setOptions({
|
|
|
|
headerRight: () => null,
|
|
|
|
});
|
|
|
|
}, [refresh, navigation]),
|
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={() =>
|
|
|
|
(settings?.images && item.image && (
|
|
|
|
<Image source={{uri: item.image}} style={{height: 75, width: 75}} />
|
|
|
|
)) ||
|
|
|
|
null
|
|
|
|
}
|
2022-07-11 01:00:17 +00:00
|
|
|
/>
|
|
|
|
);
|
|
|
|
|
|
|
|
return (
|
2022-09-16 09:07:02 +00:00
|
|
|
<Page search={search} setSearch={setSearch}>
|
2022-07-11 01:00:17 +00:00
|
|
|
<FlatList
|
|
|
|
ListEmptyComponent={
|
|
|
|
<List.Item
|
|
|
|
title="No exercises yet"
|
2022-07-25 23:45:24 +00:00
|
|
|
description="Once sets have been added, this will highlight your personal bests."
|
2022-07-11 01:00:17 +00:00
|
|
|
/>
|
|
|
|
}
|
|
|
|
renderItem={renderItem}
|
|
|
|
data={bests}
|
|
|
|
/>
|
2022-09-16 09:07:02 +00:00
|
|
|
</Page>
|
2022-07-11 01:00:17 +00:00
|
|
|
);
|
|
|
|
}
|