This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// products/ducks/productsOperations.js | |
import { | |
getProducts, | |
patchProduct as _patchProduct | |
// other API endpoints (HTTP verbs) | |
} from 'api/products' // webpack 'api' alias | |
import { | |
fetchProductsRequest, | |
fetchProductsSuccess, |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const normalize = items => ({ | |
byId: items.reduce((_items, item) => ({ | |
..._items, | |
[item.id]: item | |
}), {}), | |
allIds: items.map(item => item.id) | |
}) | |
export default normalize |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// api.js | |
import axios from 'axios' | |
const API_URL = process.env.API_URL | |
const AUTH_TOKEN = ... // from local storage, session etc. | |
const api = axios.create({ | |
baseURL: API_URL, | |
headers: { 'Authorization': 'JWT ' + AUTH_TOKEN } | |
}) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// duck/ordersFetch.js | |
import { getOrders } from '../api/orders' | |
import { | |
fetchOrdersRequest, // 'request' or 'begin' | |
fetchOrdersSuccess, | |
fetchOrdersError // 'error' or 'failure' | |
} from './ordersActions' | |
const fetchOrders = params => | |
dispatch => { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// api/orders.js | |
import api from './api' | |
// 'sanitize' function here | |
const getOrders = ({ offset = 0, limit = 25, query = '', ...otherParams }) => | |
api.get(`/orders?offset=${offset}&limit=${limit}&query=${query}`) | |
.then(response => ({ | |
items: response.data.results.map(sanitize), | |
count: response.data.count |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// duck/ordersActions.js | |
export const FETCH_ORDERS_REQUEST = 'FETCH_ORDERS_REQUEST' | |
export const FETCH_ORDERS_SUCCESS = 'FETCH_ORDERS_SUCCESS' | |
export const FETCH_ORDERS_ERROR = 'FETCH_ORDERS_ERROR' | |
export const fetchOrdersRequest = _ => ({ // _ = () | |
type: FETCH_ORDERS_REQUEST | |
// no payload | |
}) |