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-07-11 01:00:17 +00:00
|
|
|
import {FlatList, StyleSheet, View} from 'react-native';
|
|
|
|
import {List, Searchbar} 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-04 03:28:21 +00:00
|
|
|
import Set from './set';
|
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('');
|
|
|
|
const navigation = useNavigation<NavigationProp<BestPageParams>>();
|
|
|
|
|
|
|
|
const refresh = useCallback(async () => {
|
2022-09-04 03:28:21 +00:00
|
|
|
const weights = await getBestWeights(search);
|
2022-09-04 04:35:40 +00:00
|
|
|
console.log(BestList.name, {weights});
|
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}
|
|
|
|
description={`${item.reps} x ${item.weight}${item.unit}`}
|
|
|
|
onPress={() => navigation.navigate('ViewBest', {best: item})}
|
|
|
|
/>
|
|
|
|
);
|
|
|
|
|
|
|
|
return (
|
|
|
|
<View style={styles.container}>
|
|
|
|
<Searchbar placeholder="Search" value={search} onChangeText={setSearch} />
|
|
|
|
<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}
|
|
|
|
/>
|
|
|
|
</View>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
const styles = StyleSheet.create({
|
|
|
|
container: {
|
|
|
|
padding: 10,
|
|
|
|
flexGrow: 1,
|
|
|
|
paddingBottom: '10%',
|
|
|
|
},
|
|
|
|
});
|