Hone logo
Hone
Problems

Implementing Code Actions in Angular with TypeScript

This challenge focuses on creating custom code actions within an Angular application. Code actions allow developers to perform specific tasks directly within the application's UI, enhancing productivity and providing a tailored user experience. You'll be building a simple component that demonstrates how to define and trigger these actions.

Problem Description

You need to create an Angular component called CodeActionDemoComponent that displays a list of items and provides two code actions: "Mark as Complete" and "Delete Item." When a user clicks on these actions associated with a specific item, the component should update the item's status (for "Mark as Complete") or remove it from the list (for "Delete Item"). The component should manage its own data and update the UI accordingly.

Key Requirements:

  • Data Structure: The component should maintain an array of Item objects. Each Item object has a name (string) and a completed (boolean) property.
  • Code Actions: Implement two code actions:
    • markComplete(item: Item): Sets the completed property of the provided item to true.
    • deleteItem(item: Item): Removes the provided item from the component's data array.
  • UI Display: The component should display the list of items, showing their names and completion status. Completed items should be visually distinct (e.g., with a strikethrough).
  • Action Triggers: Each item should have buttons or links associated with it to trigger the "Mark as Complete" and "Delete Item" actions.
  • Reactive Updates: The UI should update automatically whenever an item's status changes or an item is deleted.

Expected Behavior:

  1. The component loads with an initial list of items.
  2. Clicking "Mark as Complete" on an item changes its completed status to true and updates the UI to reflect this change.
  3. Clicking "Delete Item" on an item removes it from the list and updates the UI.
  4. The component handles empty lists gracefully (e.g., displays a message indicating there are no items).

Edge Cases to Consider:

  • What happens if the user tries to mark an already completed item as complete? (Consider logging a warning or doing nothing).
  • What happens if the user tries to delete an item that doesn't exist in the list (e.g., due to a race condition)? (Consider logging an error or doing nothing).

Examples

Example 1:

Input: Initial items: [{name: "Task 1", completed: false}, {name: "Task 2", completed: false}]
Output: UI displays: "Task 1" (not completed), "Task 2" (not completed) with "Mark Complete" and "Delete" buttons for each.
Explanation: The component renders the initial list of items.

Example 2:

Input: User clicks "Mark Complete" on "Task 1".
Output: UI displays: "Task 1" (completed - e.g., strikethrough), "Task 2" (not completed) with "Mark Complete" and "Delete" buttons for each.
Explanation: The `markComplete` function is called, updating the `completed` property of "Task 1". The UI re-renders to reflect the change.

Example 3:

Input: User clicks "Delete" on "Task 2".
Output: UI displays: "Task 1" (not completed) with "Mark Complete" and "Delete" buttons.
Explanation: The `deleteItem` function is called, removing "Task 2" from the data array. The UI re-renders to reflect the change.

Constraints

  • Angular Version: Use Angular version 14 or higher.
  • Data Size: The initial list of items should contain between 3 and 5 items.
  • Performance: The component should update the UI efficiently, even with a larger list of items (consider using change detection optimization techniques if necessary, though not strictly required for this challenge).
  • No External Libraries: Do not use any external libraries for UI components or state management. Use standard Angular features.

Notes

  • Consider using template-driven forms or reactive forms for handling the UI updates, although this is not strictly required.
  • Think about how to structure your component's code for readability and maintainability.
  • Focus on demonstrating the core concepts of defining and triggering code actions within an Angular component.
  • You can use any styling approach you prefer (CSS, SCSS, etc.). The visual appearance is less important than the functionality.
  • The Item interface is crucial for type safety. Make sure to define it correctly.
Loading editor...
typescript