Exercise: Product Reviews

Build a product's reviews page from four components that talk through inputs and outputs. A star rating that shows or chooses a rating, a list of reviews, a form that sends a new review up, and the page that holds the data and puts them together.

Journeyman · Angular · Updated

Northwind's customers want to review the products. You'll build Chai's reviews page out of four components, each with one job, talking to each other through inputs and outputs:

ProductReviews       holds the reviews, works out the average
├── StarRating       the average, in stars
├── ReviewList       the reviews
│   └── StarRating   each review's stars
└── ReviewForm       writing a new review
    └── StarRating   clickable, to choose a rating

StarRating appears three times, in three different roles: that's the point of a reusable component.

Get the exercise code

Open this lesson's GitHub repository, github.com/ask-codey/exercise-product-reviews, also linked from the Exercise box beside the lesson. Clone it into your bootcamp folder. In Git Bash:

cd ~/bootcamp
git clone https://github.com/ask-codey/exercise-product-reviews.git

Git makes a folder, exercise-product-reviews, with the exercise code inside.

Open the folder in VS Code, and a terminal there:

npm install
npm test
Folder Component For you to finish
src/app/star-rating/ StarRating Inputs rating (required) and editable, output rated
src/app/review-list/ ReviewList Input reviews
src/app/review-form/ ReviewForm Output submitted, and the form
src/app/product-reviews/ ProductReviews The average, adding reviews, and the page
src/app/reviews.spec.ts Eleven tests for all four

Each component's requirements are in the comment at the top of its .ts file. src/app/review.ts holds the Review interface.

What to do

Finish the components in this order, running npm test as you go:

  1. StarRating: five star buttons, the filled ones with the class on; a rating with decimals rounds to the nearest star; clicking a star sends its number on rated, but only when editable.
  2. ReviewList: one <li class="review"> per review, with its own <app-star-rating>; No reviews yet when there are none.
  3. ReviewForm: a name, a comment, and an editable <app-star-rating> that sets the form's rating; Add review sends the review on submitted and empties the form, or shows an error when the name or rating is missing.
  4. ProductReviews: the average in stars, 2 reviews, average 4.0, the list, and the form, whose reviews are added to the list.

Then run the app with ng serve, and add a review of your own.

Hints

  • Inputs and outputs: input.required<number>(), input(false), output<number>(), and this.rated.emit(star).
  • Rounding: a computed with Math.round(this.rating()) gives the number of stars to fill.
  • The stars: @for (star of stars; track star) over a plain array [1, 2, 3, 4, 5], with [class.on]="star <= filled()" and [disabled]="!editable()".
  • The form's rating: <app-star-rating [rating]="rating()" [editable]="true" (rated)="rating.set($event)" />. The form owns the rating; the stars just show it and report clicks.
  • The page: [reviews]="reviews()" down to the list, (submitted)="add($event)" up from the form. The average is a computed, so it follows the list by itself.
  • Imports: each component lists in imports the components it uses, plus FormsModule for [(ngModel)] and DecimalPipe for number: '1.1-1'.

When you're done

All eleven tests pass. Add a 3-star review as Linus, and the page shows three reviews, 3 reviews, average 3.7, and still 4 stars on top, because 3.7 rounds to 4.

Submit your exercise

The exercise is how you finish this lesson. When your work is done, put it on GitHub in a repo of your own, then submit that repo's address here.

  1. On GitHub, click +, then New repository. Name it (for example exercise-product-reviews), choose Public, and click Create repository. Leave out the README: your folder already has one.

  2. In a terminal in your exercise folder (Git Bash on Windows), send your work to your new repo. Point your copy at your own repo, then commit and push:

    git remote set-url origin https://github.com/your-name/exercise-product-reviews.git
    git add .
    git commit -m "My solution"
    git push -u origin main
    
  3. Paste your repo's address, such as https://github.com/your-name/exercise-product-reviews, into Submit your exercise on this page. The site checks that the repo is public and holds your commits, and then the lesson is complete.

Changed something after you submitted? Push it, and submit again: your latest submission is the one that counts.

So what

Four small components, none knowing more than it needs: data flows down through inputs, events flow up through outputs, and the page that owns the data decides what happens. That's how every Angular app is put together, however big it grows.

Key terms

Related