move
This commit is contained in:
123
src/store/modules/artists/actions.js
Normal file
123
src/store/modules/artists/actions.js
Normal file
@@ -0,0 +1,123 @@
|
||||
import axios from 'axios'
|
||||
import router from '../../../router'
|
||||
|
||||
export default {
|
||||
clear(context) {
|
||||
context.commit("clear");
|
||||
},
|
||||
filter(context, term) {
|
||||
return new Promise((resolve) => {
|
||||
axios.get(context.rootGetters.server + "/api/artists/filter/" + term, context.rootGetters.headers).then(res => {
|
||||
resolve(res.data);
|
||||
});
|
||||
})
|
||||
},
|
||||
loadArtists(context, force) {
|
||||
if ((!context.state.eos || force) && !context.state.loading) {
|
||||
context.state.loading = true;
|
||||
axios.get(context.rootGetters.server + "/api/artists/page/" + context.state.page++, context.rootGetters.headers).then((res) => {
|
||||
context.commit("setArtists", res.data);
|
||||
});
|
||||
}
|
||||
},
|
||||
loadArtist(context, id) {
|
||||
context.state.loading = true;
|
||||
return new Promise((resolve) => {
|
||||
axios.get(context.rootGetters.server + "/api/artists/" + id, context.rootGetters.headers).then((res) => {
|
||||
if (res.data != "") {
|
||||
context.commit("setArtists", [res.data]);
|
||||
} else {
|
||||
context.state.loading = false;
|
||||
}
|
||||
resolve(res.data);
|
||||
});
|
||||
});
|
||||
},
|
||||
loadFavourites(context) {
|
||||
axios.get(context.rootGetters.server + "/api/artists/favourites", context.rootGetters.headers).then(res => {
|
||||
if (res.data.length > 0) {
|
||||
context.commit("setArtists", res.data);
|
||||
}
|
||||
});
|
||||
},
|
||||
selectArtist(context, artist) {
|
||||
if (artist.albums.length == 0) {
|
||||
context.dispatch("loadArtist", artist._id)
|
||||
}
|
||||
context.commit('selectArtist', artist);
|
||||
if (context.rootGetters.routerQuery.id != artist._id) {
|
||||
let url = "/artists?id=" + artist._id;
|
||||
let track = context.rootGetters["tracks/selectedTrack"];
|
||||
if (track._id && track.parentType == "artist" && track.parent.parent._id == artist._id) {
|
||||
url += "&play=" + track._id
|
||||
}
|
||||
router.push(url);
|
||||
}
|
||||
|
||||
// PRELOAD NEXT AND PREV ARTIST
|
||||
let next = context.getters.nextArtist;
|
||||
if (next._id && next.albums.length == 0) {
|
||||
context.dispatch("loadArtist", next._id);
|
||||
}
|
||||
let prev = context.getters.prevArtist;
|
||||
if (prev._id && prev.albums.length == 0) {
|
||||
context.dispatch("loadArtist", prev._id);
|
||||
}
|
||||
},
|
||||
selectArtistById(context, id) {
|
||||
let artist = context.getters.collection.find(item => item._id == id);
|
||||
if (artist && artist._id != context.getters.selectedArtist._id) {
|
||||
context.dispatch("selectArtist", artist);
|
||||
}
|
||||
},
|
||||
resetSelectedArtist(context) {
|
||||
context.commit("resetSelectedArtist");
|
||||
router.push("/artists");
|
||||
},
|
||||
move(context, payload) {
|
||||
return new Promise((resolve) => {
|
||||
axios.put(context.rootGetters.server + "/api/artists/" + payload.source + "/move", payload, context.rootGetters.headers).then(res => {
|
||||
resolve(res.data);
|
||||
});
|
||||
})
|
||||
},
|
||||
remove(context, id) {
|
||||
context.commit("remove", id);
|
||||
},
|
||||
gotoPrevArtist(context) {
|
||||
let prevArtist = context.getters.prevArtist;
|
||||
if (prevArtist._id) {
|
||||
context.dispatch("selectArtist", prevArtist);
|
||||
}
|
||||
},
|
||||
gotoNextArtist(context) {
|
||||
let nextArtist = context.getters.nextArtist;
|
||||
if (nextArtist._id) {
|
||||
context.dispatch("selectArtist", nextArtist);
|
||||
}
|
||||
},
|
||||
uploadNewCover(context, artist) {
|
||||
let input = document.createElement('input');
|
||||
input.type = "file";
|
||||
input.accept = "image/jpeg, image/png";
|
||||
input.addEventListener("change", function () {
|
||||
if (input.value) {
|
||||
let formData = new FormData();
|
||||
formData.append("file", input.files[0]);
|
||||
let h = context.rootGetters.headers;
|
||||
h.headers["content-type"] = "multipart/form-data";
|
||||
axios
|
||||
.put(context.rootGetters.server + "/api/artists/" + artist._id + "/cover", formData, context.rootGetters.headers)
|
||||
.then(res => {
|
||||
artist.covers = res.data;
|
||||
});
|
||||
}
|
||||
});
|
||||
input.click();
|
||||
},
|
||||
resetCover(context, artist) {
|
||||
axios.delete(context.rootGetters.server + "/api/artists/" + artist._id + "/cover", context.rootGetters.headers).then(() => {
|
||||
artist.covers = {};
|
||||
});
|
||||
},
|
||||
}
|
||||
33
src/store/modules/artists/getters.js
Normal file
33
src/store/modules/artists/getters.js
Normal file
@@ -0,0 +1,33 @@
|
||||
export default {
|
||||
collection(state) {
|
||||
return state.collection;
|
||||
},
|
||||
favourites(state, getters, rootState, rootGetters) {
|
||||
return state.collection.filter(f => rootGetters["user/favourites"].map(m => m.itemId).indexOf(f._id) > -1);
|
||||
},
|
||||
prevArtist(state) {
|
||||
let currentIndex = state.collection.indexOf(state.selectedArtist);
|
||||
let prevArtist = {};
|
||||
if (currentIndex > 0) {
|
||||
prevArtist = state.collection[currentIndex - 1];
|
||||
}
|
||||
return prevArtist;
|
||||
},
|
||||
nextArtist(state) {
|
||||
let currentIndex = state.collection.indexOf(state.selectedArtist);
|
||||
let nextArtist = {};
|
||||
if (state.collection.length > currentIndex + 1) {
|
||||
nextArtist = state.collection[currentIndex + 1];
|
||||
}
|
||||
return nextArtist;
|
||||
},
|
||||
selectedArtist(state) {
|
||||
return state.selectedArtist;
|
||||
},
|
||||
loading(state) {
|
||||
return state.loading;
|
||||
},
|
||||
eos(state) {
|
||||
return state.eos;
|
||||
}
|
||||
}
|
||||
12
src/store/modules/artists/index.js
Normal file
12
src/store/modules/artists/index.js
Normal file
@@ -0,0 +1,12 @@
|
||||
import state from './state.js';
|
||||
import getters from './getters.js';
|
||||
import mutations from './mutations.js';
|
||||
import actions from './actions.js';
|
||||
|
||||
export default {
|
||||
namespaced: true,
|
||||
state,
|
||||
getters,
|
||||
mutations,
|
||||
actions
|
||||
}
|
||||
80
src/store/modules/artists/mutations.js
Normal file
80
src/store/modules/artists/mutations.js
Normal file
@@ -0,0 +1,80 @@
|
||||
export default {
|
||||
clear(state) {
|
||||
state.collection = [];
|
||||
state.loading = false;
|
||||
state.eos = false;
|
||||
state.page = 1;
|
||||
},
|
||||
remove(state, id) {
|
||||
let artist = state.collection.find(f => f._id == id);
|
||||
if (artist) {
|
||||
let i = state.collection.indexOf(artist);
|
||||
state.collection.splice(i, 1);
|
||||
}
|
||||
},
|
||||
resetSelectedArtist(state) {
|
||||
if (state.selectedArtist._id)
|
||||
state.selectedArtist = { albums: [], tracks: [], covers: {} };
|
||||
},
|
||||
setArtists(state, artists) {
|
||||
if (artists.length == 0) {
|
||||
state.eos = true;
|
||||
state.loading = false;
|
||||
if (state.page > 1) {
|
||||
state.page--;
|
||||
}
|
||||
return;
|
||||
}
|
||||
artists.forEach(artist => {
|
||||
let existsArtist = state.collection.find(f => f._id == artist._id);
|
||||
if (!existsArtist) {
|
||||
let item = state.collection.find((item) => {
|
||||
if (item.name > artist.name) {
|
||||
return item;
|
||||
}
|
||||
})
|
||||
|
||||
if (!artist.covers) {
|
||||
artist.covers = {};
|
||||
}
|
||||
if (item) {
|
||||
let index = state.collection.indexOf(item);
|
||||
state.collection.splice(index, 0, artist);
|
||||
} else {
|
||||
state.collection.push(artist);
|
||||
}
|
||||
|
||||
artist.albums.forEach((album) => {
|
||||
album.parent = artist;
|
||||
if (!album.covers) {
|
||||
album.covers = {};
|
||||
}
|
||||
album.tracks.forEach((track) => {
|
||||
track.parent = album;
|
||||
track.parentType = "artist"
|
||||
artist.tracks.push(track);
|
||||
});
|
||||
});
|
||||
|
||||
} else if (existsArtist && artist.albums.length > 0) {
|
||||
existsArtist.tracks = [];
|
||||
existsArtist.covers = artist.covers || {};
|
||||
existsArtist.albums = artist.albums;
|
||||
existsArtist.albums.forEach((album) => {
|
||||
album.parent = existsArtist;
|
||||
album.tracks.forEach((track) => {
|
||||
track.parent = album;
|
||||
track.parentType = "artist"
|
||||
existsArtist.tracks.push(track);
|
||||
});
|
||||
});
|
||||
}
|
||||
});
|
||||
state.loading = false;
|
||||
},
|
||||
selectArtist(state, artist) {
|
||||
if (state.selectedArtist._id != artist._id) {
|
||||
state.selectedArtist = artist;
|
||||
}
|
||||
},
|
||||
}
|
||||
7
src/store/modules/artists/state.js
Normal file
7
src/store/modules/artists/state.js
Normal file
@@ -0,0 +1,7 @@
|
||||
export default {
|
||||
collection: [],
|
||||
selectedArtist: { artists: [], tracks: [], covers: {} },
|
||||
page: 1,
|
||||
loading: false,
|
||||
eos: false
|
||||
}
|
||||
Reference in New Issue
Block a user