Service worker caching, Loader component

This commit is contained in:
James Barnsley
2019-09-10 06:59:44 +12:00
parent a86403fa59
commit c08cc71c47
49 changed files with 16390 additions and 11259 deletions

View File

@ -10,49 +10,56 @@ const helpers = require('../../helpers');
* @param endpoint = String
* @param params = String
* */
const sendRequest = (dispatch, endpoint, params) => new Promise((resolve, reject) => {
const loader_key = helpers.generateGuid();
dispatch(uiActions.startLoading(loader_key, `,discogs_${endpoint}`));
const sendRequest = (dispatch, getState, endpoint, params) => new Promise((resolve, reject) => {
const key = 'CXIwsVMAjrXIVitBWgqd';
const secret = 'KiEUfwKpebxRnEHlKoXnYIftJxeuqjTK';
const url = `https://api.discogs.com/${endpoint}?${params}`;
const loader_key = helpers.generateGuid();
dispatch(uiActions.startLoading(loader_key, `discogs_${endpoint}`));
const checkRateLimit = (request) => {
const rate = {
limit: request.getResponseHeader('X-Discogs-Ratelimit'),
remaining: request.getResponseHeader('X-Discogs-Ratelimit-Remaining'),
used: request.getResponseHeader('X-Discogs-Ratelimit-Used'),
}
if (rate.remaining !== undefined && rate.remaining === 0) {
dispatch(uiActions.createNotification({
key: "discogs_rate_limit",
type: "bad",
content: "Rate limit exceeded",
description: `Discogs rate limit exceeded, try again in a few minutes.`
}));
}
}
const config = {
method: 'GET',
cache: true,
timeout: 30000,
url: `https://api.discogs.com/${endpoint}?key=${key}&secret=${secret}&${params}`,
url: url,
crossDomain: true,
headers: {
'user-agent': 'Iris/1.0',
'User-Agent': 'Iris/1.0',
'Authorization': `Discogs key=${key}, secret=${secret}`
},
};
$.ajax(config).then(
(response) => {
(data, textStatus, request) => {
dispatch(uiActions.stopLoading(loader_key));
if (response.error) {
reject({
config,
error: response,
});
} else {
resolve(response);
}
checkRateLimit(request);
resolve(data);
},
(xhr, status, error) => {
dispatch(uiActions.stopLoading(loader_key));
// Snatch a more meaningful error
let description = null;
if (xhr && xhr.responseJSON && xhr.responseJSON.message) {
description = xhr.responseJSON.message;
}
checkRateLimit(xhr);
reject({
config,
error,
description,
status,
xhr,
});
@ -60,45 +67,62 @@ const sendRequest = (dispatch, endpoint, params) => new Promise((resolve, reject
);
});
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}`);
},
);
}
export function getArtistImages(uri, artist) {
return (dispatch, getState) => {
sendRequest(dispatch, 'database/search', `query=${artist.name}`)
sendRequest(dispatch, getState, 'database/search', `type=artist&query=${artist.name}`)
.then(
(response) => {
if (response) {
if (response.results && response.results[0].cover_image) {
if (response.results.length > 0 && response.results[0].cover_image !== undefined) {
const updated_artist = {
uri,
images: [response.results[0].cover_image],
};
dispatch(coreActions.artistLoaded(updated_artist));
} else {
console.log(`Discogs: No results for artist ${artist.name}`);
}
}
},
(error) => {
console.log(`Discogs: No results for ${artist.name}`);
console.error(error);
},
);
/*
const finalise = (cover_image, cache) => {
const updated_artist = {
uri,
images: [cover_image],
};
dispatch(coreActions.artistLoaded(updated_artist));
if (cache) {
helpers.cache.set(`discogs_${artist.name}`, cover_image);
}
}
const cached = helpers.cache.get(`discogs_${artist.name}`);
if (cached) {
finalise(cached);
return;
}
sendRequest(dispatch, getState, 'database/search', `type=artist&query=${artist.name}`)
.then(
(response) => {
if (response) {
if (response.results.length > 0 && response.results[0].cover_image !== undefined) {
finalise(response.results[0].cover_image, true);
} else {
console.log(`Discogs: No results for artist ${artist.name}`);
}
}
},
(error) => {
console.error(error);
},
);
*/
};
}