muni-logo

Task 06 - Forms, Async

This week, we'll look at forms and asynchronous operations, specifically getting and sending data to the API. You'll be given a project again, in which you'll have a freer hand this time. Create an application that will have a total of 4 pages. The routing is handled for you; focus on the page implementation.

We have prepared API endpoints and specifications for you. If you prefer to look directly at the code for these endpoints, you can find them in src/app/api.

Maximum points40 points
Deadline
March 31, 2025

Page Specifications

Page /

No modifications are needed on this page. On this page you will find buttons to navigate to the rest of the application.

Page /profile

This page will display content based on whether the user is logged in.

Page /list

This page displays all gifts from the database. Based on who is logged in, the UI will be slightly different.

Page /create

This page allows users with the user role to create a new gift.

Video

Click to see the video

Requirements

Tips

APIs

Here's a list of available APIs.

POST /api/login

Endpoint for getting a user by username and password. API sends back an instance of the user.

Example call:

const response = await fetch('/api/login', {
  method: 'POST',
  headers: {
    'Content-Type': 'application/json'
  },
  body: JSON.stringify({ username: 'santa', password: 'santa123' })
});

Response

200 OK

{
  "id": string,
  "name": string,
  "username": string,
  "password": string,
  "role": "santa" | "user"
}

401 Unauthorized

{
  "error": "Invalid username or password"
}

GET /api/gift/list

Returns a list of all gifts in the database.

Example call:

const response = fetch('/api/gift/list', {
  method: 'GET'
});

Response

200 OK

{
	"id": string,
	"name": string,
	"description": string | undefined,
	"price": number,
	"createdBy": string,
	"delivered": boolean
}

POST /gift

Endpoint to create new gift. Only user with role "user" are allowed to create new gift via this API! userId stands for the user's id, who is creating a gift. API sends back the created gift.

Example call:

const response = await fetch('/api/gift', {
  method: 'POST',
  headers: {
    'Content-Type': 'application/json'
  },
  body: JSON.stringify({
    gift: {
      name: 'New car',
      description: 'I really want a new car',
      price: 20
    },
    userId: '1'
  })
});

Response

201 Created

{
  "name": string,
  "description": string | undefined,
  "price": number,
  "id": string,
  "createdBy": string,
  "delivered": boolean
}

PATCH /api/gift/:id

Endpoint to update a gift's delivered status. Only user with role "santa" are allowed to update the gift's status via this API!

Example call:

const response = await fetch(`/api/gift/${id}`, {
  method: 'PATCH',
  headers: {
    'Content-Type': 'application/json'
  },
  body: JSON.stringify({ delivered: true })
});

Response

200 OK

{
  "name": string,
  "description": string | undefined,
  "price": number,
  "id": string,
  "createdBy": string,
  "delivered": boolean
}

404 Not found

{
  "error": "Gift does not exist."
}