Skip to content

Commit 80976e9

Browse files
committed
refactor: improve song item click logic
1 parent 972a70b commit 80976e9

File tree

3 files changed

+62
-59
lines changed

3 files changed

+62
-59
lines changed

src/API.ts

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -52,10 +52,7 @@ export const GetUserPlaylists = async (
5252
Authorization: `Bearer ${auth}`,
5353
},
5454
})
55-
.then((response) => {
56-
console.log(response.data);
57-
return response.data;
58-
});
55+
.then((response) => response.data);
5956
};
6057

6158
export const GetPlaylistDetail = async (

src/pages/PlaylistDetail/PlaylistDetail.tsx

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,10 @@ import Loader from '../../components/Loader/Loader';
66
import NotFound from '../../components/NotFound/NotFound';
77
import { useAppDispatch, useAppSelector } from '../../store/hooks';
88
import { selectCurrentSong } from '../../store/reducers/currentSong.slice';
9-
import { loadSong } from '../../store/reducers/currentSong.slice';
109
import {
1110
fetchPlaylistById,
1211
playlistDetailsSelector,
1312
} from '../../store/reducers/playlistDetail.slice';
14-
import type { Track } from '../../types/track.interface';
1513
import msToMinutesAndSeconds from '../../utils/msToMinutes';
1614
import styles from './PlaylistDetail.module.scss';
1715
import SongItem from './SongItem/SongItem';
@@ -51,12 +49,6 @@ const PlaylistDetail = () => {
5149
}
5250
}, []);
5351

54-
const songClicked = (clickedSong: Track): void => {
55-
if (clickedSong.track?.preview_url !== '') {
56-
dispatch(loadSong(clickedSong));
57-
}
58-
};
59-
6052
const getPlaylistDuration = (): string => {
6153
let totalMS = 0;
6254
if (playlist != null) {
@@ -126,7 +118,6 @@ const PlaylistDetail = () => {
126118
song={item}
127119
index={index}
128120
current={item.track.id === song?.track?.id}
129-
songClicked={() => songClicked(item)}
130121
/>
131122
),
132123
)}
Lines changed: 61 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
import Play from '../../../assets/play.svg?react';
2+
import { useAppDispatch } from '../../../store/hooks';
3+
import { loadSong } from '../../../store/reducers/currentSong.slice';
24
import type { Track } from '../../../types/track.interface';
35
import formatDate from '../../../utils/formatDate';
46
import msToMinutesAndSeconds from '../../../utils/msToMinutes';
@@ -8,60 +10,73 @@ interface SongItemPros {
810
current: boolean;
911
index: number;
1012
song: Track;
11-
songClicked: () => void;
1213
}
1314

14-
const SongItem = ({ song, index, songClicked, current }: SongItemPros) =>
15-
song.track && (
16-
<div
17-
className={[
18-
styles.Item,
19-
song.track.preview_url !== null ? styles.Enabled : styles.Disabled,
20-
].join(' ')}
21-
// biome-ignore lint/a11y/useSemanticElements: clickable div is fine
22-
role="button"
23-
tabIndex={0}
24-
onClick={songClicked}
25-
onKeyDown={(e) => {
26-
if (e.key === 'Enter') {
27-
songClicked();
28-
}
29-
}}
30-
>
31-
<div className={styles.Index}>
32-
<span className={current ? 'playing' : ''}>{index + 1}</span>
33-
<Play className={styles.IndexPlay} />
34-
</div>
15+
const SongItem = ({ song, index, current }: SongItemPros) => {
16+
const dispatch = useAppDispatch();
17+
const previewAvailable = song.track?.preview_url !== null;
18+
19+
const handleSongClick = (): void => {
20+
if (previewAvailable) {
21+
dispatch(loadSong(song));
22+
}
23+
};
24+
25+
return (
26+
song.track && (
27+
<div
28+
className={[
29+
styles.Item,
30+
previewAvailable ? styles.Enabled : styles.Disabled,
31+
].join(' ')}
32+
// biome-ignore lint/a11y/useSemanticElements: clickable div is fine
33+
role="button"
34+
tabIndex={0}
35+
onClick={handleSongClick}
36+
onKeyDown={(e) => {
37+
if (e.key === 'Enter') {
38+
handleSongClick();
39+
}
40+
}}
41+
>
42+
<div className={styles.Index}>
43+
<span className={current ? 'playing' : ''}>{index + 1}</span>
44+
<Play className={styles.IndexPlay} />
45+
</div>
3546

36-
<div className={styles.Title}>
37-
{song.track.album.images?.[0]?.url && (
38-
<img src={song.track.album.images[0].url} alt="cover img" />
39-
)}
40-
<div className={styles.NameContainer}>
41-
<div className={styles.Name}>
42-
<span className={current ? 'playing' : ''}>{song.track.name}</span>
47+
<div className={styles.Title}>
48+
{song.track.album.images?.[0]?.url && (
49+
<img src={song.track.album.images[0].url} alt="cover img" />
50+
)}
51+
<div className={styles.NameContainer}>
52+
<div className={styles.Name}>
53+
<span className={current ? 'playing' : ''}>
54+
{song.track.name}
55+
</span>
56+
</div>
57+
{song.track.explicit && <span className={styles.Explicit}>e</span>}
58+
<span
59+
className={[
60+
styles.Artist,
61+
song.track.explicit ? styles.Artist_sub : styles.Artist_badge,
62+
].join(', ')}
63+
>
64+
{song.track.artists[0].name}
65+
</span>
4366
</div>
44-
{song.track.explicit && <span className={styles.Explicit}>e</span>}
45-
<span
46-
className={[
47-
styles.Artist,
48-
song.track.explicit ? styles.Artist_sub : styles.Artist_badge,
49-
].join(', ')}
50-
>
51-
{song.track.artists[0].name}
52-
</span>
5367
</div>
54-
</div>
55-
<div>{song.track.album.name}</div>
56-
<div>{formatDate(song.added_at)}</div>
57-
<div className={styles.Length}>
58-
{msToMinutesAndSeconds(song.track.duration_ms)}
59-
{/* should be in another column */}
60-
{/* <button type="button" className={styles.More} tabIndex={-1}>
68+
<div>{song.track.album.name}</div>
69+
<div>{formatDate(song.added_at)}</div>
70+
<div className={styles.Length}>
71+
{msToMinutesAndSeconds(song.track.duration_ms)}
72+
{/* should be in another column */}
73+
{/* <button type="button" className={styles.More} tabIndex={-1}>
6174
...
6275
</button> */}
76+
</div>
6377
</div>
64-
</div>
78+
)
6579
);
80+
};
6681

6782
export default SongItem;

0 commit comments

Comments
 (0)