Blog>
Snippets

Defining Redux Actions for Dashboard Updates

Showcase the definition of Redux actions to update the analytics metrics on a dashboard, including the action types and creators.
// Action Types
const UPDATE_METRICS = 'UPDATE_METRICS';
const FETCH_METRICS_REQUEST = 'FETCH_METRICS_REQUEST';
const FETCH_METRICS_SUCCESS = 'FETCH_METRICS_SUCCESS';
const FETCH_METRICS_FAILURE = 'FETCH_METRICS_FAILURE';

// Action Creators
// Action to request metrics data
function fetchMetricsRequest() {
    return {
        type: FETCH_METRICS_REQUEST
    };
}

// Action to store the fetched metrics data
function fetchMetricsSuccess(metricsData) {
    return {
        type: FETCH_METRICS_SUCCESS,
        payload: { metricsData }
    };
}

// Action to handle an error fetching metrics data
function fetchMetricsFailure(error) {
    return {
        type: FETCH_METRICS_FAILURE,
        payload: { error }
    };
}

// Action to update metrics data locally
function updateMetrics(newMetrics) {
    return {
        type: UPDATE_METRICS,
        payload: { newMetrics }
    };
}
Defining Redux action types and action creators for updating dashboard analytics metrics. There are actions for initiating a metrics fetch request, handling a successful fetch response, handling errors during fetch, and updating metrics data locally.