Browse Source

Remember active tab for favorites page

master
jeffvli 3 years ago
committed by Jeff
parent
commit
dce5030ac4
  1. 10
      src/__tests__/App.test.tsx
  2. 33
      src/components/starred/StarredView.tsx
  3. 26
      src/redux/favoriteSlice.ts
  4. 2
      src/redux/store.ts

10
src/__tests__/App.test.tsx

@ -9,8 +9,9 @@ import { Player } from '../redux/playerSlice';
import { General } from '../redux/miscSlice'; import { General } from '../redux/miscSlice';
import { Playlist } from '../redux/playlistSlice'; import { Playlist } from '../redux/playlistSlice';
import { ConfigPage } from '../redux/configSlice'; import { ConfigPage } from '../redux/configSlice';
import App from '../App';
import { FolderSelection } from '../redux/folderSlice'; import { FolderSelection } from '../redux/folderSlice';
import { FavoritePage } from '../redux/favoriteSlice';
import App from '../App';
const middlewares: Middleware<Record<string, unknown>, any, Dispatch<AnyAction>>[] | undefined = []; const middlewares: Middleware<Record<string, unknown>, any, Dispatch<AnyAction>>[] | undefined = [];
const mockStore = configureMockStore(middlewares); const mockStore = configureMockStore(middlewares);
@ -109,6 +110,12 @@ const configState: ConfigPage = {
}, },
}; };
const favoriteState: FavoritePage = {
active: {
tab: 'tracks',
},
};
const mockInitialState = { const mockInitialState = {
player: playerState, player: playerState,
playQueue: playQueueState, playQueue: playQueueState,
@ -116,6 +123,7 @@ const mockInitialState = {
playlist: playlistState, playlist: playlistState,
folder: folderState, folder: folderState,
config: configState, config: configState,
favorite: favoriteState,
}; };
describe('App', () => { describe('App', () => {

33
src/components/starred/StarredView.tsx

@ -20,16 +20,15 @@ import ListViewType from '../viewtypes/ListViewType';
import GridViewType from '../viewtypes/GridViewType'; import GridViewType from '../viewtypes/GridViewType';
import { setStatus } from '../../redux/playerSlice'; import { setStatus } from '../../redux/playerSlice';
import { StyledNavItem } from '../shared/styled'; import { StyledNavItem } from '../shared/styled';
import useRouterQuery from '../../hooks/useRouterQuery'; import { setActive } from '../../redux/favoriteSlice';
const StarredView = () => { const StarredView = () => {
const history = useHistory(); const history = useHistory();
const dispatch = useAppDispatch(); const dispatch = useAppDispatch();
const query = useRouterQuery();
const queryClient = useQueryClient(); const queryClient = useQueryClient();
const multiSelect = useAppSelector((state) => state.multiSelect); const multiSelect = useAppSelector((state) => state.multiSelect);
const folder = useAppSelector((state) => state.folder); const folder = useAppSelector((state) => state.folder);
const [page, setPage] = useState(query.get('page') || 'tracks'); const favorite = useAppSelector((state) => state.favorite);
const [viewType, setViewType] = useState(settings.getSync('albumViewType') || 'list'); const [viewType, setViewType] = useState(settings.getSync('albumViewType') || 'list');
const [musicFolder, setMusicFolder] = useState(undefined); const [musicFolder, setMusicFolder] = useState(undefined);
@ -49,10 +48,14 @@ const StarredView = () => {
const [searchQuery, setSearchQuery] = useState(''); const [searchQuery, setSearchQuery] = useState('');
const filteredData = useSearchQuery( const filteredData = useSearchQuery(
searchQuery, searchQuery,
page === 'tracks' ? data?.song : page === 'albums' ? data?.album : data?.artist, favorite.active.tab === 'tracks'
page === 'tracks' ? data?.song
: favorite.active.tab === 'albums'
? data?.album
: data?.artist,
favorite.active.tab === 'tracks'
? ['title', 'artist', 'album', 'name', 'genre'] ? ['title', 'artist', 'album', 'name', 'genre']
: page === 'albums' : favorite.active.tab === 'albums'
? ['name', 'artist', 'genre', 'year'] ? ['name', 'artist', 'genre', 'year']
: ['name'] : ['name']
); );
@ -66,14 +69,14 @@ const StarredView = () => {
if (e.ctrlKey) { if (e.ctrlKey) {
dispatch(toggleSelected(rowData)); dispatch(toggleSelected(rowData));
} else if (e.shiftKey) { } else if (e.shiftKey) {
if (page === 'tracks') { if (favorite.active.tab === 'tracks') {
dispatch(setRangeSelected(rowData)); dispatch(setRangeSelected(rowData));
if (searchQuery !== '') { if (searchQuery !== '') {
dispatch(toggleRangeSelected(filteredData)); dispatch(toggleRangeSelected(filteredData));
} else { } else {
dispatch(toggleRangeSelected(data.song)); dispatch(toggleRangeSelected(data.song));
} }
} else if (page === 'albums') { } else if (favorite.active.tab === 'albums') {
dispatch(setRangeSelected(rowData)); dispatch(setRangeSelected(rowData));
dispatch(toggleRangeSelected(searchQuery !== '' ? filteredData : data?.album)); dispatch(toggleRangeSelected(searchQuery !== '' ? filteredData : data?.album));
} }
@ -87,7 +90,7 @@ const StarredView = () => {
timeout = null; timeout = null;
dispatch(clearSelected()); dispatch(clearSelected());
if (page === 'tracks') { if (favorite.active.tab === 'tracks') {
dispatch( dispatch(
setPlayQueueByRowClick({ setPlayQueueByRowClick({
entries: data.song, entries: data.song,
@ -98,7 +101,7 @@ const StarredView = () => {
); );
dispatch(setStatus('PLAYING')); dispatch(setStatus('PLAYING'));
dispatch(fixPlayer2Index()); dispatch(fixPlayer2Index());
} else if (page === 'albums') { } else if (favorite.active.tab === 'albums') {
history.push(`/library/album/${e.id}`); history.push(`/library/album/${e.id}`);
} else { } else {
history.push(`/library/artist/${e.id}`); history.push(`/library/artist/${e.id}`);
@ -138,7 +141,7 @@ const StarredView = () => {
<GenericPageHeader <GenericPageHeader
title="Favorites" title="Favorites"
subtitle={ subtitle={
<Nav activeKey={page} onSelect={(e) => setPage(e)}> <Nav activeKey={favorite.active.tab} onSelect={(e) => dispatch(setActive({ tab: e }))}>
<StyledNavItem eventKey="tracks">Tracks</StyledNavItem> <StyledNavItem eventKey="tracks">Tracks</StyledNavItem>
<StyledNavItem eventKey="albums">Albums</StyledNavItem> <StyledNavItem eventKey="albums">Albums</StyledNavItem>
<StyledNavItem eventKey="artists">Artists</StyledNavItem> <StyledNavItem eventKey="artists">Artists</StyledNavItem>
@ -147,7 +150,7 @@ const StarredView = () => {
searchQuery={searchQuery} searchQuery={searchQuery}
handleSearch={(e: any) => setSearchQuery(e)} handleSearch={(e: any) => setSearchQuery(e)}
clearSearchQuery={() => setSearchQuery('')} clearSearchQuery={() => setSearchQuery('')}
showViewTypeButtons={page !== 'tracks'} showViewTypeButtons={favorite.active.tab !== 'tracks'}
viewTypeSetting="album" viewTypeSetting="album"
showSearchBar showSearchBar
handleListClick={() => setViewType('list')} handleListClick={() => setViewType('list')}
@ -159,7 +162,7 @@ const StarredView = () => {
<PageLoader /> <PageLoader />
) : ( ) : (
<> <>
{page === 'tracks' && ( {favorite.active.tab === 'tracks' && (
<ListViewType <ListViewType
data={searchQuery !== '' ? filteredData : data.song} data={searchQuery !== '' ? filteredData : data.song}
tableColumns={settings.getSync('musicListColumns')} tableColumns={settings.getSync('musicListColumns')}
@ -183,7 +186,7 @@ const StarredView = () => {
handleFavorite={handleRowFavorite} handleFavorite={handleRowFavorite}
/> />
)} )}
{page === 'albums' && ( {favorite.active.tab === 'albums' && (
<> <>
{viewType === 'list' && ( {viewType === 'list' && (
<ListViewType <ListViewType
@ -230,7 +233,7 @@ const StarredView = () => {
)} )}
</> </>
)} )}
{page === 'artists' && ( {favorite.active.tab === 'artists' && (
<> <>
{viewType === 'list' && ( {viewType === 'list' && (
<ListViewType <ListViewType

26
src/redux/favoriteSlice.ts

@ -0,0 +1,26 @@
import { createSlice, PayloadAction } from '@reduxjs/toolkit';
export interface FavoritePage {
active: {
tab: string;
};
}
const initialState: FavoritePage = {
active: {
tab: 'tracks',
},
};
const favoriteSlice = createSlice({
name: 'favorites',
initialState,
reducers: {
setActive: (state, action: PayloadAction<{ tab: string }>) => {
state.active.tab = action.payload.tab;
},
},
});
export const { setActive } = favoriteSlice.actions;
export default favoriteSlice.reducer;

2
src/redux/store.ts

@ -7,6 +7,7 @@ import miscReducer from './miscSlice';
import playlistReducer from './playlistSlice'; import playlistReducer from './playlistSlice';
import folderReducer from './folderSlice'; import folderReducer from './folderSlice';
import configReducer from './configSlice'; import configReducer from './configSlice';
import favoriteReducer from './favoriteSlice';
export const store = configureStore<PlayQueue | any>({ export const store = configureStore<PlayQueue | any>({
reducer: { reducer: {
@ -17,6 +18,7 @@ export const store = configureStore<PlayQueue | any>({
playlist: playlistReducer, playlist: playlistReducer,
folder: folderReducer, folder: folderReducer,
config: configReducer, config: configReducer,
favorite: favoriteReducer,
}, },
middleware: [forwardToMain], middleware: [forwardToMain],
}); });

Loading…
Cancel
Save