diff --git a/apps/backend/src/schema/schema.ts b/apps/backend/src/schema/schema.ts index c8cdb06..8708bd6 100644 --- a/apps/backend/src/schema/schema.ts +++ b/apps/backend/src/schema/schema.ts @@ -72,6 +72,8 @@ const priceSightingRef = builder.prismaObject('PriceSighting', { }) type SearchResultItem = { + id: string + cityId: string | null type: 'venue' | 'city' name: string lat: number @@ -85,6 +87,8 @@ const searchResultRef = builder .objectRef('SearchResult') .implement({ fields: (t) => ({ + id: t.exposeString('id'), + cityId: t.exposeString('cityId', { nullable: true }), type: t.exposeString('type'), name: t.exposeString('name'), lat: t.exposeFloat('lat'), @@ -194,6 +198,8 @@ builder.queryType({ return [ ...venues.map((venue) => ({ + id: venue.id, + cityId: venue.cityId, type: 'venue' as const, name: venue.name, lat: venue.lat, @@ -203,6 +209,8 @@ builder.queryType({ latestPriceSighting: latestUnlimitedSighting(venue.pricesSighted), })), ...cities.map((city) => ({ + id: city.id, + cityId: null, type: 'city' as const, name: city.name, lat: city.centreLat, diff --git a/apps/frontend/src/components/AddReviewDialog.tsx b/apps/frontend/src/components/AddReviewDialog.tsx new file mode 100644 index 0000000..9d7cf0d --- /dev/null +++ b/apps/frontend/src/components/AddReviewDialog.tsx @@ -0,0 +1,259 @@ +import type { FormEvent } from 'react' +import { useId, useRef, useState } from 'react' +import { useMutation, useQueryClient } from '@tanstack/react-query' + +import { graphqlFetch } from '#/graphql/client' +import { CREATE_REVIEW } from '#/graphql/venues' +import type { VenueReview } from '#/types/venue' + +const USERNAME_STORAGE_KEY = 'g-spotter:review-username' +const BODY_MAX_LENGTH = 1000 +const RATING_VALUES = [1, 2, 3, 4, 5] + +interface ReviewInput { + venueId: string + username: string + body: string + rating: number +} + +export default function AddReviewDialog({ + venueId, + venueName, +}: { + venueId: string + venueName: string +}) { + const dialogRef = useRef(null) + const nameInputRef = useRef(null) + const titleId = useId() + const descriptionId = useId() + const nameId = useId() + const bodyId = useId() + const errorId = useId() + + const [username, setUsername] = useState('') + const [rating, setRating] = useState(0) + const [hoveredRating, setHoveredRating] = useState(0) + const [body, setBody] = useState('') + const [error, setError] = useState(null) + + const queryClient = useQueryClient() + + const { mutate, isPending } = useMutation({ + mutationFn: (input: ReviewInput) => + graphqlFetch<{ createReview: VenueReview }>(CREATE_REVIEW, { input }), + onSuccess: () => { + setRating(0) + setHoveredRating(0) + setBody('') + setError(null) + dialogRef.current?.close() + void queryClient.invalidateQueries({ queryKey: ['venues'] }) + }, + onError: () => { + setError('Could not post your review. Please try again.') + }, + }) + + const visibleRating = hoveredRating || rating + + function openDialog() { + const savedName = window.localStorage.getItem(USERNAME_STORAGE_KEY) + if (savedName) { + setUsername(savedName) + } + setError(null) + dialogRef.current?.showModal() + } + + function closeDialog() { + dialogRef.current?.close() + } + + function handleSubmit(event: FormEvent) { + event.preventDefault() + + if (isPending) return + + const trimmedName = username.trim() + const trimmedBody = body.trim() + + if (!trimmedName) { + setError('Please enter your name.') + nameInputRef.current?.focus() + return + } + + if (rating < 1) { + setError('Please choose a rating.') + return + } + + if (!trimmedBody) { + setError('Please write a short review.') + return + } + + setError(null) + window.localStorage.setItem(USERNAME_STORAGE_KEY, trimmedName) + mutate({ + venueId, + username: trimmedName, + body: trimmedBody, + rating, + }) + } + + return ( + <> + + + { + if (event.target === dialogRef.current) { + closeDialog() + } + }} + className="m-auto max-h-[calc(100dvh_-_2rem)] w-[min(30rem,calc(100vw_-_2rem))] overflow-y-auto rounded-2xl border border-(--line) bg-(--surface-strong) p-0 text-(--sea-ink) shadow-2xl backdrop:bg-black/50 backdrop:backdrop-blur-sm" + > +
+

+ Add a review +

+

+ Share your thoughts about {venueName}. +

+ +
+
+ + setUsername(event.target.value)} + maxLength={50} + required + autoComplete="nickname" + placeholder="e.g. Kari" + className="w-full rounded-xl border border-(--line) bg-(--chip-bg) px-3 py-2 text-sm text-(--sea-ink) outline-none placeholder:text-(--sea-ink-soft) focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-(--lagoon-deep)" + /> +
+ +
+ Rating +
setHoveredRating(0)} + > + {RATING_VALUES.map((value) => ( + + ))} + + {rating > 0 ? `${rating} / 5` : 'Select a rating'} + +
+
+ +
+
+ + + {body.length}/{BODY_MAX_LENGTH} + +
+