2025-02-09 20:38:38 -05:00
|
|
|
|
// components/templates/TemplateCard.tsx
|
|
|
|
|
import React from 'react';
|
|
|
|
|
import { View, TouchableOpacity, Platform } from 'react-native';
|
|
|
|
|
import { Text } from '@/components/ui/text';
|
|
|
|
|
import { Card, CardContent } from '@/components/ui/card';
|
|
|
|
|
import { Button } from '@/components/ui/button';
|
|
|
|
|
import { Badge } from '@/components/ui/badge';
|
|
|
|
|
import { Trash2, Star, Play } from 'lucide-react-native';
|
|
|
|
|
import {
|
|
|
|
|
AlertDialog,
|
|
|
|
|
AlertDialogAction,
|
|
|
|
|
AlertDialogCancel,
|
|
|
|
|
AlertDialogContent,
|
|
|
|
|
AlertDialogDescription,
|
|
|
|
|
AlertDialogFooter,
|
|
|
|
|
AlertDialogHeader,
|
|
|
|
|
AlertDialogTitle,
|
|
|
|
|
AlertDialogTrigger,
|
|
|
|
|
} from '@/components/ui/alert-dialog';
|
2025-02-19 21:39:47 -05:00
|
|
|
|
import { Template, TemplateExerciseDisplay } from '@/types/templates';
|
2025-03-12 19:23:28 -04:00
|
|
|
|
import { useIconColor } from '@/lib/theme/iconUtils';
|
2025-02-09 20:38:38 -05:00
|
|
|
|
|
|
|
|
|
interface TemplateCardProps {
|
|
|
|
|
template: Template;
|
|
|
|
|
onPress: () => void;
|
|
|
|
|
onDelete: (id: string) => void;
|
|
|
|
|
onFavorite: () => void;
|
|
|
|
|
onStartWorkout: () => void;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function TemplateCard({
|
|
|
|
|
template,
|
|
|
|
|
onPress,
|
|
|
|
|
onDelete,
|
|
|
|
|
onFavorite,
|
|
|
|
|
onStartWorkout
|
|
|
|
|
}: TemplateCardProps) {
|
|
|
|
|
const [showDeleteAlert, setShowDeleteAlert] = React.useState(false);
|
2025-02-22 01:16:33 -05:00
|
|
|
|
const lastUsed = template.metadata?.lastUsed ? new Date(template.metadata.lastUsed) : undefined;
|
2025-03-12 19:23:28 -04:00
|
|
|
|
const { getIconProps, getIconColor } = useIconColor();
|
2025-02-09 20:38:38 -05:00
|
|
|
|
|
|
|
|
|
const {
|
|
|
|
|
id,
|
|
|
|
|
title,
|
|
|
|
|
type,
|
|
|
|
|
category,
|
|
|
|
|
exercises,
|
|
|
|
|
description,
|
|
|
|
|
tags = [],
|
|
|
|
|
source,
|
|
|
|
|
isFavorite
|
|
|
|
|
} = template;
|
|
|
|
|
|
|
|
|
|
const handleConfirmDelete = () => {
|
|
|
|
|
onDelete(id);
|
|
|
|
|
setShowDeleteAlert(false);
|
|
|
|
|
};
|
|
|
|
|
|
2025-03-06 09:19:16 -05:00
|
|
|
|
// Prevent event propagation when clicking on action buttons
|
|
|
|
|
const handleStartWorkout = (e: any) => {
|
|
|
|
|
if (e && e.stopPropagation) {
|
|
|
|
|
e.stopPropagation();
|
|
|
|
|
}
|
|
|
|
|
onStartWorkout();
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const handleFavorite = (e: any) => {
|
|
|
|
|
if (e && e.stopPropagation) {
|
|
|
|
|
e.stopPropagation();
|
|
|
|
|
}
|
|
|
|
|
onFavorite();
|
2025-02-26 23:30:00 -05:00
|
|
|
|
};
|
|
|
|
|
|
2025-02-09 20:38:38 -05:00
|
|
|
|
return (
|
2025-03-06 09:19:16 -05:00
|
|
|
|
<TouchableOpacity onPress={onPress} activeOpacity={0.7}>
|
2025-02-22 01:16:33 -05:00
|
|
|
|
<Card className="mx-4">
|
|
|
|
|
<CardContent className="p-4">
|
|
|
|
|
<View className="flex-row justify-between items-start">
|
|
|
|
|
<View className="flex-1">
|
|
|
|
|
<View className="flex-row items-center gap-2 mb-1">
|
|
|
|
|
<Text className="text-lg font-semibold text-card-foreground">
|
|
|
|
|
{title}
|
|
|
|
|
</Text>
|
|
|
|
|
<Badge
|
|
|
|
|
variant={source === 'local' ? 'outline' : 'secondary'}
|
|
|
|
|
className="text-xs capitalize"
|
|
|
|
|
>
|
|
|
|
|
<Text>{source}</Text>
|
|
|
|
|
</Badge>
|
|
|
|
|
</View>
|
|
|
|
|
|
|
|
|
|
<View className="flex-row gap-2">
|
|
|
|
|
<Badge variant="outline" className="text-xs capitalize">
|
|
|
|
|
<Text>{type}</Text>
|
|
|
|
|
</Badge>
|
|
|
|
|
<Text className="text-sm text-muted-foreground">
|
|
|
|
|
{category}
|
|
|
|
|
</Text>
|
|
|
|
|
</View>
|
2025-02-09 20:38:38 -05:00
|
|
|
|
|
2025-02-22 01:16:33 -05:00
|
|
|
|
{exercises.length > 0 && (
|
|
|
|
|
<View className="mt-2">
|
|
|
|
|
<Text className="text-sm text-muted-foreground mb-1">
|
|
|
|
|
Exercises:
|
2025-02-09 20:38:38 -05:00
|
|
|
|
</Text>
|
2025-02-22 01:16:33 -05:00
|
|
|
|
<View className="gap-1">
|
|
|
|
|
{exercises.slice(0, 3).map((exercise, index) => (
|
|
|
|
|
<Text key={index} className="text-sm text-muted-foreground">
|
|
|
|
|
• {exercise.title} ({exercise.targetSets}×{exercise.targetReps})
|
|
|
|
|
</Text>
|
2025-02-09 20:38:38 -05:00
|
|
|
|
))}
|
2025-02-22 01:16:33 -05:00
|
|
|
|
{exercises.length > 3 && (
|
|
|
|
|
<Text className="text-sm text-muted-foreground">
|
|
|
|
|
+{exercises.length - 3} more
|
|
|
|
|
</Text>
|
|
|
|
|
)}
|
2025-02-09 20:38:38 -05:00
|
|
|
|
</View>
|
2025-02-22 01:16:33 -05:00
|
|
|
|
</View>
|
|
|
|
|
)}
|
2025-02-09 20:38:38 -05:00
|
|
|
|
|
2025-02-22 01:16:33 -05:00
|
|
|
|
{description && (
|
|
|
|
|
<Text className="text-sm text-muted-foreground mt-2 native:pr-12">
|
|
|
|
|
{description}
|
|
|
|
|
</Text>
|
|
|
|
|
)}
|
2025-02-09 20:38:38 -05:00
|
|
|
|
|
2025-02-22 01:16:33 -05:00
|
|
|
|
{tags.length > 0 && (
|
|
|
|
|
<View className="flex-row flex-wrap gap-2 mt-2">
|
|
|
|
|
{tags.map(tag => (
|
|
|
|
|
<Badge key={tag} variant="outline" className="text-xs">
|
2025-02-09 20:38:38 -05:00
|
|
|
|
<Text>{tag}</Text>
|
|
|
|
|
</Badge>
|
|
|
|
|
))}
|
|
|
|
|
</View>
|
2025-02-22 01:16:33 -05:00
|
|
|
|
)}
|
|
|
|
|
|
|
|
|
|
{lastUsed && (
|
|
|
|
|
<Text className="text-xs text-muted-foreground mt-2">
|
|
|
|
|
Last used: {lastUsed.toLocaleDateString()}
|
|
|
|
|
</Text>
|
|
|
|
|
)}
|
|
|
|
|
</View>
|
|
|
|
|
|
|
|
|
|
<View className="flex-row gap-1 native:absolute native:right-0 native:top-0 native:p-2">
|
|
|
|
|
<Button
|
|
|
|
|
variant="ghost"
|
|
|
|
|
size="icon"
|
2025-03-06 09:19:16 -05:00
|
|
|
|
onPress={handleStartWorkout}
|
2025-02-22 01:16:33 -05:00
|
|
|
|
className="native:h-10 native:w-10"
|
|
|
|
|
accessibilityLabel="Start workout"
|
|
|
|
|
>
|
2025-03-12 19:23:28 -04:00
|
|
|
|
<Play
|
|
|
|
|
size={20}
|
|
|
|
|
{...getIconProps('primary')}
|
|
|
|
|
/>
|
2025-02-22 01:16:33 -05:00
|
|
|
|
</Button>
|
|
|
|
|
<Button
|
|
|
|
|
variant="ghost"
|
|
|
|
|
size="icon"
|
2025-03-06 09:19:16 -05:00
|
|
|
|
onPress={handleFavorite}
|
2025-02-22 01:16:33 -05:00
|
|
|
|
className="native:h-10 native:w-10"
|
|
|
|
|
accessibilityLabel={isFavorite ? "Remove from favorites" : "Add to favorites"}
|
|
|
|
|
>
|
|
|
|
|
<Star
|
2025-03-12 19:23:28 -04:00
|
|
|
|
size={20}
|
|
|
|
|
{...getIconProps(isFavorite ? 'primary' : 'muted')}
|
|
|
|
|
fill={isFavorite ? getIconColor('primary') : "none"}
|
2025-02-22 01:16:33 -05:00
|
|
|
|
/>
|
|
|
|
|
</Button>
|
|
|
|
|
<AlertDialog open={showDeleteAlert} onOpenChange={setShowDeleteAlert}>
|
|
|
|
|
<AlertDialogTrigger asChild>
|
|
|
|
|
<Button
|
|
|
|
|
variant="ghost"
|
|
|
|
|
size="icon"
|
|
|
|
|
className="native:h-10 native:w-10 native:bg-muted/50 items-center justify-center"
|
|
|
|
|
accessibilityLabel="Delete template"
|
|
|
|
|
>
|
|
|
|
|
<Trash2
|
2025-03-12 19:23:28 -04:00
|
|
|
|
size={20}
|
|
|
|
|
{...getIconProps('destructive')}
|
2025-02-22 01:16:33 -05:00
|
|
|
|
/>
|
|
|
|
|
</Button>
|
|
|
|
|
</AlertDialogTrigger>
|
|
|
|
|
<AlertDialogContent>
|
|
|
|
|
<AlertDialogHeader>
|
|
|
|
|
<AlertDialogTitle>
|
|
|
|
|
<Text>Delete Template</Text>
|
|
|
|
|
</AlertDialogTitle>
|
|
|
|
|
<AlertDialogDescription>
|
|
|
|
|
<Text>Are you sure you want to delete {title}? This action cannot be undone.</Text>
|
|
|
|
|
</AlertDialogDescription>
|
|
|
|
|
</AlertDialogHeader>
|
|
|
|
|
<AlertDialogFooter>
|
|
|
|
|
<AlertDialogCancel>
|
|
|
|
|
<Text>Cancel</Text>
|
|
|
|
|
</AlertDialogCancel>
|
|
|
|
|
<AlertDialogAction onPress={handleConfirmDelete}>
|
|
|
|
|
<Text>Delete</Text>
|
|
|
|
|
</AlertDialogAction>
|
|
|
|
|
</AlertDialogFooter>
|
|
|
|
|
</AlertDialogContent>
|
|
|
|
|
</AlertDialog>
|
|
|
|
|
</View>
|
2025-02-09 20:38:38 -05:00
|
|
|
|
</View>
|
2025-02-22 01:16:33 -05:00
|
|
|
|
</CardContent>
|
|
|
|
|
</Card>
|
|
|
|
|
</TouchableOpacity>
|
2025-02-09 20:38:38 -05:00
|
|
|
|
);
|
|
|
|
|
}
|