TastingsPage.tsx
4,938 bytes
| 1 | import { useMemo, useState } from "react"; |
|---|---|
| 2 | import { useMutation, useQuery, useQueryClient } from "@tanstack/react-query"; |
| 3 | import { Edit3, MapPin, Plus, Search, Star, Trash2 } from "lucide-react"; |
| 4 | import { useQuickTaste } from "../components/QuickTaste"; |
| 5 | import { EmptyState, ErrorState, LoadingScreen, PageHeader } from "../components/Ui"; |
| 6 | import { api } from "../lib/api"; |
| 7 | import { formatDate } from "../lib/format"; |
| 8 | import type { TastingPage } from "../types"; |
| 9 | |
| 10 | export function TastingsPage() { |
| 11 | const { openTaste } = useQuickTaste(); |
| 12 | const queryClient = useQueryClient(); |
| 13 | const [search, setSearch] = useState(""); |
| 14 | const tastings = useQuery({ |
| 15 | queryKey: ["tastings", 0, 50], |
| 16 | queryFn: () => api<TastingPage>("/api/v1/tastings?page=0&size=50") |
| 17 | }); |
| 18 | const remove = useMutation({ |
| 19 | mutationFn: (id: string) => api<void>(`/api/v1/tastings/${id}`, { method: "DELETE" }), |
| 20 | onSuccess: () => queryClient.invalidateQueries() |
| 21 | }); |
| 22 | |
| 23 | const filtered = useMemo(() => tastings.data?.items.filter(item => { |
| 24 | const needle = search.trim().toLowerCase(); |
| 25 | if (!needle) return true; |
| 26 | return item.dish.name.toLowerCase().includes(needle) |
| 27 | || item.city.toLowerCase().includes(needle) |
| 28 | || item.dish.destinationName.toLowerCase().includes(needle) |
| 29 | || item.restaurantName?.toLowerCase().includes(needle); |
| 30 | }) ?? [], [search, tastings.data]); |
| 31 | |
| 32 | if (tastings.isLoading) return <LoadingScreen label="Opening your tasting journal" />; |
| 33 | if (tastings.isError) return <ErrorState message="Your tasting journal could not be loaded." />; |
| 34 | |
| 35 | return ( |
| 36 | <div className="page tastings-page"> |
| 37 | <PageHeader |
| 38 | eyebrow="Personal archive" |
| 39 | title="Your tasting journal" |
| 40 | copy="Restaurants change. This keeps the dish, place, and detail you want to remember." |
| 41 | action={<button className="button button-primary" onClick={() => openTaste()}><Plus size={18} /> Log a taste</button>} |
| 42 | /> |
| 43 | |
| 44 | <div className="journal-toolbar"> |
| 45 | <label className="search-field"> |
| 46 | <Search size={17} /> |
| 47 | <span className="sr-only">Search tasting journal</span> |
| 48 | <input value={search} onChange={event => setSearch(event.target.value)} placeholder="Dish, country, city, or place" /> |
| 49 | </label> |
| 50 | <span>{filtered.length} of {tastings.data?.totalItems ?? 0} entries</span> |
| 51 | </div> |
| 52 | |
| 53 | {filtered.length ? ( |
| 54 | <section className="journal-list" aria-label="Tastings"> |
| 55 | {filtered.map(tasting => ( |
| 56 | <article className="journal-entry" key={tasting.id}> |
| 57 | <div className="journal-image" style={{ backgroundColor: tasting.dish.destinationAccent }}> |
| 58 | {tasting.photoUrl |
| 59 | ? <img src={tasting.photoUrl} alt={`${tasting.dish.name} in ${tasting.city}`} /> |
| 60 | : <span>{tasting.dish.destinationCode}</span>} |
| 61 | </div> |
| 62 | <div className="journal-main"> |
| 63 | <div className="journal-title-row"> |
| 64 | <div> |
| 65 | <span className="eyebrow">{tasting.dish.destinationName} / {tasting.dish.categoryLabel}</span> |
| 66 | <h2>{tasting.dish.name}</h2> |
| 67 | </div> |
| 68 | <div className="journal-rating" aria-label={`${tasting.rating} out of 5`}> |
| 69 | <Star size={15} fill="currentColor" /> {tasting.rating} |
| 70 | </div> |
| 71 | </div> |
| 72 | <div className="journal-place"> |
| 73 | <MapPin size={14} /> |
| 74 | <span>{tasting.restaurantName ? `${tasting.restaurantName}, ` : ""}{tasting.city}</span> |
| 75 | <time dateTime={tasting.tastedOn}>{formatDate(tasting.tastedOn)}</time> |
| 76 | </div> |
| 77 | {tasting.note && <blockquote>{tasting.note}</blockquote>} |
| 78 | </div> |
| 79 | <div className="journal-actions"> |
| 80 | <button className="icon-button" type="button" onClick={() => openTaste({ tasting })} aria-label={`Edit ${tasting.dish.name}`}> |
| 81 | <Edit3 size={17} /> |
| 82 | </button> |
| 83 | <button className="icon-button icon-button-danger" type="button" disabled={remove.isPending} |
| 84 | onClick={() => { |
| 85 | if (window.confirm(`Delete your ${tasting.dish.name} entry?`)) remove.mutate(tasting.id); |
| 86 | }} aria-label={`Delete ${tasting.dish.name}`}> |
| 87 | <Trash2 size={17} /> |
| 88 | </button> |
| 89 | </div> |
| 90 | </article> |
| 91 | ))} |
| 92 | </section> |
| 93 | ) : ( |
| 94 | <EmptyState |
| 95 | title={search ? "No journal entries match" : "Your journal is ready"} |
| 96 | copy={search ? "Try a dish, city, country, or restaurant name." : "Log your first memorable dish to begin the map."} |
| 97 | action={!search && <button className="button button-primary" onClick={() => openTaste()}>Log first taste</button>} |
| 98 | /> |
| 99 | )} |
| 100 | </div> |
| 101 | ); |
| 102 | } |
| 103 | |