Massive/BestList.tsx

99 lines
2.6 KiB
TypeScript
Raw Normal View History

2022-07-11 01:00:17 +00:00
import {
NavigationProp,
useFocusEffect,
useNavigation,
2022-10-31 04:22:08 +00:00
} from '@react-navigation/native'
import {useCallback, useState} from 'react'
import {FlatList, Image} from 'react-native'
import {List} from 'react-native-paper'
import {BestPageParams} from './BestPage'
import {setRepo, settingsRepo} from './db'
2022-10-31 04:22:08 +00:00
import DrawerHeader from './DrawerHeader'
import GymSet from './gym-set'
import Page from './Page'
import Settings from './settings'
2022-07-11 01:00:17 +00:00
export default function BestList() {
2022-10-31 04:22:08 +00:00
const [bests, setBests] = useState<GymSet[]>()
const [term, setTerm] = useState('')
const navigation = useNavigation<NavigationProp<BestPageParams>>()
const [settings, setSettings] = useState<Settings>()
useFocusEffect(
useCallback(() => {
settingsRepo.findOne({where: {}}).then(setSettings)
}, []),
)
2022-07-11 01:00:17 +00:00
const refresh = useCallback(async (value: string) => {
const weights = await setRepo
.createQueryBuilder()
.select()
.addSelect('MAX(weight)', 'weight')
.where('name LIKE :name', {name: `%${value}%`})
.andWhere('NOT hidden')
.groupBy('name')
2022-10-31 04:22:08 +00:00
.getMany()
console.log(`${BestList.name}.refresh:`, {length: weights.length})
let newBest: GymSet[] = []
2022-09-04 04:35:40 +00:00
for (const set of weights) {
const reps = await setRepo
.createQueryBuilder()
.select()
.addSelect('MAX(reps)', 'reps')
.where('name = :name', {name: set.name})
.andWhere('weight = :weight', {weight: set.weight})
.andWhere('NOT hidden')
.groupBy('name')
2022-10-31 04:22:08 +00:00
.getMany()
newBest.push(...reps)
2022-09-04 04:35:40 +00:00
}
2022-10-31 04:22:08 +00:00
setBests(newBest)
}, [])
2022-07-11 01:00:17 +00:00
useFocusEffect(
useCallback(() => {
2022-10-31 04:22:08 +00:00
refresh(term)
}, [refresh, term]),
2022-10-31 04:22:08 +00:00
)
2022-07-11 01:00:17 +00:00
const search = useCallback(
(value: string) => {
2022-10-31 04:22:08 +00:00
setTerm(value)
refresh(value)
},
[refresh],
2022-10-31 04:22:08 +00:00
)
2022-07-11 01:00:17 +00:00
const renderItem = ({item}: {item: GymSet}) => (
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
/>
2022-10-31 04:22:08 +00:00
)
2022-07-11 01:00:17 +00:00
return (
<>
<DrawerHeader name="Best" />
<Page term={term} search={search}>
{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-10-31 04:22:08 +00:00
)
2022-07-11 01:00:17 +00:00
}