Skip to content

Commit

Permalink
display tooltip for follow button
Browse files Browse the repository at this point in the history
  • Loading branch information
francoborrelli committed Sep 9, 2024
1 parent dc51a63 commit 6b09c78
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 17 deletions.
27 changes: 13 additions & 14 deletions src/components/Actions/AddSongToLibrary.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { FC, useCallback } from 'react';
import { FC } from 'react';

import { Tooltip } from '../Tooltip';
import { useTranslation } from 'react-i18next';
Expand All @@ -18,8 +18,13 @@ const AddSongToLibrary: FC<{ id: string; onToggle: () => void; size?: number }>
}) => {
const { t } = useTranslation(['playlist']);
const dispatch = useAppDispatch();
const user = useAppSelector((state) => !!state.auth.user);

const handleAddToLibrary = () => {
if (!user) {
dispatch(uiActions.openLoginButton());
return;
}
userService.saveTracks([id]).then(() => {
message.success(t('Song added to Liked Songs'));
onToggle();
Expand All @@ -45,8 +50,13 @@ const DeleteSongFromLibrary: FC<{ id: string; onToggle: () => void; size?: numbe
}) => {
const { t } = useTranslation();
const dispatch = useAppDispatch();
const user = useAppSelector((state) => !!state.auth.user);

const handleDeleteFromLibrary = () => {
if (!user) {
dispatch(uiActions.openLoginButton());
return;
}
userService.deleteTracks([id]).then(() => {
message.success(t('Song removed from Liked Songs'));
onToggle();
Expand Down Expand Up @@ -76,20 +86,9 @@ export const AddSongToLibraryButton = ({
isSaved: boolean;
onToggle: () => void;
}) => {
const dispatch = useAppDispatch();
const user = useAppSelector((state) => !!state.auth.user);

const handleToggle = useCallback(() => {
if (!user) {
dispatch(uiActions.openLoginButton());
return;
}
onToggle();
}, [dispatch, onToggle, user]);

return isSaved ? (
<DeleteSongFromLibrary size={size} id={id} onToggle={handleToggle} />
<DeleteSongFromLibrary size={size} id={id} onToggle={onToggle} />
) : (
<AddSongToLibrary size={size} id={id} onToggle={handleToggle} />
<AddSongToLibrary size={size} id={id} onToggle={onToggle} />
);
};
1 change: 1 addition & 0 deletions src/components/Actions/ArtistActions.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ export const ArtistActionsWrapper: FC<ArtistActionsWrapperProps> = memo((props)
label: t('Follow'),
icon: <FollowIcon />,
onClick: async () => {
if (!handleUserValidation()) return;
userService.followArtists([id]).then(() => {
message.success(t('Artist followed'));
dispatch(yourLibraryActions.fetchMyArtists());
Expand Down
2 changes: 1 addition & 1 deletion src/i18n/es/playlist.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ export const playlist = {
'Removed from Your Library': 'Eliminado de Tu biblioteca',
'Add to Your Library': 'Guardar en Tu biblioteca',
'Saved to Your Library': 'Guardado en Tu biblioteca',
'Add to Liked Songs': 'Agregar a Canciones Me Gusta',
'Add to Liked Songs': 'Agregar a Tus me gusta',
'Remove from Liked Songs': 'Eliminar de Canciones Me Gusta',
'Private Playlist': 'Playlist Privada',
Follow: 'Seguir',
Expand Down
19 changes: 17 additions & 2 deletions src/pages/Artist/container/controls/followButton.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { yourLibraryActions } from '../../../../store/slices/yourLibrary';

// Services
import { userService } from '../../../../services/users';
import { uiActions } from '../../../../store/slices/ui';

// Redux

Expand All @@ -18,13 +19,20 @@ const FollowArtist: FC<{ id: string; onToggle: () => void; size?: number }> = ({
}) => {
const { t } = useTranslation(['artist']);
const dispatch = useAppDispatch();
const user = useAppSelector(
(state) => !!state.auth.user,
(prev, next) => prev === next
);

const handleFollow = useCallback(() => {
if (!user) {
return dispatch(uiActions.openLoginTooltip());
}
userService.unfollowArtists([id]).then(() => {
dispatch(artistActions.setFollowing({ following: true }));
onToggle();
});
}, [dispatch, id, onToggle]);
}, [dispatch, id, onToggle, user]);

return (
<button className='transparent-button' onClick={handleFollow}>
Expand All @@ -39,13 +47,20 @@ const UnfollowArtist: FC<{ id: string; onToggle: () => void; size?: number }> =
}) => {
const { t } = useTranslation(['artist']);
const dispatch = useAppDispatch();
const user = useAppSelector(
(state) => !!state.auth.user,
(prev, next) => prev === next
);

const handleUnfollow = useCallback(() => {
if (!user) {
return dispatch(uiActions.openLoginTooltip());
}
userService.unfollowArtists([id]).then(() => {
dispatch(artistActions.setFollowing({ following: false }));
onToggle();
});
}, [dispatch, id, onToggle]);
}, [dispatch, id, onToggle, user]);

return (
<button className='transparent-button' onClick={handleUnfollow}>
Expand Down

0 comments on commit 6b09c78

Please sign in to comment.