Files
jellyfin-audio-player/src/store/music/index.ts

149 lines
5.0 KiB
TypeScript
Raw Normal View History

2022-01-01 19:09:21 +01:00
import {
fetchAllAlbums,
albumAdapter,
fetchTracksByAlbum,
trackAdapter,
fetchRecentAlbums,
searchAndFetchAlbums,
playlistAdapter,
fetchAllPlaylists,
fetchTracksByPlaylist
} from './actions';
2020-06-20 23:10:19 +02:00
import { createSlice, Dictionary, EntityId } from '@reduxjs/toolkit';
2022-01-01 19:09:21 +01:00
import { Album, AlbumTrack, Playlist } from './types';
import { setJellyfinCredentials } from 'store/settings/actions';
2020-06-17 14:58:04 +02:00
2020-06-20 23:10:19 +02:00
export interface State {
albums: {
isLoading: boolean;
entities: Dictionary<Album>;
ids: EntityId[];
2022-01-01 19:09:21 +01:00
lastRefreshed?: number,
2020-06-20 23:10:19 +02:00
},
tracks: {
isLoading: boolean;
entities: Dictionary<AlbumTrack>;
ids: EntityId[];
2022-01-01 19:09:21 +01:00
byAlbum: Dictionary<EntityId[]>;
byPlaylist: Dictionary<EntityId[]>;
2020-06-20 23:10:19 +02:00
},
2022-01-01 19:09:21 +01:00
playlists: {
isLoading: boolean;
entities: Dictionary<Playlist>;
ids: EntityId[];
lastRefreshed?: number,
}
2020-06-20 23:10:19 +02:00
}
2022-01-03 09:07:30 +01:00
export const initialState: State = {
2020-06-17 14:58:04 +02:00
albums: {
...albumAdapter.getInitialState(),
isLoading: false,
},
tracks: {
...trackAdapter.getInitialState(),
isLoading: false,
2022-01-01 19:09:21 +01:00
byAlbum: {},
byPlaylist: {},
2020-06-17 14:58:04 +02:00
},
2022-01-01 19:09:21 +01:00
playlists: {
...playlistAdapter.getInitialState(),
isLoading: false,
}
2020-06-17 14:58:04 +02:00
};
const music = createSlice({
name: 'music',
initialState,
reducers: {
reset: () => initialState,
},
2020-06-17 14:58:04 +02:00
extraReducers: builder => {
2020-06-21 13:02:23 +02:00
/**
* Fetch All albums
*/
2020-06-17 14:58:04 +02:00
builder.addCase(fetchAllAlbums.fulfilled, (state, { payload }) => {
albumAdapter.setAll(state.albums, payload);
state.albums.isLoading = false;
2022-01-01 19:09:21 +01:00
state.albums.lastRefreshed = new Date().getTime();
2020-06-17 14:58:04 +02:00
});
builder.addCase(fetchAllAlbums.pending, (state) => { state.albums.isLoading = true; });
builder.addCase(fetchAllAlbums.rejected, (state) => { state.albums.isLoading = false; });
2020-06-21 13:02:23 +02:00
/**
* Fetch most recent albums
*/
builder.addCase(fetchRecentAlbums.fulfilled, (state, { payload }) => {
albumAdapter.upsertMany(state.albums, payload);
state.albums.isLoading = false;
});
builder.addCase(fetchRecentAlbums.pending, (state) => { state.albums.isLoading = true; });
builder.addCase(fetchRecentAlbums.rejected, (state) => { state.albums.isLoading = false; });
/**
* Fetch tracks by album
*/
2022-01-01 19:09:21 +01:00
builder.addCase(fetchTracksByAlbum.fulfilled, (state, { payload, meta }) => {
if (!payload.length) {
return;
}
trackAdapter.upsertMany(state.tracks, payload);
2020-06-17 14:58:04 +02:00
// Also store all the track ids in the album
2022-01-01 19:09:21 +01:00
state.tracks.byAlbum[meta.arg] = payload.map(d => d.Id);
const album = state.albums.entities[meta.arg];
2020-06-17 14:58:04 +02:00
if (album) {
album.lastRefreshed = new Date().getTime();
2020-06-17 14:58:04 +02:00
}
state.tracks.isLoading = false;
});
builder.addCase(fetchTracksByAlbum.pending, (state) => { state.tracks.isLoading = true; });
builder.addCase(fetchTracksByAlbum.rejected, (state) => { state.tracks.isLoading = false; });
2021-04-24 15:30:07 +02:00
builder.addCase(searchAndFetchAlbums.pending, (state) => { state.albums.isLoading = true; });
2021-04-24 14:50:43 +02:00
builder.addCase(searchAndFetchAlbums.fulfilled, (state, { payload }) => {
albumAdapter.upsertMany(state.albums, payload.albums);
2021-04-24 15:30:07 +02:00
state.albums.isLoading = false;
2021-04-24 14:50:43 +02:00
});
2022-01-01 19:09:21 +01:00
/**
* Fetch all playlists
*/
builder.addCase(fetchAllPlaylists.fulfilled, (state, { payload }) => {
playlistAdapter.setAll(state.playlists, payload);
state.playlists.isLoading = false;
state.playlists.lastRefreshed = new Date().getTime();
});
builder.addCase(fetchAllPlaylists.pending, (state) => { state.playlists.isLoading = true; });
builder.addCase(fetchAllPlaylists.rejected, (state) => { state.playlists.isLoading = false; });
/**
* Fetch tracks by playlist
*/
builder.addCase(fetchTracksByPlaylist.fulfilled, (state, { payload, meta }) => {
if (!payload.length) {
return;
}
// Upsert the retrieved tracks
trackAdapter.upsertMany(state.tracks, payload);
// Also store all the track ids in the playlist
state.tracks.byPlaylist[meta.arg] = payload.map(d => d.Id);
state.tracks.isLoading = false;
const playlist = state.playlists.entities[meta.arg];
if (playlist) {
playlist.lastRefreshed = new Date().getTime();
}
});
builder.addCase(fetchTracksByPlaylist.pending, (state) => { state.tracks.isLoading = true; });
builder.addCase(fetchTracksByPlaylist.rejected, (state) => { state.tracks.isLoading = false; });
2021-04-24 14:50:43 +02:00
// Reset any caches we have when a new server is set
builder.addCase(setJellyfinCredentials, () => initialState);
2020-06-17 14:58:04 +02:00
}
});
export default music;