Add most recent albums
This commit is contained in:
@@ -2,7 +2,7 @@ import { configureStore, getDefaultMiddleware, combineReducers } from '@reduxjs/
|
||||
import { useSelector, TypedUseSelectorHook } from 'react-redux';
|
||||
import AsyncStorage from '@react-native-community/async-storage';
|
||||
import { persistStore, persistReducer } from 'redux-persist';
|
||||
import logger from 'redux-logger';
|
||||
// import logger from 'redux-logger';
|
||||
|
||||
const persistConfig = {
|
||||
key: 'root',
|
||||
@@ -22,7 +22,7 @@ const persistedReducer = persistReducer(persistConfig, reducers);
|
||||
const store = configureStore({
|
||||
reducer: persistedReducer,
|
||||
middleware: getDefaultMiddleware({ serializableCheck: false, immutableCheck: false }).concat(
|
||||
logger
|
||||
// logger
|
||||
),
|
||||
});
|
||||
|
||||
|
||||
@@ -1,31 +1,46 @@
|
||||
import { createAsyncThunk, createEntityAdapter } from '@reduxjs/toolkit';
|
||||
import { Album, AlbumTrack } from './types';
|
||||
import { AsyncThunkAPI } from '..';
|
||||
import { retrieveAlbums, retrieveAlbumTracks } from 'utility/JellyfinApi';
|
||||
import { retrieveAlbums, retrieveAlbumTracks, retrieveRecentAlbums } from 'utility/JellyfinApi';
|
||||
|
||||
export const albumAdapter = createEntityAdapter<Album>({
|
||||
selectId: album => album.Id,
|
||||
sortComparer: (a, b) => a.Name.localeCompare(b.Name),
|
||||
});
|
||||
|
||||
/**
|
||||
* Fetch all albums available on the jellyfin server
|
||||
*/
|
||||
export const fetchAllAlbums = createAsyncThunk<Album[], undefined, AsyncThunkAPI>(
|
||||
'/albums/all',
|
||||
async (empty, thunkAPI) => {
|
||||
console.log('RETRIEVING ALBUMS');
|
||||
const credentials = thunkAPI.getState().settings.jellyfin;
|
||||
return retrieveAlbums(credentials) as Promise<Album[]>;
|
||||
}
|
||||
);
|
||||
|
||||
/**
|
||||
* Retrieve the most recent albums
|
||||
*/
|
||||
export const fetchRecentAlbums = createAsyncThunk<Album[], number | undefined, AsyncThunkAPI>(
|
||||
'/albums/recent',
|
||||
async (numberOfAlbums, thunkAPI) => {
|
||||
const credentials = thunkAPI.getState().settings.jellyfin;
|
||||
return retrieveRecentAlbums(credentials, numberOfAlbums) as Promise<Album[]>;
|
||||
}
|
||||
);
|
||||
|
||||
export const trackAdapter = createEntityAdapter<AlbumTrack>({
|
||||
selectId: track => track.Id,
|
||||
sortComparer: (a, b) => a.IndexNumber - b.IndexNumber,
|
||||
});
|
||||
|
||||
/**
|
||||
* Retrieve all tracks from a particular album
|
||||
*/
|
||||
export const fetchTracksByAlbum = createAsyncThunk<AlbumTrack[], string, AsyncThunkAPI>(
|
||||
'/tracks/byAlbum',
|
||||
async (ItemId, thunkAPI) => {
|
||||
console.log('RETRIEVING ALBUMS');
|
||||
const credentials = thunkAPI.getState().settings.jellyfin;
|
||||
return retrieveAlbumTracks(ItemId, credentials) as Promise<AlbumTrack[]>;
|
||||
}
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import { fetchAllAlbums, albumAdapter, fetchTracksByAlbum, trackAdapter } from './actions';
|
||||
import { fetchAllAlbums, albumAdapter, fetchTracksByAlbum, trackAdapter, fetchRecentAlbums } from './actions';
|
||||
import { createSlice, Dictionary, EntityId } from '@reduxjs/toolkit';
|
||||
import { Album, AlbumTrack } from './types';
|
||||
|
||||
@@ -32,6 +32,9 @@ const music = createSlice({
|
||||
initialState,
|
||||
reducers: {},
|
||||
extraReducers: builder => {
|
||||
/**
|
||||
* Fetch All albums
|
||||
*/
|
||||
builder.addCase(fetchAllAlbums.fulfilled, (state, { payload }) => {
|
||||
albumAdapter.setAll(state.albums, payload);
|
||||
state.albums.isLoading = false;
|
||||
@@ -39,6 +42,20 @@ const music = createSlice({
|
||||
});
|
||||
builder.addCase(fetchAllAlbums.pending, (state) => { state.albums.isLoading = true; });
|
||||
builder.addCase(fetchAllAlbums.rejected, (state) => { state.albums.isLoading = false; });
|
||||
|
||||
/**
|
||||
* 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
|
||||
*/
|
||||
builder.addCase(fetchTracksByAlbum.fulfilled, (state, { payload }) => {
|
||||
trackAdapter.setAll(state.tracks, payload);
|
||||
|
||||
|
||||
42
src/store/music/selectors.ts
Normal file
42
src/store/music/selectors.ts
Normal file
@@ -0,0 +1,42 @@
|
||||
import { useTypedSelector } from 'store';
|
||||
import { parseISO } from 'date-fns';
|
||||
import { Album } from './types';
|
||||
|
||||
/**
|
||||
* Retrieves a list of the n most recent albums
|
||||
*/
|
||||
export function useRecentAlbums(amount: number) {
|
||||
const albums = useTypedSelector((state) => state.music.albums.entities);
|
||||
const albumIds = useTypedSelector((state) => state.music.albums.ids);
|
||||
|
||||
const sorted = [...albumIds].sort((a, b) => {
|
||||
const albumA = albums[a];
|
||||
const albumB = albums[b];
|
||||
const dateA = albumA ? parseISO(albumA.DateCreated).getTime() : 0;
|
||||
const dateB = albumB ? parseISO(albumB.DateCreated).getTime() : 0;
|
||||
return dateB - dateA;
|
||||
});
|
||||
|
||||
return sorted.slice(0, amount);
|
||||
}
|
||||
|
||||
export function useAlbumsByArtist() {
|
||||
const albums = useTypedSelector((state) => state.music.albums.entities);
|
||||
const albumIds = useTypedSelector((state) => state.music.albums.ids);
|
||||
|
||||
const sorted = [...albumIds].sort((a, b) => {
|
||||
const albumA = albums[a];
|
||||
const albumB = albums[b];
|
||||
if ((!albumA && !albumB) || (!albumA?.AlbumArtist && !albumB?.AlbumArtist)) {
|
||||
return 0;
|
||||
} else if (!albumA || !albumA.AlbumArtist) {
|
||||
return 1;
|
||||
} else if (!albumB || !albumB.AlbumArtist) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
return albumA.AlbumArtist.localeCompare(albumB.AlbumArtist);
|
||||
});
|
||||
|
||||
return sorted;
|
||||
}
|
||||
@@ -35,13 +35,14 @@ export interface Album {
|
||||
PrimaryImageAspectRatio: number;
|
||||
Artists: string[];
|
||||
ArtistItems: ArtistItem[];
|
||||
AlbumArtist: string;
|
||||
AlbumArtist?: string;
|
||||
AlbumArtists: AlbumArtist[];
|
||||
ImageTags: ImageTags;
|
||||
BackdropImageTags: any[];
|
||||
LocationType: string;
|
||||
Tracks?: string[];
|
||||
lastRefreshed?: number;
|
||||
DateCreated: string;
|
||||
}
|
||||
|
||||
export interface AlbumTrack {
|
||||
|
||||
Reference in New Issue
Block a user