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: create "simple" wallet data object #297

Merged
merged 7 commits into from
Oct 27, 2023
Merged
Show file tree
Hide file tree
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
5 changes: 3 additions & 2 deletions app/Data/Gallery/GalleryData.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace App\Data\Gallery;

use App\Data\SimpleWalletData;
use App\Enums\CurrencyCode;
use App\Models\Gallery;
use App\Models\User;
Expand All @@ -28,7 +29,7 @@ public function __construct(
public int $collectionsCount,
public ?float $value,
public ?string $coverImage,
public GalleryWalletData $wallet,
public SimpleWalletData $wallet,
public GalleryNftsData $nfts,
public bool $isOwner = false,
public bool $hasLiked = false,
Expand Down Expand Up @@ -62,8 +63,8 @@ public static function fromModel(Gallery $gallery, ?int $limit = 6): self
collectionsCount: $galleryCache->collectionsCount(),
value: $gallery->value($currency),
coverImage: $gallery->cover_image,
wallet: GalleryWalletData::fromModel($gallery->user->wallet),
nfts: new GalleryNftsData(GalleryNftData::collection($gallery->nfts()->orderByPivot('order_index', 'asc')->paginate($limit, ['*'], 'page', 1))),
wallet: SimpleWalletData::fromModel($gallery->user->wallet),
isOwner: $isOwner,
hasLiked: $hasLiked,
);
Expand Down
5 changes: 3 additions & 2 deletions app/Data/Nfts/NftData.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace App\Data\Nfts;

use App\Data\SimpleWalletData;
use App\Models\Nft;
use DateTime;
use Spatie\LaravelData\Attributes\MapInputName;
Expand All @@ -22,7 +23,7 @@ public function __construct(
public string $tokenNumber,
public NftCollectionData $collection,
public NftImagesData $images,
public ?NftWalletData $wallet,
public ?SimpleWalletData $wallet,
public ?DateTime $lastViewedAt,
public ?DateTime $lastActivityFetchedAt
) {
Expand All @@ -37,7 +38,7 @@ public static function fromModel(Nft $nft): self
tokenNumber: $nft->token_number,
collection: NftCollectionData::fromModel($nft->collection),
images: NftImagesData::from($nft->images()),
wallet: $nft->wallet_id ? NftWalletData::fromModel($nft->wallet) : null,
wallet: $nft->wallet_id ? SimpleWalletData::fromModel($nft->wallet) : null,
lastViewedAt: $nft->last_viewed_at,
lastActivityFetchedAt: $nft->collection->activity_updated_at,
);
Expand Down
25 changes: 0 additions & 25 deletions app/Data/Nfts/NftWalletData.php

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@

declare(strict_types=1);

namespace App\Data\Gallery;
namespace App\Data;

use App\Data\Wallet\WalletAvatarData;
use App\Models\Wallet;
use Spatie\LaravelData\Data;
use Spatie\TypeScriptTransformer\Attributes\TypeScript;

