From 596c61f065e89547c8bfb3f2f9bed88af4acded1 Mon Sep 17 00:00:00 2001 From: jeffvli Date: Thu, 14 Jul 2022 23:25:17 -0700 Subject: [PATCH] Add is not favorite filter (#357) --- src/components/library/AdvancedFilters.tsx | 15 +++++++++++++++ src/hooks/useAdvancedFilter.ts | 18 +++++++++++++----- src/redux/viewSlice.ts | 8 +++++++- 3 files changed, 35 insertions(+), 6 deletions(-) diff --git a/src/components/library/AdvancedFilters.tsx b/src/components/library/AdvancedFilters.tsx index 4d5f81b..3a04d43 100644 --- a/src/components/library/AdvancedFilters.tsx +++ b/src/components/library/AdvancedFilters.tsx @@ -176,6 +176,21 @@ const AdvancedFilters = ({ filteredData, originalData, filter, setAdvancedFilter > Is favorite + { + dispatch( + setAdvancedFilters({ + listType: Item.Album, + filter: 'notStarred', + value: e, + }) + ); + }} + > + Is not favorite + diff --git a/src/hooks/useAdvancedFilter.ts b/src/hooks/useAdvancedFilter.ts index f7bf364..ae6565a 100644 --- a/src/hooks/useAdvancedFilter.ts +++ b/src/hooks/useAdvancedFilter.ts @@ -22,11 +22,19 @@ const useAdvancedFilter = (data: any[], filters: AdvancedFilters) => { }) : data; + const filteredByNotStarred = filterProps.properties.notStarred + ? (data || []).filter((entry) => { + return entry.starred === null || entry.starred === undefined; + }) + : data; + + const starFilter = filterProps.properties.starred ? filteredByStarred : filteredByNotStarred; + // Genre filter const genreRegex = new RegExp(filterProps.properties?.genre?.list.join('|'), 'i'); const filteredByGenres = filterProps.properties.genre.list.length > 0 - ? (filteredByStarred || []).filter((entry) => { + ? (starFilter || []).filter((entry) => { const entryGenres = _.map(entry.genre, 'title'); if (filterProps.properties.genre.type === 'or') { @@ -42,7 +50,7 @@ const useAdvancedFilter = (data: any[], filters: AdvancedFilters) => { return matches.length === filterProps.properties.genre.list.length; }) - : filteredByStarred; + : starFilter; const artistRegex = new RegExp(filterProps.properties?.artist?.list.join('|'), 'i'); @@ -69,7 +77,7 @@ const useAdvancedFilter = (data: any[], filters: AdvancedFilters) => { // Instead of filtering from the previous (genre), start from the starred filter const filteredByArtistsBase = filterProps.properties.artist.list.length > 0 - ? (filteredByStarred || []).filter((entry) => { + ? (starFilter || []).filter((entry) => { const entryArtistIds = _.map(entry.artist, 'id'); if (filterProps.properties.artist.type === 'or') { @@ -85,7 +93,7 @@ const useAdvancedFilter = (data: any[], filters: AdvancedFilters) => { return matches.length === filterProps.properties.artist.list.length; }) - : filteredByStarred; + : starFilter; const filteredByYear = !( filterProps.properties.year.from === 0 && filterProps.properties.year.to === 0 @@ -110,7 +118,7 @@ const useAdvancedFilter = (data: any[], filters: AdvancedFilters) => { }) : filteredByArtists; - setByStarredData(_.compact(_.uniqBy(filteredByStarred, 'uniqueId'))); + setByStarredData(_.compact(_.uniqBy(starFilter, 'uniqueId'))); setByGenreData(_.compact(_.uniqBy(filteredByGenres, 'uniqueId'))); setByArtistData(_.compact(_.uniqBy(filteredByArtists, 'uniqueId'))); setByArtistBaseData(_.compact(_.uniqBy(filteredByArtistsBase, 'uniqueId'))); diff --git a/src/redux/viewSlice.ts b/src/redux/viewSlice.ts index 44aa25b..4c0db52 100644 --- a/src/redux/viewSlice.ts +++ b/src/redux/viewSlice.ts @@ -10,6 +10,7 @@ export interface AdvancedFilters { nav: 'filters' | 'sort'; properties: { starred: boolean; + notStarred: boolean; genre: { list: any[]; type: 'and' | 'or'; @@ -55,6 +56,7 @@ const initialState: View = { nav: 'filters', properties: { starred: false, + notStarred: false, genre: { list: [], type: 'and', @@ -109,7 +111,7 @@ const viewSlice = createSlice({ state, action: PayloadAction<{ listType: Item; - filter: 'enabled' | 'starred' | 'genre' | 'artist' | 'year' | 'nav'; + filter: 'enabled' | 'starred' | 'notStarred' | 'genre' | 'artist' | 'year' | 'nav'; value: any; }> ) => { @@ -122,6 +124,10 @@ const viewSlice = createSlice({ state.album.advancedFilters.properties.starred = action.payload.value; } + if (action.payload.filter === 'notStarred') { + state.album.advancedFilters.properties.notStarred = action.payload.value; + } + if (action.payload.filter === 'genre') { state.album.advancedFilters.properties.genre = action.payload.value; }