Skip to content

Commit

Permalink
FIX: not clearing text from album search
Browse files Browse the repository at this point in the history
  • Loading branch information
rigon committed Nov 9, 2023
1 parent a7683b3 commit a69b6d9
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 8 deletions.
20 changes: 13 additions & 7 deletions src/AlbumList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,20 +27,25 @@ interface AlbumListProps {
const AlbumList: FC<AlbumListProps> = ({onClick}) => {
const { collection, album } = useParams();
const { data = [], isFetching } = useGetAlbumsQuery({collection}, {skip: collection === undefined});
const [ searchTerm, setSearchTerm ] = useState<string[]>([]);
const [ searchTerm, setSearchTerm ] = useState<string>("");

const albums = useMemo(() => searchTerm.length < 1 ? data :
data.filter(album => {
const albums = useMemo(() => {
if (searchTerm.length < 1) // Empty search, don't filter
return data;

const terms = searchTerm.toLowerCase().split(/\s+/);
return data.filter(album => {
const low = album.name.toLowerCase();
return searchTerm.reduce((acc, term) => acc && low.includes(term), true);
}), [searchTerm, data]);
return terms.reduce((acc, term) => acc && low.includes(term), true);
});
}, [searchTerm, data]);
const isEmptyList = albums.length < 1;

const onSearch = (event: React.ChangeEvent<HTMLInputElement>) => {
setSearchTerm(event.target.value.toLowerCase().split(/\s+/));
setSearchTerm(event.target.value);
};
const clearSearch = () => {
setSearchTerm([]);
setSearchTerm("");
};

const renderRow = ({ index, style }: ListChildComponentProps<AlbumType>) => {
Expand Down Expand Up @@ -79,6 +84,7 @@ const AlbumList: FC<AlbumListProps> = ({onClick}) => {
return <>
<TextField
label="Search albums"
value={searchTerm}
onChange={onSearch}
fullWidth
variant="filled"
Expand Down
2 changes: 1 addition & 1 deletion src/favoriteHook.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ const useFavorite = () => {
const { infoNotification, errorNotification } = useNotification();

const favoriteStatus = (favorites: PseudoAlbumType[]) => {
const list = favorites && Array.isArray(favorites) ? favorites : [];
const list = Array.isArray(favorites) ? favorites : [];
const isFavorite = list.length > 0;
const isFavoriteThis = isFavorite && list.some(
entry => entry.collection === favorite.collection && entry.album === favorite.album);
Expand Down

0 comments on commit a69b6d9

Please sign in to comment.