We need to expose a number of functions that can build objects for us containing a type and a payload property. Depending on what function we invoke, we will assign it with a different constant, and of course a different payload, if using one. The action creator fetchProducts() will create an object where only the type is set. This is followed by a fetchSuccessfully() action creator, which will be invoked once the data comes back from an endpoint. Lastly, we have the fetchError() action creator, which we will invoke if an error occurs:
// product/product.actions.ts
import {
FETCHING_PRODUCTS_SUCCESSFULLY,
FETCHING_PRODUCTS_ERROR,
FETCHING_PRODUCTS
} from "./product.constants";
export const fetchSuccessfully = (products) => ({
type: FETCHING_PRODUCTS_SUCCESSFULLY,
payload: products
});
export const fetchError = (error) => ({
type: FETCHING_PRODUCTS_ERROR,
payload: error
});
export const fetchProductsSuccessfully = (products) => ({
type: FETCHING_PRODUCTS_SUCCESSFULLY,
payload: products
});
export const fetchProducts =() => ({ type: FETCHING_PRODUCTS });