2022-08-26 03:10:28 +00:00
|
|
|
import {
|
|
|
|
NavigationProp,
|
|
|
|
useFocusEffect,
|
|
|
|
useNavigation,
|
|
|
|
} from '@react-navigation/native';
|
2022-10-28 05:59:54 +00:00
|
|
|
import {useCallback, useState} from 'react';
|
2022-09-16 09:07:02 +00:00
|
|
|
import {FlatList} from 'react-native';
|
|
|
|
import {List} from 'react-native-paper';
|
2022-10-23 06:13:58 +00:00
|
|
|
import DrawerHeader from './DrawerHeader';
|
2022-09-16 09:07:02 +00:00
|
|
|
import Page from './Page';
|
2022-09-24 02:48:52 +00:00
|
|
|
import Set from './set';
|
2022-09-18 06:08:09 +00:00
|
|
|
import {getDistinctSets} from './set.service';
|
2022-08-26 03:10:28 +00:00
|
|
|
import SetList from './SetList';
|
|
|
|
import WorkoutItem from './WorkoutItem';
|
|
|
|
import {WorkoutsPageParams} from './WorkoutsPage';
|
|
|
|
|
|
|
|
const limit = 15;
|
|
|
|
|
|
|
|
export default function WorkoutList() {
|
2022-09-24 02:48:52 +00:00
|
|
|
const [workouts, setWorkouts] = useState<Set[]>();
|
2022-08-26 03:10:28 +00:00
|
|
|
const [offset, setOffset] = useState(0);
|
2022-10-28 05:59:54 +00:00
|
|
|
const [term, setTerm] = useState('');
|
2022-08-26 03:10:28 +00:00
|
|
|
const [end, setEnd] = useState(false);
|
|
|
|
const navigation = useNavigation<NavigationProp<WorkoutsPageParams>>();
|
|
|
|
|
2022-10-28 05:59:54 +00:00
|
|
|
const refresh = useCallback(async (value: string) => {
|
2022-09-18 06:08:09 +00:00
|
|
|
const newWorkouts = await getDistinctSets({
|
2022-10-28 05:59:54 +00:00
|
|
|
term: `%${value}%`,
|
2022-09-04 03:28:21 +00:00
|
|
|
limit,
|
|
|
|
offset: 0,
|
|
|
|
});
|
2022-09-24 05:15:44 +00:00
|
|
|
console.log(`${WorkoutList.name}`, {newWorkout: newWorkouts[0]});
|
2022-09-04 03:28:21 +00:00
|
|
|
setWorkouts(newWorkouts);
|
2022-08-26 03:10:28 +00:00
|
|
|
setOffset(0);
|
|
|
|
setEnd(false);
|
2022-10-28 05:59:54 +00:00
|
|
|
}, []);
|
2022-08-26 03:10:28 +00:00
|
|
|
|
|
|
|
useFocusEffect(
|
|
|
|
useCallback(() => {
|
2022-10-28 05:59:54 +00:00
|
|
|
refresh(term);
|
|
|
|
}, [refresh, term]),
|
2022-08-26 03:10:28 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
const renderItem = useCallback(
|
2022-09-24 02:48:52 +00:00
|
|
|
({item}: {item: Set}) => (
|
2022-10-28 05:59:54 +00:00
|
|
|
<WorkoutItem
|
|
|
|
item={item}
|
|
|
|
key={item.name}
|
|
|
|
onRemoved={() => refresh(term)}
|
|
|
|
/>
|
2022-08-26 03:10:28 +00:00
|
|
|
),
|
2022-10-28 05:59:54 +00:00
|
|
|
[refresh, term],
|
2022-08-26 03:10:28 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
const next = useCallback(async () => {
|
|
|
|
if (end) return;
|
|
|
|
const newOffset = offset + limit;
|
|
|
|
console.log(`${SetList.name}.next:`, {
|
|
|
|
offset,
|
|
|
|
limit,
|
|
|
|
newOffset,
|
2022-10-28 05:59:54 +00:00
|
|
|
term,
|
2022-08-26 03:10:28 +00:00
|
|
|
});
|
2022-09-18 06:08:09 +00:00
|
|
|
const newWorkouts = await getDistinctSets({
|
2022-10-28 05:59:54 +00:00
|
|
|
term: `%${term}%`,
|
2022-09-04 03:28:21 +00:00
|
|
|
limit,
|
|
|
|
offset: newOffset,
|
2022-09-04 04:30:02 +00:00
|
|
|
});
|
2022-09-04 03:28:21 +00:00
|
|
|
if (newWorkouts.length === 0) return setEnd(true);
|
2022-08-26 03:10:28 +00:00
|
|
|
if (!workouts) return;
|
2022-09-04 03:28:21 +00:00
|
|
|
setWorkouts([...workouts, ...newWorkouts]);
|
|
|
|
if (newWorkouts.length < limit) return setEnd(true);
|
2022-08-26 03:10:28 +00:00
|
|
|
setOffset(newOffset);
|
2022-10-28 05:59:54 +00:00
|
|
|
}, [term, end, offset, workouts]);
|
2022-08-26 03:10:28 +00:00
|
|
|
|
|
|
|
const onAdd = useCallback(async () => {
|
|
|
|
navigation.navigate('EditWorkout', {
|
2022-09-24 02:48:52 +00:00
|
|
|
value: {name: '', sets: 3, image: '', steps: '', reps: 0, weight: 0},
|
2022-08-26 03:10:28 +00:00
|
|
|
});
|
|
|
|
}, [navigation]);
|
|
|
|
|
2022-10-28 05:59:54 +00:00
|
|
|
const search = useCallback(
|
|
|
|
(value: string) => {
|
|
|
|
setTerm(value);
|
|
|
|
refresh(value);
|
|
|
|
},
|
|
|
|
[refresh],
|
|
|
|
);
|
|
|
|
|
2022-08-26 03:10:28 +00:00
|
|
|
return (
|
2022-10-22 23:35:58 +00:00
|
|
|
<>
|
2022-10-23 06:13:58 +00:00
|
|
|
<DrawerHeader name="Workouts" />
|
2022-10-28 05:59:54 +00:00
|
|
|
<Page onAdd={onAdd} term={term} search={search}>
|
2022-10-22 23:35:58 +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}
|
|
|
|
/>
|
|
|
|
)}
|
|
|
|
</Page>
|
|
|
|
</>
|
2022-08-26 03:10:28 +00:00
|
|
|
);
|
|
|
|
}
|