Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ Kork & Krone is a wine review site for students, inspired by Vivino. It is a gro

The app is in Norwegian, because its users are Norwegian students. The code and the documentation are in English, as is common in software projects.

Students can search, filter and sort a catalogue of wines. Each wine shows two ratings side by side: the community rating from the X-Wines dataset and the rating from other students. Students give their own verdict as 1 to 5 stars.
Students can search, filter and sort a catalogue of wines. Each wine shows one average of all ratings, from the X-Wines dataset and from other students, and how many gave each score. Students give their own verdict as 0.5 to 5 stars, in half stars.

- **Application:** [it2810-18.idi.ntnu.no/project2/](http://it2810-18.idi.ntnu.no/project2/). Requires the NTNU network or VPN.
- **GraphQL API:** `/project2/graphql` on the VM, `localhost:3001/graphql` locally.
Expand Down Expand Up @@ -53,7 +53,7 @@ Submission 3 should be the first complete prototype, with search, browsing, sort
| Search page with results | Done | Search with debounce, number of hits and "Last inn flere" at `/sok` |
| Filtering and sorting in the user interface | Done | Filter panel, active filters and sorting on `/sok`. Needs the new backend on the VM |
| Wine detail page | Done | Facts, food pairing, the community rating and student reviews at `/vin/:id`. Student reviews are added with the reviews feature |
| Student reviews (stored in the database) | Done | 1–5 stars and an optional comment, saved and shown at once on the wine page |
| Student reviews (stored in the database) | Done | 0.5–5 stars and an optional comment, saved and shown at once on the wine page. They count in the shared average |
| Larger dataset on the VM | Done | Slim version: 1 007 wines and 150 000 ratings |
| Continuous integration (CI) | Not yet started | |

Expand Down Expand Up @@ -106,7 +106,8 @@ This section describes what users can do. "Backend" means the API supports the f
| Pagination | Done | Done | Pages behind an opaque cursor that holds an offset. The user loads more results with a button, not with infinite scroll. Focus moves to the first new wine |
| Search state in the URL | Not needed | Done | `?q=&type=&land=&drue=&mat=&vurdering=&alkohol=&sort=`. Survives reloads. Typing replaces the history entry, so the back button leaves the search instead of stepping through every word |
| Wine detail page | Done (`wine(id)`) | Done | Name, winery, region, grapes, alcohol, body, acidity, vintages and food pairing in Norwegian. Grapes and food link to the search. |
| Student reviews | Done | Done | 1–5 stars and an optional comment, stored in PostgreSQL. The average and the list update at once without a reload. One review per student per wine |
| Student reviews | Done | Done | 0.5–5 stars in half stars and an optional comment, stored in PostgreSQL. Hover the left half of a star for half a star, or use the arrow keys. The page updates at once without a reload. One review per student per wine |
| Shared average and distribution | Done | Done | Student reviews count in the average together with X-Wines, also when sorting and filtering. The wine page shows how many gave each score |
| Wishlist ("Vil prøve") | Planned | Done, stored in the browser | Moves to the backend when users and reviews are in place |
| User | Done (Anonymous ID) | Done | An anonymous ID stored in browser until we have login (#29) |

Expand Down
1 change: 1 addition & 0 deletions frontend/e2e/wine.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ async function stubApi(page: Page) {
__typename: 'Wine',
id: wine.id,
studentRating: { __typename: 'StudentRating', average: null, count: 0 },
ratingDistribution: [],
reviews: [],
},
myReview: null,
Expand Down
21 changes: 12 additions & 9 deletions frontend/src/components/ui/Stars.module.css
Original file line number Diff line number Diff line change
Expand Up @@ -8,25 +8,28 @@
gap: 2px;
}

.filled,
.empty {
.star {
flex-shrink: 0;
stroke-width: 1.5;
stroke-linejoin: round;
}

.filled {
fill: var(--color-brand);
stroke: var(--color-brand);
.outline,
.fill {
stroke-width: 1.5;
stroke-linejoin: round;
}

.empty {
.outline {
fill: none;
stroke: var(--color-text-muted);
}

.fill {
fill: var(--color-brand);
stroke: var(--color-brand);
}

@media (prefers-color-scheme: dark) {
.filled {
.fill {
fill: var(--cork-300);
stroke: var(--cork-300);
}
Expand Down
42 changes: 42 additions & 0 deletions frontend/src/components/ui/Stars.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import { describe, expect, it } from 'vitest';
import { render, screen } from '@testing-library/react';
import { formatStars, starFill } from '../../lib/stars';
import { Stars } from './Stars';

const ALL_STARS = [1, 2, 3, 4, 5];

describe('starFill', () => {
it('fills whole stars, half a star, and leaves the rest empty', () => {
expect(ALL_STARS.map((star) => starFill(star, 3.5))).toEqual([
'full',
'full',
'full',
'half',
'empty',
]);
});

it('leaves every star empty without a rating', () => {
expect(ALL_STARS.map((star) => starFill(star, null))).toEqual(Array(5).fill('empty'));
});
});

describe('formatStars', () => {
it('shows a decimal only when it is needed', () => {
expect(formatStars(3.5)).toBe('3,5');
expect(formatStars(4)).toBe('4');
});
});

describe('Stars', () => {
it('rounds to the nearest half star and gives screen readers the value as text', () => {
render(<Stars value={4.12} />);
expect(screen.getByText('4 av 5 stjerner')).toBeInTheDocument();
});

it('draws half a star', () => {
const { container } = render(<Stars value={2.5} />);
expect(screen.getByText('2,5 av 5 stjerner')).toBeInTheDocument();
expect(container.querySelectorAll('clipPath')).toHaveLength(1);
});
});
35 changes: 27 additions & 8 deletions frontend/src/components/ui/Stars.tsx
Original file line number Diff line number Diff line change
@@ -1,41 +1,60 @@
import { useId } from 'react';
import { formatStars, starFill, type StarFill } from '../../lib/stars';
import styles from './Stars.module.css';

const STAR_PATH =
'M12 2.5l2.94 5.96 6.56.95-4.75 4.63 1.12 6.54L12 17.5l-5.87 3.08 1.12-6.54L2.5 9.41l6.56-.95z';

/** One star. Hidden from screen readers, so the text next to it is what they read. */
export function StarIcon({ filled, size = 20 }: { filled: boolean; size?: number }) {
export function StarIcon({ fill, size = 20 }: { fill: StarFill; size?: number }) {
// The id must be unique on the page and valid in url(#...), so special characters are removed.
const clipId = `half-star-${useId().replace(/[^a-zA-Z0-9_-]/g, '')}`;

return (
<svg
width={size}
height={size}
viewBox="0 0 24 24"
className={filled ? styles.filled : styles.empty}
className={styles.star}
aria-hidden="true"
focusable="false"
>
<path d={STAR_PATH} />
{fill === 'half' && (
<defs>
<clipPath id={clipId}>
<rect x="0" y="0" width="12" height="24" />
</clipPath>
</defs>
)}
<path d={STAR_PATH} className={styles.outline} />
{fill !== 'empty' && (
<path
d={STAR_PATH}
className={styles.fill}
clipPath={fill === 'half' ? `url(#${clipId})` : undefined}
/>
)}
</svg>
);
}

interface StarsProps {
/** 1–5. Rounded to whole stars. */
/** 0.5–5. Rounded to the nearest half star. */
value: number;
size?: number;
}

/** A rating as five stars, with the text «4 av 5 stjerner» for screen readers. */
/** A rating as five stars, with the text «3,5 av 5 stjerner» for screen readers. */
export function Stars({ value, size = 20 }: StarsProps) {
const filled = Math.min(5, Math.max(0, Math.round(value)));
const rounded = Math.min(5, Math.max(0, Math.round(value * 2) / 2));
return (
<span className={styles.stars}>
<span className={styles.icons} aria-hidden="true">
{[1, 2, 3, 4, 5].map((star) => (
<StarIcon key={star} filled={star <= filled} size={size} />
<StarIcon key={star} fill={starFill(star, rounded)} size={size} />
))}
</span>
<span className="sr-only">{filled} av 5 stjerner</span>
<span className="sr-only">{formatStars(rounded)} av 5 stjerner</span>
</span>
);
}
1 change: 1 addition & 0 deletions frontend/src/features/wine/WinePage.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ const noReviews = (): MockLink.MockedResponse => ({
__typename: 'Wine',
id: '100062',
studentRating: { __typename: 'StudentRating', average: null, count: 0 },
ratingDistribution: [],
reviews: [],
},
myReview: null,
Expand Down
13 changes: 12 additions & 1 deletion frontend/src/features/wine/api/reviewQueries.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,23 @@ export interface ApiReview {
updatedAt: string;
}

/** How many gave one score (0.5–5), from X-Wines and students together. */
export interface RatingCount {
rating: number;
count: number;
}

export interface WineReviewsData {
wine: {
id: string;
studentRating: { average: number | null; count: number };
ratingDistribution: RatingCount[];
reviews: ApiReview[];
} | null;
myReview: Pick<ApiReview, 'id' | 'rating' | 'comment'> | null;
}

/** The student part of the wine page, and the user's own review, in one request. */
/** The reviews part of the wine page, and the user's own review, in one request. */
export const WINE_REVIEWS_QUERY = gql`
query WineReviews($id: ID!, $userId: ID!) {
wine(id: $id) {
Expand All @@ -27,6 +34,10 @@ export const WINE_REVIEWS_QUERY = gql`
average
count
}
ratingDistribution {
rating
count
}
reviews(first: 10) {
id
rating
Expand Down
5 changes: 4 additions & 1 deletion frontend/src/features/wine/api/useWineReviews.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
SAVE_REVIEW_MUTATION,
WINE_REVIEWS_QUERY,
type ApiReview,
type RatingCount,
type WineReviewsData,
} from './reviewQueries';

Expand All @@ -17,6 +18,7 @@ export interface ReviewDraft {
export interface WineReviewsResult {
students: RatingSummary | null;
reviews: ApiReview[];
distribution: RatingCount[];
myReview: WineReviewsData['myReview'];
loading: boolean;
error: Error | undefined;
Expand All @@ -38,7 +40,7 @@ export function useWineReviews(wineId: string): WineReviewsResult {
// After a change: fetch this wine's reviews again, and remove «Nylig dømt» from the cache,
// so the landing page asks for the new list the next time it is shown.
const afterChange = {
refetchQueries: ['WineReviews'],
refetchQueries: ['WineReviews', 'Wine'],
awaitRefetchQueries: true,
};

Expand Down Expand Up @@ -79,6 +81,7 @@ export function useWineReviews(wineId: string): WineReviewsResult {
return {
students: data?.wine?.studentRating ?? null,
reviews: data?.wine?.reviews ?? [],
distribution: data?.wine?.ratingDistribution ?? [],
myReview: data?.myReview ?? null,
loading,
error,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
.distribution {
display: grid;
gap: var(--space-2);
}

.title {
font-size: var(--text-lg);
}

.note {
font-size: var(--text-sm);
color: var(--color-text-muted);
}

.rows {
display: grid;
gap: var(--space-1);
margin: 0;
padding: 0;
list-style: none;
}

.row {
display: grid;
grid-template-columns: 6rem 1fr 3rem;
align-items: center;
gap: var(--space-2);
}

.bar {
height: 0.75rem;
border-radius: var(--radius-pill);
background: var(--color-surface-muted);
overflow: hidden;
}

.fill {
display: block;
height: 100%;
background: var(--color-brand);
}

.count {
text-align: right;
font-variant-numeric: tabular-nums;
}

@media (prefers-color-scheme: dark) {
.fill {
background: var(--cork-300);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import { describe, expect, it } from 'vitest';
import { render, screen } from '@testing-library/react';
import { axe } from 'vitest-axe';
import { groupByStars } from '../../../lib/stars';
import { RatingDistribution } from './RatingDistribution';
describe('groupByStars', () => {
it('counts half stars with the star above, from five stars to one', () => {
expect(
groupByStars([
{ rating: 5, count: 7 },
{ rating: 4.5, count: 1 },
{ rating: 3.5, count: 6 },
{ rating: 0.5, count: 2 },
]),
).toEqual([
{ stars: 5, count: 8 },
{ stars: 4, count: 6 },
{ stars: 3, count: 0 },
{ stars: 2, count: 0 },
{ stars: 1, count: 2 },
]);
});
});

describe('RatingDistribution', () => {
it('shows every row with the number as text', () => {
render(
<RatingDistribution
distribution={[
{ rating: 5, count: 9 },
{ rating: 1, count: 1 },
]}
/>,
);
const rows = screen.getAllByRole('listitem');
expect(rows).toHaveLength(5);
expect(rows[0]).toHaveTextContent(/5 stjerner\s*9 vurderinger/);
expect(rows[4]).toHaveTextContent(/1 stjerne\s*1 vurdering/);
});

it('shows nothing when nobody has rated the wine', () => {
const { container } = render(<RatingDistribution distribution={[]} />);
expect(container).toBeEmptyDOMElement();
});

it('has no axe violations', async () => {
const { container } = render(<RatingDistribution distribution={[{ rating: 4, count: 3 }]} />);
expect(await axe(container)).toHaveNoViolations();
});
});
Loading