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

@ -1,4 +1,5 @@
import axios from 'axios';
const coreActions = require('../core/actions');
const uiActions = require('../ui/actions');
const helpers = require('../../helpers');
@ -18,57 +19,47 @@ export function set(data) {
* @param params = string, the url params to send
* @params signed = boolean, whether we've got a signed request with baked-in api_key
* */
const sendRequest = (dispatch, getState, params, signed = false) => new Promise((resolve, reject) => {
const loader_key = helpers.generateGuid();
const sendRequest = (dispatch, getState, params, signed = false) => new Promise((resolve, reject) => {
let url = `https://ws.audioscrobbler.com/2.0/?format=json&${params}`;
let http_method = 'GET';
let method = params.substring(params.indexOf('method=') + 7, params.length);
method = method.substring(0, method.indexOf('&'));
dispatch(uiActions.startLoading(loader_key, `lastfm_${method}`));
const config = {
method: 'GET',
cache: true,
timeout: 30000,
url: `https://ws.audioscrobbler.com/2.0/?format=json&${params}`,
};
// Signed requests don't need our api_key as the proxy has it's own
if (!signed) {
config.url += '&api_key=4320a3ef51c9b3d69de552ac083c55e3';
url += '&api_key=4320a3ef51c9b3d69de552ac083c55e3';
} else {
config.method = 'POST';
http_method = 'POST';
}
$.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));
const config = {
method: http_method,
timeout: 30000,
cache: "force-cache",
};
// Snatch a more meaningful error
let description = null;
if (xhr && xhr.responseJSON && xhr.responseJSON.message) {
description = xhr.responseJSON.message;
}
const loader_key = helpers.generateGuid();
dispatch(uiActions.startLoading(loader_key, `lastfm_${method}`));
reject({
config,
error,
description,
status,
xhr,
});
},
);
function status(response) {
dispatch(uiActions.stopLoading(loader_key));
if (response.status >= 200 && response.status < 300) {
return Promise.resolve(response)
} else {
return Promise.reject(new Error(response.statusText))
}
}
fetch(url, config)
.then(status)
.then(response => response.json())
.then(data => {
resolve(data);
})
.catch(error => {
reject(error);
});
});
/**
@ -79,7 +70,7 @@ const sendRequest = (dispatch, getState, params, signed = false) => new Promise(
* @param params = string, the url params to send
* @param signed = boolean
* */
const sendSignedRequest = (dispatch, getState, params) => new Promise((resolve, reject) => {
const sendSignedRequest = (dispatch, getState, params) => new Promise((resolve, reject) => {
// Not authorized
if (!getState().lastfm.authorization) {
reject({
@ -227,7 +218,7 @@ export function getTrack(uri) {
};
}
export function getArtist(uri, artist, mbid = false) {
export function getArtist(uri, artist, mbid = false) {
return (dispatch, getState) => {
if (mbid) {
var params = `method=artist.getInfo&mbid=${mbid}`;
@ -271,7 +262,7 @@ export function getAlbum(uri, artist, album, mbid = false) {
sendRequest(dispatch, getState, params)
.then(
(response) => {
if (response.album) {
if (response.album) {
const existing_album = getState().core.albums[uri];
const album = {
@ -303,10 +294,10 @@ export function getAlbum(uri, artist, album, mbid = false) {
export function getImages(context, uri) {
return (dispatch, getState) => {
return (dispatch, getState) => {
let record = getState().core[context][uri];
if (record) {
switch (context) {
switch (context) {
case 'tracks':
if (record.mbid) {