2016-08-10 21:36:11 +02:00
|
|
|
/**
|
|
|
|
* This file implements actions to fetch and load data from the API.
|
|
|
|
*/
|
|
|
|
|
|
|
|
// NPM imports
|
2016-08-05 00:00:25 +02:00
|
|
|
import { normalize, arrayOf } from "normalizr";
|
2016-07-07 23:23:18 +02:00
|
|
|
import humps from "humps";
|
|
|
|
|
2016-08-10 21:36:11 +02:00
|
|
|
// Other actions
|
2016-07-07 23:23:18 +02:00
|
|
|
import { CALL_API } from "../middleware/api";
|
2016-08-10 21:36:11 +02:00
|
|
|
import { pushEntities } from "./entities";
|
2016-08-01 00:26:52 +02:00
|
|
|
|
2016-08-10 21:36:11 +02:00
|
|
|
// Models
|
|
|
|
import { artist, song, album } from "../models/api";
|
2016-08-05 00:00:25 +02:00
|
|
|
|
2016-08-10 21:36:11 +02:00
|
|
|
// Constants
|
2016-08-07 00:58:36 +02:00
|
|
|
export const DEFAULT_LIMIT = 32; /** Default max number of elements to retrieve. */
|
2016-07-07 23:23:18 +02:00
|
|
|
|
2016-08-10 21:36:11 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* This function wraps around an API action to generate actions trigger
|
|
|
|
* functions to load items etc.
|
|
|
|
*
|
|
|
|
* @param action API action.
|
|
|
|
* @param requestType Action type to trigger on request.
|
|
|
|
* @param successType Action type to trigger on success.
|
|
|
|
* @param failureType Action type to trigger on failure.
|
|
|
|
*/
|
2016-07-07 23:23:18 +02:00
|
|
|
export default function (action, requestType, successType, failureType) {
|
2016-08-10 21:36:11 +02:00
|
|
|
/** Get the name of the item associated with action */
|
2016-07-07 23:23:18 +02:00
|
|
|
const itemName = action.rstrip("s");
|
2016-08-05 00:00:25 +02:00
|
|
|
|
2016-08-10 21:36:11 +02:00
|
|
|
/**
|
|
|
|
* Normalizr helper to normalize API response.
|
|
|
|
*
|
|
|
|
* @param jsonData The JS object returned by the API.
|
|
|
|
* @return A normalized object.
|
|
|
|
*/
|
|
|
|
const _normalizeAPIResponse = function (jsonData) {
|
|
|
|
return normalize(
|
2016-08-05 00:00:25 +02:00
|
|
|
jsonData,
|
|
|
|
{
|
|
|
|
artist: arrayOf(artist),
|
|
|
|
album: arrayOf(album),
|
2016-08-10 23:50:23 +02:00
|
|
|
song: arrayOf(song),
|
2016-08-05 00:00:25 +02:00
|
|
|
},
|
|
|
|
{
|
2016-08-10 21:36:11 +02:00
|
|
|
// Use custom assignEntity function to delete useless fields
|
2016-08-05 00:00:25 +02:00
|
|
|
assignEntity: function (output, key, value) {
|
|
|
|
if (key == "sessionExpire") {
|
|
|
|
delete output.sessionExpire;
|
|
|
|
} else {
|
|
|
|
output[key] = value;
|
|
|
|
}
|
2016-08-10 23:50:23 +02:00
|
|
|
},
|
2016-08-05 00:00:25 +02:00
|
|
|
}
|
|
|
|
);
|
2016-08-10 21:36:11 +02:00
|
|
|
};
|
2016-08-05 00:00:25 +02:00
|
|
|
|
2016-08-10 21:36:11 +02:00
|
|
|
/**
|
|
|
|
* Callback on successful fetch of paginated items
|
|
|
|
*
|
|
|
|
* @param jsonData JS object returned from the API.
|
|
|
|
* @param pageNumber Number of the page that was fetched.
|
|
|
|
*/
|
|
|
|
const fetchPaginatedItemsSuccess = function (jsonData, pageNumber, limit) {
|
2016-08-11 01:05:33 +02:00
|
|
|
const totalCount = jsonData.totalCount;
|
2016-08-10 21:36:11 +02:00
|
|
|
jsonData = _normalizeAPIResponse(jsonData);
|
|
|
|
|
|
|
|
// Compute the total number of pages
|
2016-08-11 01:05:33 +02:00
|
|
|
const nPages = Math.ceil(totalCount / limit);
|
2016-08-10 21:36:11 +02:00
|
|
|
|
|
|
|
// Return success actions
|
|
|
|
return [
|
|
|
|
// Action for the global entities store
|
|
|
|
pushEntities(jsonData.entities, [itemName]),
|
|
|
|
// Action for the paginated store
|
|
|
|
{
|
|
|
|
type: successType,
|
|
|
|
payload: {
|
|
|
|
type: itemName,
|
|
|
|
result: jsonData.result[itemName],
|
|
|
|
nPages: nPages,
|
2016-08-10 23:50:23 +02:00
|
|
|
currentPage: pageNumber,
|
|
|
|
},
|
|
|
|
},
|
2016-08-10 21:36:11 +02:00
|
|
|
];
|
2016-07-07 23:23:18 +02:00
|
|
|
};
|
2016-08-10 21:36:11 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Callback on successful fetch of single item
|
|
|
|
*
|
|
|
|
* @param jsonData JS object returned from the API.
|
|
|
|
* @param pageNumber Number of the page that was fetched.
|
|
|
|
*/
|
|
|
|
const fetchItemSuccess = function (jsonData) {
|
|
|
|
jsonData = _normalizeAPIResponse(jsonData);
|
|
|
|
|
|
|
|
return pushEntities(jsonData.entities, [itemName]);
|
|
|
|
};
|
|
|
|
|
|
|
|
/** Callback on request */
|
2016-07-07 23:23:18 +02:00
|
|
|
const fetchItemsRequest = function () {
|
2016-08-10 21:36:11 +02:00
|
|
|
// Return a request type action
|
2016-07-07 23:23:18 +02:00
|
|
|
return {
|
|
|
|
type: requestType,
|
|
|
|
payload: {
|
2016-08-10 23:50:23 +02:00
|
|
|
},
|
2016-07-07 23:23:18 +02:00
|
|
|
};
|
|
|
|
};
|
2016-08-10 21:36:11 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Callback on failed fetch
|
|
|
|
*
|
|
|
|
* @param error An error object, either a string or an i18nError
|
|
|
|
* object.
|
|
|
|
*/
|
2016-07-07 23:23:18 +02:00
|
|
|
const fetchItemsFailure = function (error) {
|
2016-08-10 21:36:11 +02:00
|
|
|
// Return a failure type action
|
2016-07-07 23:23:18 +02:00
|
|
|
return {
|
|
|
|
type: failureType,
|
|
|
|
payload: {
|
2016-08-10 23:50:23 +02:00
|
|
|
error: error,
|
|
|
|
},
|
2016-07-07 23:23:18 +02:00
|
|
|
};
|
|
|
|
};
|
2016-08-10 21:36:11 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Method to trigger a fetch of items.
|
|
|
|
*
|
|
|
|
* @param endpoint Ampache server base URL.
|
|
|
|
* @param username Username to use for API request.
|
|
|
|
* @param filter An eventual filter to apply (mapped to API filter
|
|
|
|
* param)
|
|
|
|
* @param pageNumber Number of the page to fetch items from.
|
|
|
|
* @param limit Max number of items to fetch.
|
|
|
|
* @param include [Optional] A list of includes to return as well
|
|
|
|
* (mapped to API include param)
|
|
|
|
*
|
|
|
|
* @return A CALL_API action to fetch the specified items.
|
|
|
|
*/
|
|
|
|
const fetchItems = function (endpoint, username, passphrase, filter, pageNumber, limit, include = []) {
|
|
|
|
// Compute offset in number of items from the page number
|
2016-08-01 00:26:52 +02:00
|
|
|
const offset = (pageNumber - 1) * DEFAULT_LIMIT;
|
2016-08-10 21:36:11 +02:00
|
|
|
// Set extra params for pagination
|
2016-08-05 00:00:25 +02:00
|
|
|
let extraParams = {
|
2016-07-07 23:23:18 +02:00
|
|
|
offset: offset,
|
2016-08-10 23:50:23 +02:00
|
|
|
limit: limit,
|
2016-07-07 23:23:18 +02:00
|
|
|
};
|
2016-08-10 21:36:11 +02:00
|
|
|
|
|
|
|
// Handle filter
|
2016-07-07 23:23:18 +02:00
|
|
|
if (filter) {
|
|
|
|
extraParams.filter = filter;
|
|
|
|
}
|
2016-08-10 21:36:11 +02:00
|
|
|
|
|
|
|
// Handle includes
|
2016-07-07 23:23:18 +02:00
|
|
|
if (include && include.length > 0) {
|
|
|
|
extraParams.include = include;
|
|
|
|
}
|
2016-08-10 21:36:11 +02:00
|
|
|
|
|
|
|
// Return a CALL_API action
|
2016-07-07 23:23:18 +02:00
|
|
|
return {
|
|
|
|
type: CALL_API,
|
|
|
|
payload: {
|
|
|
|
endpoint: endpoint,
|
|
|
|
dispatch: [
|
|
|
|
fetchItemsRequest,
|
2016-08-10 21:36:11 +02:00
|
|
|
null,
|
2016-08-10 23:50:23 +02:00
|
|
|
fetchItemsFailure,
|
2016-07-07 23:23:18 +02:00
|
|
|
],
|
|
|
|
action: action,
|
|
|
|
auth: passphrase,
|
|
|
|
username: username,
|
2016-08-10 23:50:23 +02:00
|
|
|
extraParams: extraParams,
|
|
|
|
},
|
2016-07-07 23:23:18 +02:00
|
|
|
};
|
|
|
|
};
|
2016-08-10 21:36:11 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* High level method to load paginated items from the API wihtout dealing about credentials.
|
|
|
|
*
|
|
|
|
* @param pageNumber [Optional] Number of the page to fetch items from.
|
|
|
|
* @param filter [Optional] An eventual filter to apply (mapped to
|
|
|
|
* API filter param)
|
|
|
|
* @param include [Optional] A list of includes to return as well
|
|
|
|
* (mapped to API include param)
|
|
|
|
*
|
|
|
|
* Dispatches the CALL_API action to fetch these items.
|
|
|
|
*/
|
2016-08-10 23:50:23 +02:00
|
|
|
const loadPaginatedItems = function ({ pageNumber = 1, limit = DEFAULT_LIMIT, filter = null, include = [] } = {}) {
|
2016-07-07 23:23:18 +02:00
|
|
|
return (dispatch, getState) => {
|
2016-08-10 21:36:11 +02:00
|
|
|
// Get credentials from the state
|
2016-07-07 23:23:18 +02:00
|
|
|
const { auth } = getState();
|
2016-08-10 21:36:11 +02:00
|
|
|
// Get the fetch action to dispatch
|
|
|
|
const fetchAction = fetchItems(
|
|
|
|
auth.endpoint,
|
|
|
|
auth.username,
|
|
|
|
auth.token.token,
|
|
|
|
filter,
|
|
|
|
pageNumber,
|
|
|
|
limit,
|
|
|
|
include
|
|
|
|
);
|
|
|
|
// Set success callback
|
|
|
|
fetchAction.payload.dispatch[1] = (
|
|
|
|
jsonData => dispatch => {
|
|
|
|
// Dispatch all the necessary actions
|
|
|
|
const actions = fetchPaginatedItemsSuccess(jsonData, pageNumber, limit);
|
|
|
|
actions.map(action => dispatch(action));
|
|
|
|
}
|
|
|
|
);
|
|
|
|
// Dispatch action
|
|
|
|
dispatch(fetchAction);
|
2016-07-07 23:23:18 +02:00
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2016-08-10 21:36:11 +02:00
|
|
|
/**
|
|
|
|
* High level method to load a single item from the API wihtout dealing about credentials.
|
|
|
|
*
|
|
|
|
* @param filter The filter to apply (mapped to API filter param)
|
|
|
|
* @param include [Optional] A list of includes to return as well
|
|
|
|
* (mapped to API include param)
|
|
|
|
*
|
|
|
|
* Dispatches the CALL_API action to fetch this item.
|
|
|
|
*/
|
2016-08-10 23:50:23 +02:00
|
|
|
const loadItem = function ({ filter = null, include = [] } = {}) {
|
2016-08-10 21:36:11 +02:00
|
|
|
return (dispatch, getState) => {
|
|
|
|
// Get credentials from the state
|
|
|
|
const { auth } = getState();
|
|
|
|
// Get the action to dispatch
|
|
|
|
const fetchAction = fetchItems(
|
|
|
|
auth.endpoint,
|
|
|
|
auth.username,
|
|
|
|
auth.token.token,
|
|
|
|
filter,
|
|
|
|
1,
|
|
|
|
DEFAULT_LIMIT,
|
|
|
|
include
|
|
|
|
);
|
|
|
|
// Set success callback
|
|
|
|
fetchAction.payload.dispatch[1] = (
|
|
|
|
jsonData => dispatch => {
|
|
|
|
dispatch(fetchItemSuccess(jsonData));
|
|
|
|
}
|
|
|
|
);
|
|
|
|
// Dispatch action
|
|
|
|
dispatch(fetchAction);
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
// Remap the above methods to methods including item name
|
2016-07-07 23:23:18 +02:00
|
|
|
var returned = {};
|
2016-08-10 21:36:11 +02:00
|
|
|
const camelizedAction = humps.pascalize(action);
|
|
|
|
returned["loadPaginated" + camelizedAction] = loadPaginatedItems;
|
|
|
|
returned["load" + camelizedAction.rstrip("s")] = loadItem;
|
2016-07-07 23:23:18 +02:00
|
|
|
return returned;
|
|
|
|
}
|