Hone logo
Hone
Problems

Efficient Batch Updates in Vue.js with TypeScript

Often in web applications, users need to make multiple modifications to a list of data. Performing individual updates for each change can be inefficient and lead to a poor user experience due to frequent re-renders. This challenge focuses on implementing a mechanism for efficient batch updates in a Vue.js application using TypeScript. You will create a component that allows users to select multiple items from a list and apply a uniform change to all of them simultaneously.

Problem Description

Your task is to build a Vue 3 component that displays a list of items. Users should be able to select multiple items from this list. Once multiple items are selected, a button should appear allowing the user to trigger a "batch update." This update will apply a common change to all selected items. The goal is to achieve this efficiently, minimizing unnecessary re-renders.

Key Requirements:

  1. Display a List of Items: Render a list of items, each with a unique identifier and some displayable data (e.g., a name or value).
  2. Selectable Items: Each item in the list should have a mechanism (e.g., a checkbox) to allow users to select or deselect it.
  3. Batch Update Trigger: A button should be visible only when one or more items are selected. Clicking this button will initiate the batch update.
  4. Apply Batch Update: When the batch update button is clicked, a predefined modification should be applied to all currently selected items. For this challenge, the modification will be to toggle a boolean isActive property on the selected items.
  5. Efficient Rendering: The updates should be performed in a way that avoids excessive re-renders of the entire list. Vue's reactivity system should be leveraged effectively.
  6. TypeScript Integration: The entire component and its data structures must be written in TypeScript.

Expected Behavior:

  • When the component loads, a list of items is displayed.
  • Users can click on individual items (or their associated checkboxes) to select or deselect them.
  • A "Batch Update" button appears when at least one item is selected and hides when no items are selected.
  • When the "Batch Update" button is clicked, all currently selected items will have their isActive property toggled. The UI should reflect these changes.
  • Deselecting an item should not revert the isActive status if it was already toggled by a batch update.

Edge Cases:

  • Selecting and deselecting items multiple times before performing a batch update.
  • Selecting all items and then deselecting some before the batch update.
  • The list might be empty initially.

Examples

Example 1:

Input Data (initial):
[
  { id: 1, name: 'Item A', isActive: false },
  { id: 2, name: 'Item B', isActive: false },
  { id: 3, name: 'Item C', isActive: false },
]

User Actions:
1. Select Item 1.
2. Select Item 3.
3. Click "Batch Update".

Output Data (after batch update):
[
  { id: 1, name: 'Item A', isActive: true },
  { id: 2, name: 'Item B', isActive: false },
  { id: 3, name: 'Item C', isActive: true },
]

Explanation: Items with IDs 1 and 3 were selected and then the batch update toggled their `isActive` property from `false` to `true`. Item 2 was not selected and its state remains unchanged.

Example 2:

Input Data (after Example 1's batch update):
[
  { id: 1, name: 'Item A', isActive: true },
  { id: 2, name: 'Item B', isActive: false },
  { id: 3, name: 'Item C', isActive: true },
]

User Actions:
1. Deselect Item 1.
2. Select Item 2.
3. Click "Batch Update".

Output Data (after batch update):
[
  { id: 1, name: 'Item A', isActive: true },
  { id: 2, name: 'Item B', isActive: true },
  { id: 3, name: 'Item C', isActive: true },
]

Explanation: Item 1 was deselected, but its `isActive` state (true) was retained. Item 2 was selected and its `isActive` property was toggled from `false` to `true`. Item 3 remained selected and its `isActive` state was toggled from `true` to `false` (effectively staying `true` if it were false, or staying `false` if it were true - in this case, it toggles from true to false).

Example 3: Empty List Scenario

Input Data (initial):
[]

User Actions:
- No items to select.
- "Batch Update" button should not be visible.

Output Data:
[]

Explanation: The component correctly handles an empty list, displaying nothing and not showing the batch update button.

Constraints

  • The list of items will contain at most 1000 items.
  • Each item will have a unique numeric id and a name string.
  • Each item will also have a boolean isActive property.
  • The Vue.js version used will be 3.x.
  • The solution must be written entirely in TypeScript.
  • The component should efficiently update the UI without noticeable lag for the given list size.

Notes

  • Consider how you will manage the selected state for each item. A separate array or set of selected IDs could be useful.
  • Think about how to update the isActive property in your data store (e.g., a reactive array) efficiently. Modifying items directly in the array is generally efficient in Vue 3.
  • The "Batch Update" functionality is a good candidate for demonstrating the power of Vue's reactivity and potentially debouncing or throttling if the operation were more complex or the list much larger. For this challenge, direct updates are expected.
  • The component should be self-contained and demonstrate the complete logic.
Loading editor...
typescript