Skip to content

Commit

Permalink
Merge pull request #14 from oogunjob/fixing-select-all
Browse files Browse the repository at this point in the history
Fix for Select All in Playlists Component
  • Loading branch information
oogunjob authored Jan 27, 2024
2 parents bd4d8e6 + c68bb14 commit d9151e0
Showing 1 changed file with 15 additions and 13 deletions.
28 changes: 15 additions & 13 deletions src/app/components/playlists.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,9 @@ import DefaultButton from "./buttons/defaultButton";

interface Playlist {
key: number;
// Add other properties based on your actual playlist object structure
// For example:
name: string;
// ... other properties
}

// TODO: There's currently a bug where if the playlists have the same name, they will be considered the same playlist
// This is because the playlists are stored in a set, which doesn't allow duplicates
// Need to use Ids instead of names

function Playlists({
provider,
selectedPlaylists,
Expand Down Expand Up @@ -55,14 +48,23 @@ function Playlists({
}
};

// Toggle the playlist option to be selected or not
const handleToggleOption = (playlist: Playlist) => {
if (selectedPlaylists.find((selected) => selected.key === playlist.key)) {
// Option is selected, unselect it
setSelectedPlaylists(selectedPlaylists.filter((selected) => selected.key !== playlist.key));
} else {
// Option is not selected, select it
// Select the option if it hasn't been selected yet
if (!isSelected(playlist)) {
setSelectedPlaylists([...selectedPlaylists, playlist]);
}
// Deselect the option if it has been selected
else {
setSelectedPlaylists(selectedPlaylists.filter((selected) => selected.key !== playlist.key));
}

console.log(selectedPlaylists);
};

// Check if the playlist is selected
const isSelected = (playlist: Playlist) => {
return selectedPlaylists.map(selected => selected.key).includes(playlist.key);
};

if (!isLoaded) {
Expand Down Expand Up @@ -90,7 +92,7 @@ function Playlists({
{playlists.map((playlist, index) => (
<div
key={index}
className={`w-[120px] h-[120px] rounded-lg hover:bg-blue-200 flex items-center justify-center ${selectedPlaylists.includes(playlist) ? "bg-blue-200" : ""}`}
className={`w-[120px] h-[120px] rounded-lg hover:bg-blue-200 flex items-center justify-center ${isSelected(playlist) ? "bg-blue-200" : ""}`}
>
<div onClick={() => handleToggleOption(playlist)} className={`flex flex-col items-center justify-center cursor-pointer `}>
<div className={`flex items-center justify-center w-20 h-20 bg-white rounded-[10px] shadow`}>
Expand Down

0 comments on commit d9151e0

Please sign in to comment.