2022-08-26 03:10:28 +00:00
|
|
|
import {
|
|
|
|
NavigationProp,
|
|
|
|
useFocusEffect,
|
|
|
|
useNavigation,
|
2023-08-12 03:22:50 +00:00
|
|
|
} from "@react-navigation/native";
|
|
|
|
import { useCallback, useState } from "react";
|
|
|
|
import { FlatList } from "react-native";
|
|
|
|
import { List } from "react-native-paper";
|
|
|
|
import { setRepo, settingsRepo } from "./db";
|
|
|
|
import DrawerHeader from "./DrawerHeader";
|
|
|
|
import GymSet from "./gym-set";
|
|
|
|
import Page from "./Page";
|
|
|
|
import SetList from "./SetList";
|
|
|
|
import Settings from "./settings";
|
|
|
|
import WorkoutItem from "./WorkoutItem";
|
|
|
|
import { WorkoutsPageParams } from "./WorkoutsPage";
|
2022-08-26 03:10:28 +00:00
|
|
|
|
2023-08-12 03:22:50 +00:00
|
|
|
const limit = 15;
|
2022-08-26 03:10:28 +00:00
|
|
|
|
|
|
|
export default function WorkoutList() {
|
2023-08-12 03:22:50 +00:00
|
|
|
const [workouts, setWorkouts] = useState<GymSet[]>();
|
|
|
|
const [offset, setOffset] = useState(0);
|
|
|
|
const [term, setTerm] = useState("");
|
|
|
|
const [end, setEnd] = useState(false);
|
|
|
|
const [settings, setSettings] = useState<Settings>();
|
|
|
|
const navigation = useNavigation<NavigationProp<WorkoutsPageParams>>();
|
2022-08-26 03:10:28 +00:00
|
|
|
|
2022-10-28 05:59:54 +00:00
|
|
|
const refresh = useCallback(async (value: string) => {
|
2022-10-31 00:20:36 +00:00
|
|
|
const newWorkouts = await setRepo
|
|
|
|
.createQueryBuilder()
|
|
|
|
.select()
|
2023-08-12 03:22:50 +00:00
|
|
|
.where("name LIKE :name", { name: `%${value.trim()}%` })
|
|
|
|
.groupBy("name")
|
|
|
|
.orderBy("name")
|
2022-10-31 00:20:36 +00:00
|
|
|
.limit(limit)
|
2023-08-12 03:22:50 +00:00
|
|
|
.getMany();
|
|
|
|
console.log(`${WorkoutList.name}`, { newWorkout: newWorkouts[0] });
|
|
|
|
setWorkouts(newWorkouts);
|
|
|
|
setOffset(0);
|
|
|
|
setEnd(false);
|
|
|
|
}, []);
|
2022-08-26 03:10:28 +00:00
|
|
|
|
|
|
|
useFocusEffect(
|
|
|
|
useCallback(() => {
|
2023-08-12 03:22:50 +00:00
|
|
|
refresh(term);
|
|
|
|
settingsRepo.findOne({ where: {} }).then(setSettings);
|
|
|
|
}, [refresh, term])
|
|
|
|
);
|
2022-08-26 03:10:28 +00:00
|
|
|
|
|
|
|
const renderItem = useCallback(
|
2023-06-27 03:16:59 +00:00
|
|
|
({ item }: { item: GymSet }) => (
|
2022-11-01 03:50:03 +00:00
|
|
|
<WorkoutItem
|
|
|
|
images={settings?.images}
|
|
|
|
item={item}
|
|
|
|
key={item.name}
|
|
|
|
onRemove={() => refresh(term)}
|
|
|
|
/>
|
2022-08-26 03:10:28 +00:00
|
|
|
),
|
2023-08-12 03:22:50 +00:00
|
|
|
[refresh, term, settings?.images]
|
|
|
|
);
|
2022-08-26 03:10:28 +00:00
|
|
|
|
|
|
|
const next = useCallback(async () => {
|
2023-08-12 03:22:50 +00:00
|
|
|
if (end) return;
|
|
|
|
const newOffset = offset + limit;
|
2022-08-26 03:10:28 +00:00
|
|
|
console.log(`${SetList.name}.next:`, {
|
|
|
|
offset,
|
|
|
|
limit,
|
|
|
|
newOffset,
|
2022-10-28 05:59:54 +00:00
|
|
|
term,
|
2023-08-12 03:22:50 +00:00
|
|
|
});
|
2022-10-31 00:20:36 +00:00
|
|
|
const newWorkouts = await setRepo
|
|
|
|
.createQueryBuilder()
|
|
|
|
.select()
|
2023-08-12 03:22:50 +00:00
|
|
|
.where("name LIKE :name", { name: `%${term.trim()}%` })
|
|
|
|
.groupBy("name")
|
|
|
|
.orderBy("name")
|
2022-10-31 00:20:36 +00:00
|
|
|
.limit(limit)
|
|
|
|
.offset(newOffset)
|
2023-08-12 03:22:50 +00:00
|
|
|
.getMany();
|
|
|
|
if (newWorkouts.length === 0) return setEnd(true);
|
|
|
|
if (!workouts) return;
|
|
|
|
setWorkouts([...workouts, ...newWorkouts]);
|
|
|
|
if (newWorkouts.length < limit) return setEnd(true);
|
|
|
|
setOffset(newOffset);
|
|
|
|
}, [term, end, offset, workouts]);
|
2022-08-26 03:10:28 +00:00
|
|
|
|
|
|
|
const onAdd = useCallback(async () => {
|
2023-08-12 03:22:50 +00:00
|
|
|
navigation.navigate("EditWorkout", {
|
2022-10-31 00:20:36 +00:00
|
|
|
value: new GymSet(),
|
2023-08-12 03:22:50 +00:00
|
|
|
});
|
|
|
|
}, [navigation]);
|
2022-08-26 03:10:28 +00:00
|
|
|
|
2022-10-28 05:59:54 +00:00
|
|
|
const search = useCallback(
|
|
|
|
(value: string) => {
|
2023-08-12 03:22:50 +00:00
|
|
|
setTerm(value);
|
|
|
|
refresh(value);
|
2022-10-28 05:59:54 +00:00
|
|
|
},
|
2023-08-12 03:22:50 +00:00
|
|
|
[refresh]
|
|
|
|
);
|
2022-10-28 05:59:54 +00:00
|
|
|
|
2022-08-26 03:10:28 +00:00
|
|
|
return (
|
2022-10-22 23:35:58 +00:00
|
|
|
<>
|
2023-08-12 03:22:50 +00:00
|
|
|
<DrawerHeader name="Workouts" />
|
2022-10-28 05:59:54 +00:00
|
|
|
<Page onAdd={onAdd} term={term} search={search}>
|
2023-08-12 03:22:50 +00:00
|
|
|
{workouts?.length === 0 ? (
|
|
|
|
<List.Item
|
|
|
|
title="No workouts yet."
|
|
|
|
description="A workout is something you do at the gym. For example Deadlifts are a workout."
|
|
|
|
/>
|
|
|
|
) : (
|
|
|
|
<FlatList
|
|
|
|
data={workouts}
|
|
|
|
style={{ flex: 1 }}
|
|
|
|
renderItem={renderItem}
|
|
|
|
keyExtractor={(w) => w.name}
|
|
|
|
onEndReached={next}
|
|
|
|
/>
|
|
|
|
)}
|
2022-10-22 23:35:58 +00:00
|
|
|
</Page>
|
|
|
|
</>
|
2023-08-12 03:22:50 +00:00
|
|
|
);
|
2022-08-26 03:10:28 +00:00
|
|
|
}
|