Hone logo
Hone
Problems

Vue Store Composition with TypeScript

This challenge focuses on building a reusable Vue store module using Vuex's composition API and TypeScript. Creating modular stores is crucial for larger Vue applications, promoting code organization, maintainability, and reusability. You'll be tasked with building a store module that manages a simple product inventory, demonstrating the core principles of state management with Vuex and TypeScript.

Problem Description

You need to create a Vuex store module named product that manages a list of products. Each product has an id (number), name (string), and price (number). The module should provide actions to add a new product, delete a product by its ID, and update a product's price. It should also expose the product list as state. The module should be written in TypeScript, ensuring type safety throughout.

Key Requirements:

  • State: The state should be an array of product objects.
  • Getters: Provide a getter to retrieve all products.
  • Mutations: Implement mutations to update the state (add, delete, update).
  • Actions: Implement actions that commit mutations to modify the state. Actions should handle asynchronous operations if needed (though this challenge doesn't require actual asynchronous calls, the structure should be prepared for it).
  • TypeScript: Use TypeScript to define the product type and ensure type safety for state, mutations, and actions.

Expected Behavior:

  • The store should initialize with an empty product list.
  • Adding a product should add a new product object to the list.
  • Deleting a product should remove the product with the specified ID from the list.
  • Updating a product's price should modify the price of the product with the specified ID.
  • The getter should return the current product list.

Edge Cases to Consider:

  • Attempting to delete a product with an ID that doesn't exist. (Should not throw an error, simply do nothing).
  • Attempting to update the price of a product with an ID that doesn't exist. (Should not throw an error, simply do nothing).
  • Handling invalid input when adding a product (e.g., missing name or price). For simplicity, assume valid input is always provided.

Examples

Example 1:

Input: Initial state: []
Action: addProduct({ name: 'Laptop', price: 1200 })
Action: addProduct({ name: 'Mouse', price: 25 })
Output: [{ id: 1, name: 'Laptop', price: 1200 }, { id: 2, name: 'Mouse', price: 25 }]
Explanation: Two products are added to the initial empty state.  IDs are automatically assigned sequentially.

Example 2:

Input: State: [{ id: 1, name: 'Laptop', price: 1200 }, { id: 2, name: 'Mouse', price: 25 }]
Action: deleteProduct(2)
Output: [{ id: 1, name: 'Laptop', price: 1200 }]
Explanation: The product with ID 2 (Mouse) is removed from the state.

Example 3:

Input: State: [{ id: 1, name: 'Laptop', price: 1200 }, { id: 2, name: 'Mouse', price: 25 }]
Action: updateProductPrice(1, 1300)
Output: [{ id: 1, name: 'Laptop', price: 1300 }, { id: 2, name: 'Mouse', price: 25 }]
Explanation: The price of the product with ID 1 (Laptop) is updated to 1300.

Constraints

  • Product IDs should be auto-incremented starting from 1.
  • The store module should be self-contained and not rely on external dependencies beyond Vuex.
  • The code should be well-formatted and easy to understand.
  • The solution must be written in TypeScript.
  • Assume the Vuex store is already initialized and the module is being registered.

Notes

  • Consider using a type alias or interface to define the Product type.
  • Think about how to structure your mutations and actions for clarity and maintainability.
  • While not required, consider how you might extend this module to handle more complex product data or asynchronous operations in the future.
  • Focus on the core functionality of managing the product list and its associated actions. Error handling beyond the specified edge cases is not required.
Loading editor...
typescript