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

refactor: prevent the user from selecting more than 16 nfts #373

Merged
merged 16 commits into from
Nov 9, 2023
Merged
Show file tree
Hide file tree
Changes from 11 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
1 change: 1 addition & 0 deletions lang/en/pages.php
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,7 @@
'title_too_long' => 'Gallery name must not exceed :max characters.',
'already_selected_nft' => 'NFT already exists in this gallery',
'nft_missing_image' => 'Only NFTs with images can be added to galleries',
'nft_gallery_limit' => 'You can only add 16 NFTs per gallery',
'gallery_cover' => 'Gallery Cover',
'gallery_cover_description' => 'The cover is used for the card on the gallery list page. While the cover is not a requirement it will allow you to add personality and stand out from the crowd.',
'gallery_cover_information' => 'Image dimensions must be at least 287px x 190px, with a max size of 2 MB (JPG, PNG or GIF)',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ export const NftCollections = ({
getLoadingNftsCount,
}: NftCollectionsProperties): JSX.Element => {
const { selected, addToSelection, removeFromSelection } = useNftSelectableContext();
const { nfts: addedNfts } = useEditableGalleryContext();
const { nfts: addedNfts, nftLimit } = useEditableGalleryContext();

const items = Object.entries(
groupBy(nfts, (nft: App.Data.Gallery.GalleryNftData) => `${nft.chainId}-${nft.tokenAddress}`),
Expand Down Expand Up @@ -111,6 +111,8 @@ export const NftCollections = ({
onDeselectNft={removeFromSelection}
onSelectNft={addToSelection}
validateImage={true}
storedNfts={addedNfts.selected}
nftLimit={nftLimit}
/>

{!allNftsLoaded(nfts[0]) && (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
GalleryHeadingPlaceholder,
GalleryStats,
GalleryStatsPlaceholder,
NftImageContainer,
NftImageGrid,
} from "./NftGalleryCard.blocks";
import * as useAuthorizedActionMock from "@/Hooks/useAuthorizedAction";
Expand Down Expand Up @@ -286,6 +287,101 @@ describe("NftImageGrid", () => {
expect(screen.getByTestId(`NftImageGrid__container--1--invalid_image`)).toBeInTheDocument();
expect(screen.getByTestId(`NftImageGrid__container--4--invalid_image`)).toBeInTheDocument();
});

it("should show a limit reached message if the limit is reached", () => {
const nftList = [
...nfts,
{
id: 5,
name: "nft_5",
images: { thumb: "nft_image_5", small: "nft_image_5", large: "nft_image_5" },
tokenNumber: "5",
ownedByCurrentUser: true,
...collectionInfo,
},
];

render(
<NftImageGrid
nfts={{
paginated: {
data: nftList,
...paginationData,
},
}}
storedNfts={nfts}
nftLimit={1}
/>,
);

expect(screen.getByTestId(`NftImageGrid__container--5--limit_reached`)).toBeInTheDocument();
expect(screen.getByTestId(`NftImageGrid__image--5`)).toHaveClass("blur-sm");
});

it("should not show a limit reached message if the limit is not reached", () => {
render(
<NftImageGrid
nfts={{
paginated: {
data: nfts,
...paginationData,
},
}}
nftLimit={1}
/>,
);

expect(screen.queryByTestId("NftImageGrid__container--5--limit_reached")).not.toBeInTheDocument();
});
});

describe("NftImageContainer", () => {
const nft: App.Data.Gallery.GalleryNftData = {
id: 1,
name: "nft_1",
images: { thumb: "nft_image_1", small: "nft_image_1", large: "nft_image_1" },
tokenNumber: "1",
ownedByCurrentUser: false,
...collectionInfo,
};

it("should render", () => {
render(
<NftImageContainer
nft={nft}
validateImage={true}
/>,
);

expect(screen.getByTestId("NftImageGrid__container--1")).toBeInTheDocument();
});

it("should blur image if nft is in selected list", () => {
render(
<NftImageContainer
nft={nft}
validateImage={true}
nftLimit={1}
combinedNfts={[nft]}
/>,
);

expect(screen.getByTestId("NftImageGrid__container--1--limit_reached")).toBeInTheDocument();
expect(screen.getByTestId("NftImageGrid__image--1")).toHaveClass("blur-sm");
});

it("should not blur image if no list is passed", () => {
render(
<NftImageContainer
nft={nft}
validateImage={true}
nftLimit={1}
/>,
);

expect(screen.queryByTestId("NftImageGrid__container--1--limit_reached")).not.toBeInTheDocument();
expect(screen.queryByTestId("NftImageGrid__image--1")).not.toHaveClass("blur-sm");
});
});

describe("GalleryHeading", () => {
Expand Down
32 changes: 31 additions & 1 deletion resources/js/Components/Galleries/NftGalleryCard.blocks.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ interface NftImageContainerProperties {
isSelected?: boolean;
isAdded?: boolean;
validateImage?: boolean;
combinedNfts?: App.Data.Gallery.GalleryNftData[];
nftLimit?: number;
}

interface NftImageGridProperties {
Expand All @@ -43,6 +45,8 @@ interface NftImageGridProperties {
selectedNfts?: App.Data.Gallery.GalleryNftData[];
addedNfts?: App.Data.Gallery.GalleryNftData[];
validateImage?: boolean;
storedNfts?: App.Data.Gallery.GalleryNftData[];
nftLimit?: number;
}

const NftImage = ({
Expand Down Expand Up @@ -87,13 +91,15 @@ const NftImage = ({
</div>
);

const NftImageContainer = ({
export const NftImageContainer = ({
nft,
onClick,
allowSelection,
isSelected,
isAdded,
validateImage,
combinedNfts = [],
nftLimit,
}: NftImageContainerProperties): JSX.Element => {
const { t } = useTranslation();

Expand Down Expand Up @@ -130,6 +136,25 @@ const NftImageContainer = ({
);
}

if (isTruthy(nftLimit) && combinedNfts.length >= nftLimit && !isTruthy(isSelected)) {
return (
<div
data-testid={`NftImageGrid__container--${nft.tokenNumber}--limit_reached`}
className="relative overflow-hidden rounded-xl"
>
<Tooltip content={t("pages.galleries.create.nft_gallery_limit")}>
<div>
<NftImage
nft={nft}
allowSelection={false}
className="blur-sm grayscale"
/>
</div>
</Tooltip>
</div>
);
}

return (
<div
data-testid={`NftImageGrid__container--${nft.tokenNumber}`}
Expand Down Expand Up @@ -158,8 +183,11 @@ export const NftImageGrid = ({
onSelectNft,
onDeselectNft,
validateImage,
storedNfts = [],
nftLimit,
}: NftImageGridProperties): JSX.Element => {
const nftData = "paginated" in nfts ? nfts.paginated.data : nfts;
const combinedNfts = [...storedNfts, ...(isTruthy(selectedNfts) ? selectedNfts : [])];

return (
<div
Expand All @@ -184,6 +212,8 @@ export const NftImageGrid = ({
}}
isSelected={isSelected}
isAdded={isAdded}
combinedNfts={combinedNfts}
nftLimit={nftLimit}
patricio0312rev marked this conversation as resolved.
Show resolved Hide resolved
validateImage={validateImage}
/>
);
Expand Down
2 changes: 1 addition & 1 deletion resources/js/I18n/Locales/en.json

Large diffs are not rendered by default.

Loading