Skip to content

Commit

Permalink
Refactor album wish list management components
Browse files Browse the repository at this point in the history
  • Loading branch information
davewalker5 committed Nov 10, 2023
1 parent 1e2aeff commit 383a506
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 75 deletions.
27 changes: 0 additions & 27 deletions src/music-catalogue-ui/components/addAlbumToCatalogue.js

This file was deleted.

27 changes: 0 additions & 27 deletions src/music-catalogue-ui/components/addAlbumToWishList.js

This file was deleted.

29 changes: 10 additions & 19 deletions src/music-catalogue-ui/components/albumRow.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import pages from "@/helpers/navigation";
import DeleteAlbum from "./deleteAlbum";
import AddAlbumToWishList from "./addAlbumToWishList";
import AddAlbumToCatalogue from "./addAlbumToCatalogue";
import DeleteAlbumActionIcon from "./deleteAlbumActionIcon";
import AlbumWishListActionIcon from "./albumWishListActionIcon";

/**
* Component to render a row containing the details of a single album
Expand Down Expand Up @@ -36,29 +35,21 @@ const AlbumRow = ({
{album.released}
</td>
<td>
<DeleteAlbum
<DeleteAlbumActionIcon
album={album}
isWishList={isWishList}
logout={logout}
setAlbums={setAlbums}
/>
</td>
<td>
{isWishList == false ? (
<AddAlbumToWishList
artistId={artist.id}
album={album}
logout={logout}
setAlbums={setAlbums}
/>
) : (
<AddAlbumToCatalogue
artistId={artist.id}
album={album}
logout={logout}
setAlbums={setAlbums}
/>
)}
<AlbumWishListActionIcon
artistId={artist.id}
album={album}
isWishList={isWishList}
logout={logout}
setAlbums={setAlbums}
/>
</td>
</tr>
);
Expand Down
37 changes: 37 additions & 0 deletions src/music-catalogue-ui/components/albumWishListActionIcon.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import { useCallback } from "react";
import { FontAwesomeIcon } from "@fortawesome/react-fontawesome";
import {
faHeartCirclePlus,
faRecordVinyl,
} from "@fortawesome/free-solid-svg-icons";
import { apiSetAlbumWishListFlag, apiFetchAlbumsByArtist } from "@/helpers/api";

const AlbumWishListActionIcon = ({
artistId,
album,
isWishList,
logout,
setAlbums,
}) => {
// Set the icon depending on the direction in which the album will move
const icon = isWishList ? faRecordVinyl : faHeartCirclePlus;

/* Callback to move an album between the wish list and catalogue */
const setAlbumWishListFlag = useCallback(async () => {
// Move the album to the wish list
const result = await apiSetAlbumWishListFlag(album, !isWishList, logout);
if (result) {
// Successful, so refresh the album list
const fetchedAlbums = await apiFetchAlbumsByArtist(
artistId,
isWishList,
logout
);
setAlbums(fetchedAlbums);
}
}, [artistId, album, isWishList, logout, setAlbums]);

return <FontAwesomeIcon icon={icon} onClick={setAlbumWishListFlag} />;
};

export default AlbumWishListActionIcon;
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { apiDeleteAlbum, apiFetchAlbumsByArtist } from "@/helpers/api";
import { FontAwesomeIcon } from "@fortawesome/react-fontawesome";
import { faTrashAlt } from "@fortawesome/free-solid-svg-icons";

const DeleteAlbum = ({ album, isWishList, logout, setAlbums }) => {
const DeleteAlbumActionIcon = ({ album, isWishList, logout, setAlbums }) => {
/* Callback to prompt for confirmation and delete an album */
const confirmDeleteAlbum = useCallback(
async (e, album) => {
Expand Down Expand Up @@ -40,4 +40,4 @@ const DeleteAlbum = ({ album, isWishList, logout, setAlbums }) => {
);
};

export default DeleteAlbum;
export default DeleteAlbumActionIcon;

0 comments on commit 383a506

Please sign in to comment.