#[TypeScript]
class GalleryWalletData extends Data
class SimpleWalletData extends Data
{
public function __construct(
public string $address,
Expand All @@ -21,13 +21,10 @@ public function __construct(

public static function fromModel(Wallet $wallet): self
{
$domain = $wallet->details?->domain;
$avatarData = WalletAvatarData::fromModel($wallet);

return new self(
$wallet['address'],
$domain,
$avatarData,
address: $wallet->address,
domain: $wallet->details?->domain,
avatar: WalletAvatarData::fromModel($wallet),
);
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { NftOwner } from "./NftOwner";
import NFTCollectionFactory from "@/Tests/Factories/Nfts/NFTCollectionFactory";
import NftFactory from "@/Tests/Factories/Nfts/NftFactory";
import NftWalletFactory from "@/Tests/Factories/Nfts/NftWalletFactory";
import SimpleWalletDataFactory from "@/Tests/Factories/SimpleWalletDataFactory";
import { render, screen } from "@/Tests/testing-library";
import { ExplorerChains } from "@/Utils/Explorer";

Expand Down Expand Up @@ -34,7 +34,7 @@ describe("NftOwner", () => {
const collection = new NFTCollectionFactory().create({
chainId: ExplorerChains.PolygonMainnet,
});
const wallet = new NftWalletFactory().create();
const wallet = new SimpleWalletDataFactory().create();
const nft = new NftFactory().withWallet().create({
collection,
wallet,
Expand All @@ -54,7 +54,7 @@ describe("NftOwner", () => {
const collection = new NFTCollectionFactory().create({
chainId: ExplorerChains.EthereumMainnet,
});
const wallet = new NftWalletFactory().create();
const wallet = new SimpleWalletDataFactory().create();
const nft = new NftFactory().withWallet().create({
collection,
wallet,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ export const GalleryControls = ({
}: {
likesCount?: number;
gallery?: App.Data.Gallery.GalleryData;
wallet: App.Data.Gallery.GalleryWalletData;
wallet: App.Data.SimpleWalletData;
isDisabled?: boolean;
showEditAction?: boolean;
alreadyReported?: boolean;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ export const GalleryCurator = ({
className,
truncate = 10,
}: {
wallet: App.Data.Gallery.GalleryWalletData;
wallet: App.Data.SimpleWalletData;
className?: string;
truncate?: number | false;
}): JSX.Element => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -209,13 +209,7 @@ export const NftImageGrid = ({
);
};

export const GalleryHeading = ({
name,
wallet,
}: {
name: string;
wallet: App.Data.Gallery.GalleryWalletData;
}): JSX.Element => {
export const GalleryHeading = ({ name, wallet }: { name: string; wallet: App.Data.SimpleWalletData }): JSX.Element => {
const truncateReference = useRef<HTMLHeadingElement>(null);

const isTruncated = useIsTruncated({ reference: truncateReference });
Expand Down
4 changes: 2 additions & 2 deletions resources/js/Tests/Factories/Gallery/GalleryDataFactory.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { faker } from "@faker-js/faker";
import GalleryNftDataFactory from "./GalleryNftDataFactory";
import GalleryWalletDataFactory from "./GalleryWalletDataFactory";
import ModelFactory from "@/Tests/Factories/ModelFactory";
import SimpleWalletDataFactory from "@/Tests/Factories/SimpleWalletDataFactory";

const url = "http://test.test";

Expand All @@ -17,7 +17,7 @@ export default class GalleryDataFactory extends ModelFactory<App.Data.Gallery.Ga
collectionsCount: faker.datatype.number({ min: 1, max: 16 }),
value: this.optional(Number(faker.finance.amount(1 * 1e18, 25 * 1e18, 0))),
coverImage: this.optional(faker.image.avatar()),
wallet: new GalleryWalletDataFactory().create(),
wallet: new SimpleWalletDataFactory().create(),
nfts: {
paginated: {
data: new GalleryNftDataFactory().createMany(faker.datatype.number({ min: 0, max: 3 })),
Expand Down
6 changes: 3 additions & 3 deletions resources/js/Tests/Factories/Nfts/NftFactory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { faker } from "@faker-js/faker";
import ModelFactory from "@/Tests/Factories/ModelFactory";
import NFTCollectionFactory from "@/Tests/Factories/Nfts/NFTCollectionFactory";
import NftImagesDataFactory from "@/Tests/Factories/Nfts/NftImagesDataFactory";
import NftWalletFactory from "@/Tests/Factories/Nfts/NftWalletFactory";
import SimpleWalletDataFactory from "@/Tests/Factories/SimpleWalletDataFactory";

export default class NftFactory extends ModelFactory<App.Data.Nfts.NftData> {
protected factory(): App.Data.Nfts.NftData {
Expand All @@ -13,15 +13,15 @@ export default class NftFactory extends ModelFactory<App.Data.Nfts.NftData> {
tokenNumber: faker.datatype.number({ min: 1, max: 100000 }).toString(),
collection: new NFTCollectionFactory().withImage().create(),
images: new NftImagesDataFactory().create(),
wallet: this.optional(new NftWalletFactory().create()),
wallet: this.optional(new SimpleWalletDataFactory().create()),
lastViewedAt: null,
lastActivityFetchedAt: null,
};
}

public withWallet(): this {
return this.state(() => ({
wallet: new NftWalletFactory().create(),
wallet: new SimpleWalletDataFactory().create(),
}));
}

Expand Down
9 changes: 0 additions & 9 deletions resources/js/Tests/Factories/Nfts/NftWalletFactory.ts

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { faker } from "@faker-js/faker";
import WalletAvatarDataFactory from "./Wallet/WalletAvatarDataFactory";
import ModelFactory from "@/Tests/Factories/ModelFactory";
import WalletAvatarDataFactory from "@/Tests/Factories/Wallet/WalletAvatarDataFactory";

export default class GalleryWalletDataFactory extends ModelFactory<App.Data.Gallery.GalleryWalletData> {
protected factory(): App.Data.Gallery.GalleryWalletData {
export default class SimpleWalletDataFactory extends ModelFactory<App.Data.SimpleWalletData> {
protected factory(): App.Data.SimpleWalletData {
return {
address: this.generateAddress(),
domain: this.optional(faker.internet.domainName()),
Expand Down
17 changes: 7 additions & 10 deletions resources/types/generated.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,11 @@ declare namespace App.Data {
timestamp: number;
price: number;
};
export type SimpleWalletData = {
address: string;
domain: string | null;
avatar: App.Data.Wallet.WalletAvatarData;
};
export type TokenListItemData = {
guid: number | null;
name: string;
Expand Down Expand Up @@ -232,7 +237,7 @@ declare namespace App.Data.Gallery {
collectionsCount: number;
value: number | null;
coverImage: string | null;
wallet: App.Data.Gallery.GalleryWalletData;
wallet: App.Data.SimpleWalletData;
nfts: App.Data.Gallery.GalleryNftsData;
isOwner: boolean;
hasLiked: boolean;
Expand Down Expand Up @@ -284,11 +289,6 @@ declare namespace App.Data.Gallery {
nfts: number;
likes: number;
};
export type GalleryWalletData = {
address: string;
domain: string | null;
avatar: App.Data.Wallet.WalletAvatarData;
};
}
declare namespace App.Data.Network {
export type NetworkWithCollectionsData = {
Expand Down Expand Up @@ -349,7 +349,7 @@ declare namespace App.Data.Nfts {
tokenNumber: string;
collection: App.Data.Nfts.NftCollectionData;
images: App.Data.Nfts.NftImagesData;
wallet: App.Data.Nfts.NftWalletData | null;
wallet: App.Data.SimpleWalletData | null;
lastViewedAt: string | null;
lastActivityFetchedAt: string | null;
};
Expand All @@ -360,9 +360,6 @@ declare namespace App.Data.Nfts {
original: string | null;
originalRaw: string | null;
};
export type NftWalletData = {
address: string;
};
}
declare namespace App.Data.Token {
export type TokenData = {
Expand Down
Loading