2022-07-09 01:27:19 +00:00
|
|
|
import React, {useCallback, useContext, useEffect, useState} from 'react';
|
2022-07-04 04:03:48 +00:00
|
|
|
import {FlatList, StyleSheet, View} from 'react-native';
|
2022-07-07 02:18:38 +00:00
|
|
|
import {List, Searchbar} from 'react-native-paper';
|
|
|
|
import {DatabaseContext} from './App';
|
2022-07-08 12:11:10 +00:00
|
|
|
import Best from './best';
|
|
|
|
import ViewBest from './ViewBest';
|
2022-07-03 01:50:01 +00:00
|
|
|
|
2022-07-08 03:20:03 +00:00
|
|
|
export default function BestPage() {
|
2022-07-08 12:11:10 +00:00
|
|
|
const [bests, setBests] = useState<Best[]>([]);
|
2022-07-04 04:03:48 +00:00
|
|
|
const [search, setSearch] = useState('');
|
2022-07-06 04:27:36 +00:00
|
|
|
const [refreshing, setRefresing] = useState(false);
|
2022-07-08 12:11:10 +00:00
|
|
|
const [best, setBest] = useState<Best>();
|
2022-07-07 02:18:38 +00:00
|
|
|
const db = useContext(DatabaseContext);
|
2022-07-04 04:03:48 +00:00
|
|
|
|
2022-07-09 01:27:19 +00:00
|
|
|
const refresh = useCallback(async () => {
|
|
|
|
const bestWeight = `
|
|
|
|
SELECT name, reps, unit, MAX(weight) AS weight
|
|
|
|
FROM sets
|
|
|
|
WHERE name LIKE ?
|
|
|
|
GROUP BY name;
|
|
|
|
`;
|
|
|
|
const bestReps = `
|
|
|
|
SELECT name, MAX(reps) as reps, unit, weight
|
|
|
|
FROM sets
|
|
|
|
WHERE name = ?
|
|
|
|
AND weight = ?
|
|
|
|
GROUP BY name;
|
|
|
|
`;
|
2022-07-08 07:03:53 +00:00
|
|
|
const [weight] = await db.executeSql(bestWeight, [`%${search}%`]);
|
2022-07-08 12:11:10 +00:00
|
|
|
if (!weight) return setBests([]);
|
|
|
|
let newBest: Best[] = [];
|
2022-07-08 07:03:53 +00:00
|
|
|
for (let i = 0; i < weight.rows.length; i++) {
|
|
|
|
const [reps] = await db.executeSql(bestReps, [
|
|
|
|
weight.rows.item(i).name,
|
|
|
|
weight.rows.item(i).weight,
|
|
|
|
]);
|
2022-07-08 12:11:10 +00:00
|
|
|
newBest = newBest.concat(reps.rows.raw());
|
2022-07-08 07:03:53 +00:00
|
|
|
}
|
2022-07-08 12:11:10 +00:00
|
|
|
setBests(newBest);
|
2022-07-09 01:48:45 +00:00
|
|
|
}, [search, db]);
|
2022-07-04 04:03:48 +00:00
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
refresh();
|
2022-07-09 01:48:45 +00:00
|
|
|
}, [search, refresh]);
|
2022-07-04 04:03:48 +00:00
|
|
|
|
2022-07-08 12:11:10 +00:00
|
|
|
const renderItem = ({item}: {item: Best}) => (
|
2022-07-04 04:03:48 +00:00
|
|
|
<List.Item
|
|
|
|
key={item.name}
|
|
|
|
title={item.name}
|
2022-07-08 07:03:53 +00:00
|
|
|
description={`${item.reps} x ${item.weight}${item.unit}`}
|
2022-07-08 12:11:10 +00:00
|
|
|
onPress={() => setBest(item)}
|
2022-07-04 04:03:48 +00:00
|
|
|
/>
|
|
|
|
);
|
|
|
|
|
2022-07-03 01:50:01 +00:00
|
|
|
return (
|
2022-07-04 04:03:48 +00:00
|
|
|
<View style={styles.container}>
|
2022-07-04 04:17:29 +00:00
|
|
|
<Searchbar placeholder="Search" value={search} onChangeText={setSearch} />
|
2022-07-06 04:27:36 +00:00
|
|
|
<FlatList
|
2022-07-07 05:19:31 +00:00
|
|
|
ListEmptyComponent={
|
|
|
|
<List.Item
|
|
|
|
title="No exercises yet"
|
|
|
|
description="Once sets have been added, Exercises list your personal bests."
|
|
|
|
/>
|
|
|
|
}
|
2022-07-06 04:27:36 +00:00
|
|
|
refreshing={refreshing}
|
|
|
|
onRefresh={async () => {
|
|
|
|
setRefresing(true);
|
|
|
|
await refresh();
|
|
|
|
setRefresing(false);
|
|
|
|
}}
|
|
|
|
renderItem={renderItem}
|
2022-07-08 12:11:10 +00:00
|
|
|
data={bests}
|
2022-07-06 04:27:36 +00:00
|
|
|
/>
|
2022-07-08 12:11:10 +00:00
|
|
|
|
|
|
|
<ViewBest setBest={setBest} best={best} />
|
2022-07-03 01:50:01 +00:00
|
|
|
</View>
|
|
|
|
);
|
|
|
|
}
|
2022-07-04 04:03:48 +00:00
|
|
|
|
|
|
|
const styles = StyleSheet.create({
|
|
|
|
container: {
|
|
|
|
padding: 10,
|
2022-07-07 05:19:31 +00:00
|
|
|
flexGrow: 1,
|
2022-07-08 01:51:00 +00:00
|
|
|
paddingBottom: '10%',
|
2022-07-04 04:03:48 +00:00
|
|
|
},
|
|
|
|
});
|