2022-10-12 22:25:23 +00:00
|
|
|
import {
|
|
|
|
NavigationProp,
|
|
|
|
useFocusEffect,
|
|
|
|
useNavigation,
|
|
|
|
} from '@react-navigation/native';
|
2022-10-12 01:07:48 +00:00
|
|
|
import React, {useCallback, useMemo, useState} from 'react';
|
|
|
|
import {GestureResponderEvent, Text} from 'react-native';
|
2022-10-12 01:53:05 +00:00
|
|
|
import {Divider, List, Menu} from 'react-native-paper';
|
2022-10-12 01:07:48 +00:00
|
|
|
import {getBestSet} from './best.service';
|
2022-07-06 05:40:53 +00:00
|
|
|
import {Plan} from './plan';
|
2022-09-24 07:09:22 +00:00
|
|
|
import {PlanPageParams} from './plan-page-params';
|
2022-09-04 04:56:46 +00:00
|
|
|
import {deletePlan} from './plan.service';
|
2022-10-12 01:07:48 +00:00
|
|
|
import {DAYS} from './time';
|
2022-07-06 05:40:53 +00:00
|
|
|
|
|
|
|
export default function PlanItem({
|
|
|
|
item,
|
|
|
|
onRemove,
|
|
|
|
}: {
|
|
|
|
item: Plan;
|
|
|
|
onRemove: () => void;
|
|
|
|
}) {
|
|
|
|
const [show, setShow] = useState(false);
|
2022-07-08 02:59:19 +00:00
|
|
|
const [anchor, setAnchor] = useState({x: 0, y: 0});
|
2022-10-12 22:25:23 +00:00
|
|
|
const [today, setToday] = useState<string>();
|
2022-10-12 01:07:48 +00:00
|
|
|
const days = useMemo(() => item.days.split(','), [item.days]);
|
2022-07-11 00:28:30 +00:00
|
|
|
const navigation = useNavigation<NavigationProp<PlanPageParams>>();
|
2022-10-12 22:25:23 +00:00
|
|
|
|
|
|
|
useFocusEffect(
|
|
|
|
useCallback(() => {
|
|
|
|
const newToday = DAYS[new Date().getDay()];
|
|
|
|
setToday(newToday);
|
|
|
|
}, []),
|
|
|
|
);
|
2022-07-06 05:40:53 +00:00
|
|
|
|
2022-07-09 23:51:52 +00:00
|
|
|
const remove = useCallback(async () => {
|
2022-09-20 23:50:20 +00:00
|
|
|
if (typeof item.id === 'number') await deletePlan(item.id);
|
2022-07-06 05:40:53 +00:00
|
|
|
setShow(false);
|
|
|
|
onRemove();
|
2022-09-04 02:05:25 +00:00
|
|
|
}, [setShow, item.id, onRemove]);
|
2022-07-06 05:40:53 +00:00
|
|
|
|
2022-10-12 01:07:48 +00:00
|
|
|
const start = useCallback(async () => {
|
|
|
|
const workouts = item.workouts.split(',');
|
|
|
|
const first = workouts[0];
|
|
|
|
const set = await getBestSet(first);
|
|
|
|
setShow(false);
|
|
|
|
navigation.navigate('StartPlan', {plan: item, set});
|
|
|
|
}, [item, navigation]);
|
|
|
|
|
2022-07-09 23:51:52 +00:00
|
|
|
const longPress = useCallback(
|
|
|
|
(e: GestureResponderEvent) => {
|
|
|
|
setAnchor({x: e.nativeEvent.pageX, y: e.nativeEvent.pageY});
|
|
|
|
setShow(true);
|
|
|
|
},
|
|
|
|
[setAnchor, setShow],
|
|
|
|
);
|
2022-07-08 02:59:19 +00:00
|
|
|
|
2022-10-12 01:07:48 +00:00
|
|
|
const edit = useCallback(() => {
|
|
|
|
setShow(false);
|
|
|
|
navigation.navigate('EditPlan', {plan: item});
|
|
|
|
}, [navigation, item]);
|
|
|
|
|
2022-10-12 22:25:23 +00:00
|
|
|
const title = useMemo(
|
|
|
|
() =>
|
|
|
|
days.map((day, index) => (
|
|
|
|
<Text key={day}>
|
|
|
|
{day === today ? (
|
|
|
|
<Text style={{fontWeight: 'bold', textDecorationLine: 'underline'}}>
|
|
|
|
{day}
|
|
|
|
</Text>
|
|
|
|
) : (
|
|
|
|
day
|
|
|
|
)}
|
|
|
|
{index === days.length - 1 ? '' : ', '}
|
|
|
|
</Text>
|
|
|
|
)),
|
|
|
|
[days, today],
|
|
|
|
);
|
|
|
|
|
|
|
|
const description = useMemo(
|
|
|
|
() => item.workouts.replace(/,/g, ', '),
|
|
|
|
[item.workouts],
|
|
|
|
);
|
|
|
|
|
2022-07-06 05:40:53 +00:00
|
|
|
return (
|
|
|
|
<>
|
|
|
|
<List.Item
|
2022-10-12 01:07:48 +00:00
|
|
|
onPress={start}
|
2022-10-12 22:25:23 +00:00
|
|
|
title={title}
|
|
|
|
description={description}
|
2022-07-08 02:59:19 +00:00
|
|
|
onLongPress={longPress}
|
2022-07-06 05:40:53 +00:00
|
|
|
right={() => (
|
2022-07-08 02:59:19 +00:00
|
|
|
<Menu anchor={anchor} visible={show} onDismiss={() => setShow(false)}>
|
2022-10-12 01:07:48 +00:00
|
|
|
<Menu.Item icon="edit" onPress={edit} title="Edit" />
|
2022-10-12 01:53:05 +00:00
|
|
|
<Divider />
|
2022-09-27 00:35:27 +00:00
|
|
|
<Menu.Item icon="delete" onPress={remove} title="Delete" />
|
2022-07-06 05:40:53 +00:00
|
|
|
</Menu>
|
|
|
|
)}
|
|
|
|
/>
|
|
|
|
</>
|
|
|
|
);
|
|
|
|
}
|