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";
|
|
|
|
|
|
|
|
import { CALL_API } from "../middleware/api";
|
2016-08-01 00:26:52 +02:00
|
|
|
|
2016-08-05 00:00:25 +02:00
|
|
|
import { artist, track, album } from "../models/api";
|
|
|
|
|
2016-08-01 00:26:52 +02:00
|
|
|
export const DEFAULT_LIMIT = 30; /** Default max number of elements to retrieve. */
|
2016-07-07 23:23:18 +02:00
|
|
|
|
|
|
|
export default function (action, requestType, successType, failureType) {
|
|
|
|
const itemName = action.rstrip("s");
|
2016-08-05 00:00:25 +02:00
|
|
|
|
|
|
|
const fetchItemsSuccess = function (jsonData, pageNumber) {
|
|
|
|
// Normalize data
|
|
|
|
jsonData = normalize(
|
|
|
|
jsonData,
|
|
|
|
{
|
|
|
|
artist: arrayOf(artist),
|
|
|
|
album: arrayOf(album),
|
|
|
|
song: arrayOf(track)
|
|
|
|
},
|
|
|
|
{
|
|
|
|
assignEntity: function (output, key, value) {
|
|
|
|
// Delete useless fields
|
|
|
|
if (key == "sessionExpire") {
|
|
|
|
delete output.sessionExpire;
|
|
|
|
} else {
|
|
|
|
output[key] = value;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
|
|
|
const nPages = Math.ceil(jsonData.result[itemName].length / DEFAULT_LIMIT);
|
2016-07-07 23:23:18 +02:00
|
|
|
return {
|
|
|
|
type: successType,
|
|
|
|
payload: {
|
2016-08-05 00:00:25 +02:00
|
|
|
result: jsonData.result,
|
|
|
|
entities: jsonData.entities,
|
2016-08-01 00:26:52 +02:00
|
|
|
nPages: nPages,
|
|
|
|
currentPage: pageNumber
|
2016-07-07 23:23:18 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
};
|
|
|
|
const fetchItemsRequest = function () {
|
|
|
|
return {
|
|
|
|
type: requestType,
|
|
|
|
payload: {
|
|
|
|
}
|
|
|
|
};
|
|
|
|
};
|
|
|
|
const fetchItemsFailure = function (error) {
|
|
|
|
return {
|
|
|
|
type: failureType,
|
|
|
|
payload: {
|
|
|
|
error: error
|
|
|
|
}
|
|
|
|
};
|
|
|
|
};
|
2016-08-01 00:26:52 +02:00
|
|
|
const fetchItems = function (endpoint, username, passphrase, filter, pageNumber, include = [], limit=DEFAULT_LIMIT) {
|
|
|
|
const offset = (pageNumber - 1) * DEFAULT_LIMIT;
|
2016-08-05 00:00:25 +02:00
|
|
|
let extraParams = {
|
2016-07-07 23:23:18 +02:00
|
|
|
offset: offset,
|
|
|
|
limit: limit
|
|
|
|
};
|
|
|
|
if (filter) {
|
|
|
|
extraParams.filter = filter;
|
|
|
|
}
|
|
|
|
if (include && include.length > 0) {
|
|
|
|
extraParams.include = include;
|
|
|
|
}
|
|
|
|
return {
|
|
|
|
type: CALL_API,
|
|
|
|
payload: {
|
|
|
|
endpoint: endpoint,
|
|
|
|
dispatch: [
|
|
|
|
fetchItemsRequest,
|
|
|
|
jsonData => dispatch => {
|
2016-08-05 00:00:25 +02:00
|
|
|
dispatch(fetchItemsSuccess(jsonData, pageNumber));
|
2016-07-07 23:23:18 +02:00
|
|
|
},
|
|
|
|
fetchItemsFailure
|
|
|
|
],
|
|
|
|
action: action,
|
|
|
|
auth: passphrase,
|
|
|
|
username: username,
|
|
|
|
extraParams: extraParams
|
|
|
|
}
|
|
|
|
};
|
|
|
|
};
|
|
|
|
const loadItems = function({ pageNumber = 1, filter = null, include = [] } = {}) {
|
|
|
|
return (dispatch, getState) => {
|
|
|
|
const { auth } = getState();
|
2016-08-01 00:26:52 +02:00
|
|
|
dispatch(fetchItems(auth.endpoint, auth.username, auth.token.token, filter, pageNumber, include));
|
2016-07-07 23:23:18 +02:00
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
const camelizedAction = humps.pascalize(action);
|
|
|
|
var returned = {};
|
|
|
|
returned["fetch" + camelizedAction + "Success"] = fetchItemsSuccess;
|
|
|
|
returned["fetch" + camelizedAction + "Request"] = fetchItemsRequest;
|
|
|
|
returned["fetch" + camelizedAction + "Failure"] = fetchItemsFailure;
|
|
|
|
returned["fetch" + camelizedAction] = fetchItems;
|
|
|
|
returned["load" + camelizedAction] = loadItems;
|
|
|
|
return returned;
|
|
|
|
}
|