2022-07-11 01:00:17 +00:00
|
|
|
import {
|
|
|
|
NavigationProp,
|
|
|
|
useFocusEffect,
|
|
|
|
useNavigation,
|
2023-08-12 03:03:31 +00:00
|
|
|
} from "@react-navigation/native";
|
|
|
|
import { useCallback, useState } from "react";
|
|
|
|
import { FlatList, Image } from "react-native";
|
|
|
|
import { List } from "react-native-paper";
|
2023-08-13 22:42:15 +00:00
|
|
|
import { getBestSets } from "./best.service";
|
|
|
|
import { LIMIT } from "./constants";
|
|
|
|
import { settingsRepo } from "./db";
|
2023-08-12 03:03:31 +00:00
|
|
|
import DrawerHeader from "./DrawerHeader";
|
2023-08-13 22:42:15 +00:00
|
|
|
import { GraphsPageParams } from "./GraphsPage";
|
2023-08-12 03:03:31 +00:00
|
|
|
import GymSet from "./gym-set";
|
|
|
|
import Page from "./Page";
|
|
|
|
import Settings from "./settings";
|
2022-07-11 01:00:17 +00:00
|
|
|
|
2023-07-03 23:35:52 +00:00
|
|
|
export default function GraphsList() {
|
2023-08-13 22:42:15 +00:00
|
|
|
const [bests, setBests] = useState<GymSet[]>([]);
|
|
|
|
const [offset, setOffset] = useState(0);
|
|
|
|
const [end, setEnd] = useState(false);
|
2023-08-12 03:03:31 +00:00
|
|
|
const [term, setTerm] = useState("");
|
|
|
|
const navigation = useNavigation<NavigationProp<GraphsPageParams>>();
|
|
|
|
const [settings, setSettings] = useState<Settings>();
|
2022-11-01 03:50:03 +00:00
|
|
|
|
|
|
|
useFocusEffect(
|
|
|
|
useCallback(() => {
|
2023-08-12 03:03:31 +00:00
|
|
|
settingsRepo.findOne({ where: {} }).then(setSettings);
|
|
|
|
}, [])
|
|
|
|
);
|
2022-07-11 01:00:17 +00:00
|
|
|
|
2022-10-28 05:59:54 +00:00
|
|
|
const refresh = useCallback(async (value: string) => {
|
2023-08-13 22:42:15 +00:00
|
|
|
const result = await getBestSets({ term: value, offset: 0 });
|
2023-08-12 03:03:31 +00:00
|
|
|
setBests(result);
|
2023-08-13 22:42:15 +00:00
|
|
|
setOffset(0);
|
2023-08-12 03:03:31 +00:00
|
|
|
}, []);
|
2022-07-11 01:00:17 +00:00
|
|
|
|
|
|
|
useFocusEffect(
|
|
|
|
useCallback(() => {
|
2023-08-12 03:03:31 +00:00
|
|
|
refresh(term);
|
|
|
|
}, [refresh, term])
|
|
|
|
);
|
2022-07-11 01:00:17 +00:00
|
|
|
|
2023-08-13 22:42:15 +00:00
|
|
|
const next = useCallback(async () => {
|
|
|
|
if (end) return;
|
|
|
|
const newOffset = offset + LIMIT;
|
|
|
|
console.log(`${GraphsList.name}.next:`, { offset, newOffset, term });
|
|
|
|
const newBests = await getBestSets({ term, offset });
|
|
|
|
if (newBests.length === 0) return setEnd(true);
|
|
|
|
if (!bests) return;
|
|
|
|
setBests([...bests, ...newBests]);
|
|
|
|
if (newBests.length < LIMIT) return setEnd(true);
|
|
|
|
setOffset(newOffset);
|
|
|
|
}, [term, end, offset, bests]);
|
|
|
|
|
2022-10-28 05:59:54 +00:00
|
|
|
const search = useCallback(
|
|
|
|
(value: string) => {
|
2023-08-12 03:03:31 +00:00
|
|
|
setTerm(value);
|
|
|
|
refresh(value);
|
2022-10-28 05:59:54 +00:00
|
|
|
},
|
2023-08-12 03:03:31 +00:00
|
|
|
[refresh]
|
|
|
|
);
|
2022-07-11 01:00:17 +00:00
|
|
|
|
2023-06-27 03:16:59 +00:00
|
|
|
const renderItem = ({ item }: { item: GymSet }) => (
|
2022-07-11 01:00:17 +00:00
|
|
|
<List.Item
|
|
|
|
key={item.name}
|
|
|
|
title={item.name}
|
2023-08-12 03:03:31 +00:00
|
|
|
description={`${item.reps} x ${item.weight}${item.unit || "kg"}`}
|
|
|
|
onPress={() => navigation.navigate("ViewGraph", { best: item })}
|
2022-09-18 06:30:37 +00:00
|
|
|
left={() =>
|
2022-09-21 01:48:45 +00:00
|
|
|
(settings.images && item.image && (
|
2023-06-27 03:16:59 +00:00
|
|
|
<Image
|
|
|
|
source={{ uri: item.image }}
|
|
|
|
style={{ height: 75, width: 75 }}
|
|
|
|
/>
|
2022-09-18 06:30:37 +00:00
|
|
|
)) ||
|
2023-08-12 03:03:31 +00:00
|
|
|
null
|
|
|
|
}
|
2022-07-11 01:00:17 +00:00
|
|
|
/>
|
2023-08-12 03:03:31 +00:00
|
|
|
);
|
2022-07-11 01:00:17 +00:00
|
|
|
|
|
|
|
return (
|
2022-10-22 23:35:58 +00:00
|
|
|
<>
|
2023-08-12 03:03:31 +00:00
|
|
|
<DrawerHeader name="Graphs" />
|
2022-10-28 05:59:54 +00:00
|
|
|
<Page term={term} search={search}>
|
2023-08-12 03:03:31 +00:00
|
|
|
{bests?.length === 0 ? (
|
|
|
|
<List.Item
|
|
|
|
title="No exercises yet"
|
|
|
|
description="Once sets have been added, this will highlight your personal bests."
|
|
|
|
/>
|
|
|
|
) : (
|
2023-08-13 22:42:15 +00:00
|
|
|
<FlatList
|
|
|
|
style={{ flex: 1 }}
|
|
|
|
renderItem={renderItem}
|
|
|
|
data={bests}
|
|
|
|
onEndReached={next}
|
|
|
|
/>
|
2023-08-12 03:03:31 +00:00
|
|
|
)}
|
2022-10-22 23:35:58 +00:00
|
|
|
</Page>
|
|
|
|
</>
|
2023-08-12 03:03:31 +00:00
|
|
|
);
|
2022-07-11 01:00:17 +00:00
|
|
|
}
|