Adding as an index reference where index uris change

This commit is contained in:
James Barnsley
2019-02-01 10:19:21 +13:00
parent 1406985662
commit 9afbaa5b03
8 changed files with 131 additions and 72 deletions

View File

@ -213,6 +213,15 @@ export function loadedMore(parent_type, parent_key, records_type, records_data){
}
}
export function removeFromIndex(index_name, key, new_key = null){
return {
type: 'REMOVE_FROM_INDEX',
index_name: index_name,
key: key,
new_key: new_key
}
}
/**

View File

@ -233,24 +233,6 @@ const CoreMiddleware = (function(){
* Playlist manipulation
**/
case 'PLAYLIST_KEY_UPDATED':
var playlists = Object.assign({}, core.playlists);
if (playlists[action.key] === undefined){
dispatch(coreActions.handleException("Cannot change key of playlist not in index"));
}
// Delete our old playlist by key, and add by new key
var playlist = Object.assign({}, playlists[action.key]);
delete playlists[action.key];
playlists[action.new_key] = playlist;
store.dispatch({
type: 'UPDATE_PLAYLISTS_INDEX',
playlists: playlists
});
break;
case 'PLAYLIST_TRACKS':
var tracks = helpers.formatTracks(action.tracks);
action.tracks_uris = helpers.arrayOf('uri', tracks);

View File

@ -152,6 +152,31 @@ export default function reducer(core = {}, action){
/**
* Remove an item from an index
**/
case 'REMOVE_FROM_INDEX':
var index = Object.assign({}, core[action.index_name]);
// We have a new key to redirect to
if (action.new_key){
index[action.key] = {
moved_to: action.new_key
}
// No redirection, so just a clean delete
} else {
delete index[action.key];
}
var updated_core = {};
updated_core[action.index_name] = index;
return Object.assign({}, core, updated_core);
/**
* Playlists

View File

@ -1655,30 +1655,39 @@ const MopidyMiddleware = (function(){
break
case 'MOPIDY_SAVE_PLAYLIST':
var uri = action.key;
// Even though we have the full playlist in our index, our "playlists.save" request
// requires a Mopidy playlist object (with updates)
request(socket, store, 'playlists.lookup', { uri: action.key })
.then(response => {
var playlist = Object.assign({}, response, { name: action.name })
request(socket, store, 'playlists.save', { playlist: playlist })
var mopidy_playlist = Object.assign({}, response, { name: action.name });
request(socket, store, 'playlists.save', { playlist: mopidy_playlist })
.then(response => {
store.dispatch(coreActions.playlistLoaded(playlist));
// Overwrite our playlist with the response to our save
// This is essential to get the updated URI from Mopidy
var playlist = Object.assign(
{},
store.getState().core.playlists[action.key],
{
uri: response.uri,
name: response.name
}
);
// When we rename a playlist, the URI also changes to reflect the name change
// We need to update our index, as well as redirect our current page URL
if (action.key != response.uri){
store.dispatch({
type: 'PLAYLIST_KEY_UPDATED',
key: action.key,
new_key: response.uri
});
// When we rename a playlist, the URI also changes to reflect the name change.
// We need to update our index, as well as redirect our current page URL.
if (action.key !== playlist.uri){
console.log("Updated playlist key");
// TODO: Somehow need to push new URL. This is only needed to prevent issues
// when the user refreshes their browser (as the URI is now old)
// history.push('/playlist/'+encodeURIComponent(response.uri));
// Remove old playlist (by old key/uri) from index
// By providing the new key, the old playlist gets replaced with a redirector object
store.dispatch(coreActions.removeFromIndex('playlists', action.key, playlist.uri));
}
store.dispatch(coreActions.playlistLoaded(playlist));
store.dispatch(uiActions.createNotification({type: 'info', content: 'Playlist saved'}));
})
});

View File

@ -47,6 +47,12 @@ class Playlist extends React.Component{
}
componentWillReceiveProps(nextProps){
// Follow a URI moved_to instruction
if (this.props.playlist && nextProps.playlist && this.props.playlist.moved_to != nextProps.playlist.moved_to){
this.props.history.push('/playlist/'+encodeURIComponent(nextProps.playlist.moved_to));
}
if (nextProps.uri != this.props.uri){
this.props.coreActions.loadPlaylist(nextProps.uri);
} else if (!this.props.mopidy_connected && nextProps.mopidy_connected){