Files
Iris/src/js/services/discogs/actions.js

105 lines
2.8 KiB
JavaScript
Raw Normal View History

2019-09-05 15:36:11 +12:00
const coreActions = require('../core/actions');
const uiActions = require('../ui/actions');
const helpers = require('../../helpers');
/**
* Send an ajax request to the LastFM API
*
* @param dispatch = obj
* @param endpoint = String
* @param params = String
2019-09-05 15:36:11 +12:00
* */
const sendRequest = (dispatch, endpoint, params) => new Promise((resolve, reject) => {
const loader_key = helpers.generateGuid();
2019-09-05 15:36:11 +12:00
dispatch(uiActions.startLoading(loader_key, `,discogs_${endpoint}`));
2019-09-05 15:18:16 +12:00
2019-09-05 15:36:11 +12:00
const key = 'CXIwsVMAjrXIVitBWgqd';
const secret = 'KiEUfwKpebxRnEHlKoXnYIftJxeuqjTK';
2019-09-05 15:36:11 +12:00
const config = {
method: 'GET',
cache: true,
timeout: 30000,
url: `https://api.discogs.com/${endpoint}?key=${key}&secret=${secret}&${params}`,
headers: {
'user-agent': 'Iris/1.0',
},
};
2019-09-05 15:36:11 +12:00
$.ajax(config).then(
(response) => {
dispatch(uiActions.stopLoading(loader_key));
if (response.error) {
reject({
config,
error: response,
});
} else {
resolve(response);
}
},
(xhr, status, error) => {
dispatch(uiActions.stopLoading(loader_key));
2019-09-05 15:36:11 +12:00
// Snatch a more meaningful error
let description = null;
if (xhr && xhr.responseJSON && xhr.responseJSON.message) {
description = xhr.responseJSON.message;
}
2019-09-05 15:36:11 +12:00
reject({
config,
error,
description,
status,
xhr,
});
},
);
});
2019-09-05 15:36:11 +12:00
export function findArtist(name) {
return sendRequest(dispatch, `artists/${artist.mbid}`, 'inc=url-rels')
.then(
(response) => {
if (response) {
const image_relations = response.relations.filter((rel) => rel.type == 'image');
if (image_relations) {
const updated_artist = {
uri,
images: image_relations.map((relation) => relation.url.resource.replace('File:', 'Special:FilePath/')),
};
dispatch(coreActions.artistLoaded(updated_artist));
}
}
},
(error) => {
console.log(`Musicbrainz: No results for ${artist.mbid}`);
},
);
}
2019-09-05 15:36:11 +12:00
export function getArtistImages(uri, artist) {
return (dispatch, getState) => {
sendRequest(dispatch, 'database/search', `query=${artist.name}`)
.then(
(response) => {
if (response) {
if (response.results && response.results[0].cover_image) {
const updated_artist = {
uri,
images: [response.results[0].cover_image],
};
dispatch(coreActions.artistLoaded(updated_artist));
}
}
},
(error) => {
console.log(`Discogs: No results for ${artist.name}`);
},
);
};
}