Massive/PlanItem.tsx

54 lines
1.5 KiB
TypeScript
Raw Normal View History

2022-07-11 00:28:30 +00:00
import {NavigationProp, useNavigation} from '@react-navigation/native';
import React, {useCallback, useState} from 'react';
2022-07-08 02:59:19 +00:00
import {GestureResponderEvent} from 'react-native';
import {List, Menu} from 'react-native-paper';
2022-07-06 05:40:53 +00:00
import {Plan} from './plan';
import {PlanPageParams} from './plan-page-params';
import {deletePlan} from './plan.service';
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-07-11 00:28:30 +00:00
const navigation = useNavigation<NavigationProp<PlanPageParams>>();
2022-07-06 05:40:53 +00:00
2022-07-09 23:51:52 +00:00
const remove = useCallback(async () => {
if (typeof item.id === 'number') await deletePlan(item.id);
2022-07-06 05:40:53 +00:00
setShow(false);
onRemove();
}, [setShow, item.id, onRemove]);
2022-07-06 05:40:53 +00:00
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-07-06 05:40:53 +00:00
return (
<>
<List.Item
2022-07-11 00:28:30 +00:00
onPress={() => navigation.navigate('EditPlan', {plan: item})}
2022-09-18 06:45:05 +00:00
title={
item.days
? item.days.replace(/,/g, ', ')
: item.workouts.replace(/,/g, ', ')
}
description={item.days ? item.workouts.replace(/,/g, ', ') : null}
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-09-27 00:35:27 +00:00
<Menu.Item icon="delete" onPress={remove} title="Delete" />
2022-07-06 05:40:53 +00:00
</Menu>
)}
/>
</>
);
}