|
| 1 | +import React from "react"; |
| 2 | +import { |
| 3 | + Card, |
| 4 | + CardContent, |
| 5 | + CardFooter, |
| 6 | + CardHeader, |
| 7 | + CardTitle, |
| 8 | +} from "@/components/ui/card"; |
| 9 | +import { cn } from "@/lib/utils"; |
| 10 | +import Typography from "@/components/ui/typography"; |
| 11 | +import { Separator } from "@/components/ui/separator"; |
| 12 | +import { Button } from "@/components/ui/button"; |
| 13 | +import { Badge } from "@/components/ui/badge"; |
| 14 | +import { ArrowRight, Calendar, MapPin, Star, User, Users } from "lucide-react"; |
| 15 | +import Link from "next/link"; |
| 16 | +import type { Ride } from "@/types/ride.types"; |
| 17 | +import { getStatusVariant, getStatusLabel } from "@/utils/myRidesUtils"; |
| 18 | + |
| 19 | +interface RideCardProps { |
| 20 | + ride: Ride; |
| 21 | + viewType: "passenger" | "driver"; |
| 22 | + buttonLabel?: string; |
| 23 | + className?: string; |
| 24 | +} |
| 25 | + |
| 26 | +const RideCard = ({ |
| 27 | + ride, |
| 28 | + viewType, |
| 29 | + buttonLabel = "View details", |
| 30 | + className, |
| 31 | +}: RideCardProps) => { |
| 32 | + const cardDateFormatter = new Intl.DateTimeFormat("en-GB", { |
| 33 | + dateStyle: "medium", |
| 34 | + timeStyle: "short", |
| 35 | + }); |
| 36 | + |
| 37 | + const statusVariant = getStatusVariant(ride.status); |
| 38 | + const isCompleted = ride.status === "completed"; |
| 39 | + const isCancelled = ride.status === "cancelled" || ride.status === "rejected"; |
| 40 | + |
| 41 | + return ( |
| 42 | + <Card |
| 43 | + className={cn( |
| 44 | + "shadow-sm hover:shadow-lg transition-all duration-300 border-l-4", |
| 45 | + isCompleted |
| 46 | + ? "border-l-green-500/50 hover:border-l-green-500" |
| 47 | + : isCancelled |
| 48 | + ? "border-l-red-500/50 hover:border-l-red-500" |
| 49 | + : "border-l-primary/20 hover:border-l-primary", |
| 50 | + className |
| 51 | + )} |
| 52 | + > |
| 53 | + <CardHeader className="pb-6"> |
| 54 | + <div className="flex items-start justify-between gap-3 flex-wrap"> |
| 55 | + <CardTitle className="flex items-center gap-3 text-xl"> |
| 56 | + <div className="p-2 bg-primary/10 rounded-full"> |
| 57 | + <MapPin className="w-4 h-4 text-primary" /> |
| 58 | + </div> |
| 59 | + <span className="flex items-center gap-2 flex-wrap"> |
| 60 | + <Typography className="font-bold">{ride.origin}</Typography> |
| 61 | + <ArrowRight className="w-4 h-4 text-muted-foreground flex-shrink-0" /> |
| 62 | + <Typography className="font-bold">{ride.destination}</Typography> |
| 63 | + </span> |
| 64 | + </CardTitle> |
| 65 | + <Badge variant={statusVariant} className="px-3 py-1 shrink-0"> |
| 66 | + {getStatusLabel(ride.status)} |
| 67 | + </Badge> |
| 68 | + </div> |
| 69 | + </CardHeader> |
| 70 | + |
| 71 | + <CardContent className="space-y-4 pb-4"> |
| 72 | + <div className="flex flex-col gap-3"> |
| 73 | + <div className="flex items-center gap-2 text-sm"> |
| 74 | + <Calendar className="w-4 h-4 text-primary flex-shrink-0" /> |
| 75 | + <Typography className="font-medium"> |
| 76 | + {cardDateFormatter.format(ride.departureDate)} |
| 77 | + </Typography> |
| 78 | + </div> |
| 79 | + {viewType === "passenger" && ride.driver && ( |
| 80 | + <div className="flex items-center gap-2 rounded-full"> |
| 81 | + <User className="w-3.5 h-3.5 text-primary" /> |
| 82 | + <Typography className="font-medium"> |
| 83 | + {ride.driver.name} |
| 84 | + </Typography> |
| 85 | + {ride.driver.rating && ( |
| 86 | + <div className="flex items-center gap-1 ml-1"> |
| 87 | + <Star className="w-3.5 h-3.5 fill-yellow-400 text-yellow-400" /> |
| 88 | + <Typography className="font-semibold text-xs"> |
| 89 | + {ride.driver.rating} |
| 90 | + </Typography> |
| 91 | + </div> |
| 92 | + )} |
| 93 | + </div> |
| 94 | + )} |
| 95 | + {viewType === "driver" && |
| 96 | + ride.passengers && |
| 97 | + ride.passengers.length > 0 && ( |
| 98 | + <div className="flex items-center gap-2 pt-2"> |
| 99 | + <Users className="w-4 h-4 text-primary flex-shrink-0" /> |
| 100 | + <Typography className="text-sm text-muted-foreground"> |
| 101 | + {ride.passengers.length} passenger |
| 102 | + {ride.passengers.length !== 1 ? "s" : ""} booked |
| 103 | + </Typography> |
| 104 | + </div> |
| 105 | + )} |
| 106 | + </div> |
| 107 | + </CardContent> |
| 108 | + |
| 109 | + <Separator /> |
| 110 | + |
| 111 | + <CardFooter className="flex justify-between items-center pt-4"> |
| 112 | + <div className="flex flex-col gap-1"> |
| 113 | + <Typography className="text-2xl font-bold text-primary"> |
| 114 | + €{ride.pricePerSeat?.toFixed(2)} |
| 115 | + </Typography> |
| 116 | + <Typography className="text-xs text-muted-foreground"> |
| 117 | + per seat |
| 118 | + </Typography> |
| 119 | + </div> |
| 120 | + <Link href={`/my-rides/${ride.id}`}> |
| 121 | + <Button size="default" className="font-semibold" tabIndex={-1}> |
| 122 | + {buttonLabel} |
| 123 | + </Button> |
| 124 | + </Link> |
| 125 | + </CardFooter> |
| 126 | + </Card> |
| 127 | + ); |
| 128 | +}; |
| 129 | + |
| 130 | +export default RideCard; |
0 commit comments