Massive/PlanList.tsx

76 lines
1.9 KiB
TypeScript
Raw Normal View History

2022-07-11 00:28:30 +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} from 'react-native'
import {List} from 'react-native-paper'
import {Like} from 'typeorm'
import {planRepo} from './db'
import DrawerHeader from './DrawerHeader'
import Page from './Page'
import {Plan} from './plan'
import {PlanPageParams} from './plan-page-params'
import PlanItem from './PlanItem'
2022-07-11 00:28:30 +00:00
export default function PlanList() {
2022-10-31 04:22:08 +00:00
const [term, setTerm] = useState('')
const [plans, setPlans] = useState<Plan[]>()
const [ids, setIds] = useState<number[]>([])
2022-10-31 04:22:08 +00:00
const navigation = useNavigation<NavigationProp<PlanPageParams>>()
2022-07-11 00:28:30 +00:00
const refresh = useCallback(async (value: string) => {
planRepo
.find({
where: [{days: Like(`%${value}%`)}, {workouts: Like(`%${value}%`)}],
})
2022-10-31 04:22:08 +00:00
.then(setPlans)
}, [])
2022-07-11 00:28:30 +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 00:28:30 +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 00:28:30 +00:00
const renderItem = useCallback(
({item}: {item: Plan}) => (
<PlanItem ids={ids} setIds={setIds} item={item} key={item.id} />
2022-07-11 00:28:30 +00:00
),
[ids],
2022-10-31 04:22:08 +00:00
)
2022-07-11 00:28:30 +00:00
const onAdd = () =>
2022-10-31 04:22:08 +00:00
navigation.navigate('EditPlan', {plan: {days: '', workouts: ''}})
2022-07-11 00:28:30 +00:00
return (
<>
<DrawerHeader name="Plans" ids={ids} />
<Page onAdd={onAdd} term={term} search={search}>
{plans?.length === 0 ? (
<List.Item
title="No plans yet"
description="A plan is a list of workouts for certain days."
/>
) : (
<FlatList
style={{flex: 1}}
data={plans}
renderItem={renderItem}
keyExtractor={set => set.id?.toString() || ''}
/>
)}
</Page>
</>
2022-10-31 04:22:08 +00:00
)
2022-07-11 00:28:30 +00:00
}