Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix for Select All in Playlists Component #14

Merged
merged 1 commit into from
Jan 27, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading