Hone logo
Hone
Problems

Implementing User Action Handling in an Angular Application

This challenge focuses on building a fundamental part of any interactive application: handling user actions. You will create a system within an Angular component that allows users to perform specific operations by triggering distinct "actions." This is crucial for creating responsive and user-friendly interfaces.

Problem Description

You need to implement a mechanism in an Angular component to manage and execute different user-defined actions. This system should allow for:

  1. Defining Actions: A clear way to define what actions are available.
  2. Triggering Actions: A method to initiate a specific action.
  3. Executing Actions: The component should be able to perform the logic associated with each action.
  4. Parameter Passing: Actions should be able to accept and utilize input parameters.
  5. Action Feedback: The component should provide some form of feedback indicating which action was executed and with what parameters.

Key Requirements:

  • Create a component, let's call it ActionHandlerComponent.
  • This component should have a mechanism to store and manage a collection of predefined actions. Each action should have a unique identifier and a corresponding execution logic (a function).
  • Implement a method within the component to call a specific action by its identifier and pass optional arguments to it.
  • The component should maintain a history or log of executed actions, including the action identifier and the arguments passed.
  • The component should expose this action history to its template for display.

Expected Behavior:

When an action is triggered, its associated logic should execute, and an entry for that action and its parameters should be added to the component's action history. The template should dynamically update to show this history.

Edge Cases to Consider:

  • Attempting to trigger an action that does not exist.
  • Triggering an action with no parameters when one is expected, or vice-versa (though for this challenge, we'll keep parameter handling simple).

Examples

Example 1: Basic Action Execution

Input: (Conceptual - representing user interaction) User clicks a "Save" button, triggering the save action.

Component State (before action): actionHistory: []

Action Definition (internal to component): actions = { 'save': (data) => console.log('Saving:', data), ... }

Triggering Action: this.triggerAction('save', { userId: 123, content: 'My data' })

Component State (after action): actionHistory: [ { id: 'save', params: { userId: 123, content: 'My data' } } ]

Explanation: The save action was triggered with specific data. The action logic (logging to console) executed, and the action details were recorded in actionHistory.

Example 2: Another Action and History Update

Input: (Conceptual) User clicks a "Delete" button, triggering the delete action.

Component State (before action): actionHistory: [ { id: 'save', params: { userId: 123, content: 'My data' } } ]

Action Definition (internal to component): actions = { 'save': ..., 'delete': (id) => console.log('Deleting item with ID:', id) }

Triggering Action: this.triggerAction('delete', 456)

Component State (after action): actionHistory: [ { id: 'save', params: { userId: 123, content: 'My data' } }, { id: 'delete', params: 456 } ]

Explanation: The delete action was triggered with an ID. Its logic executed, and the new action was appended to the actionHistory.

Example 3: Triggering a Non-existent Action

Input: (Conceptual) User clicks a button that attempts to trigger an undefined update action.

Component State (before action): actionHistory: [ { id: 'save', params: { userId: 123, content: 'My data' } }, { id: 'delete', params: 456 } ]

Action Definition (internal to component): actions = { 'save': ..., 'delete': ... }

Triggering Action: this.triggerAction('update', { recordId: 789 })

Component State (after action): actionHistory: [ { id: 'save', params: { userId: 123, content: 'My data' } }, { id: 'delete', params: 456 } ] (No change to history)

Explanation: Attempting to trigger an action not defined in the actions map results in no execution and no addition to the history. The component should gracefully handle this without errors.

Constraints

  • The Angular component must be written in TypeScript.
  • The action definitions and the triggerAction method should be part of the ActionHandlerComponent class.
  • The actionHistory should be an array of objects, where each object has an id (string) and params (any) property.
  • The component's template should iterate over actionHistory to display the executed actions.

Notes

Consider how you will structure your action definitions. A map or an object where keys are action IDs and values are functions is a common and effective approach. When triggering actions, think about how to safely access and call the corresponding function, handling cases where an action might not be found. The params can be a single value or an object depending on the action's needs. For this challenge, focus on the core mechanism of definition, triggering, and logging.

Loading editor...
typescript