Massive/BestList.tsx

73 lines
2.0 KiB
TypeScript
Raw Normal View History

2022-07-11 01:00:17 +00:00
import {
NavigationProp,
useFocusEffect,
useNavigation,
} from '@react-navigation/native';
import React, {useCallback, useEffect, useState} from 'react';
import {FlatList, Image} from 'react-native';
import {List} from 'react-native-paper';
import {getBestReps, getBestWeights} from './best.service';
2022-07-11 01:00:17 +00:00
import {BestPageParams} from './BestPage';
import Page from './Page';
import Set from './set';
import {useSettings} from './use-settings';
2022-07-11 01:00:17 +00:00
export default function BestList() {
const [bests, setBests] = useState<Set[]>();
2022-07-11 01:00:17 +00:00
const [search, setSearch] = useState('');
const navigation = useNavigation<NavigationProp<BestPageParams>>();
const {settings} = useSettings();
2022-07-11 01:00:17 +00:00
const refresh = useCallback(async () => {
const weights = await getBestWeights(search);
2022-09-18 06:08:09 +00:00
console.log(`${BestList.name}.refresh:`, {length: weights.length});
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);
}, [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]);
const renderItem = ({item}: {item: Set}) => (
2022-07-11 01:00:17 +00:00
<List.Item
key={item.name}
title={item.name}
description={`${item.reps} x ${item.weight}${item.unit || 'kg'}`}
2022-07-11 01:00:17 +00:00
onPress={() => navigation.navigate('ViewBest', {best: item})}
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 (
<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
);
}