hngprojects / hng_boilerplate_nestjs

Description
Apache License 2.0
181 stars 105 forks source link

[FEAT] COMPONENT THEME SWITCHER - TOGGLE DARK OR LIGHT MODE #177

Closed ChibuikemMichaelIlonze closed 1 month ago

ChibuikemMichaelIlonze commented 1 month ago

Description

Develop API endpoints that allow users to toggle between light and dark modes for the application. The user’s theme preference should be stored and retrieved from the existing users table.

Acceptance Criteria

Purpose

To provide users with the ability to switch between light and dark themes, enhancing user experience and accessibility.

Requirements

Expected Outcome

API endpoints that allow users to toggle between light and dark modes and store their preferences securely in the existing users table.

Tasks

Endpoint URL

[POST] /api/v1/user/theme

Request Body:

{
  "user_id": "string",
  "theme": "string" // "light" or "dark"
}

Successful Response:

Error Response:

[GET] /api/v1/user/theme

Successful Response:

Error Response:

Example Request

Fetch Theme Preference:

curl -X GET "https://<base_url>/api/v1/user/theme?user_id=12345"

Update Theme Preference:

curl -X POST "https://<base_url>/api/v1/user/theme" \
  -H "Content-Type: application/json" \
  -d '{
    "user_id": "12345",
    "theme": "dark"
  }'

Testing

Test Scenarios

  1. Successful Toggle

    • Ensure that the toggle switch updates the integration state correctly.
    • Confirm that the status code is 200.
    • Verify that the response body confirms the state update.
  2. State Persistence

    • Enable or disable an integration.
    • Reload the page and verify that the toggle state is correctly persisted.
  3. Invalid Theme Preference

    • Send requests with an invalid theme preference (e.g., invalid_theme).
    • Verify that the endpoint returns a 400 Bad Request status code.
    • Confirm that the response body contains an appropriate error message.
  4. Unexpected Error Handling

    • Simulate an unexpected error during the toggle process.
    • Verify that the endpoint returns a 500 Internal Server Error status code.
  5. Fetching Theme Preference

    • Ensure the GET request retrieves the current theme preference correctly.
    • Confirm that the status code is 200.
    • Verify that the response body contains the correct theme preference.
markessien commented 1 month ago

You absolutely do not need a new table for this

ChibuikemMichaelIlonze commented 1 month ago

I closed it, because I opened another Ticket but it was not accepted. I have made the necessary change as requested by the admin