-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathListCard.tsx
More file actions
154 lines (141 loc) · 5.18 KB
/
ListCard.tsx
File metadata and controls
154 lines (141 loc) · 5.18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
import React, { useCallback, useEffect, useState } from "react";
import Image from "next/image";
import { useRouter } from "next/router";
import { FaHeart } from "react-icons/fa";
import { syncApi } from "@/common/api/indexer";
import { listsContractClient } from "@/common/contracts/core/lists";
import { truncate } from "@/common/lib";
import { LazyImage } from "@/common/ui/layout/components/LazyImage";
import { LayersIcon } from "@/common/ui/layout/svg";
import { LikeIcon } from "@/common/ui/layout/svg/like";
import { useWalletUserSession } from "@/common/wallet";
import { AccountProfilePicture } from "@/entities/_shared/account";
import { useDispatch } from "@/store/hooks";
import { ListFormModalType } from "../types";
export const ListCard = ({
dataForList,
background,
backdrop,
}: {
dataForList?: any;
background?: string;
backdrop: string;
}) => {
const dispatch = useDispatch();
const viewer = useWalletUserSession();
const [isUpvoted, setIsUpvoted] = useState(false);
const { push } = useRouter();
useEffect(() => {
setIsUpvoted(dataForList.upvotes?.some((data: any) => data?.account === viewer.accountId));
}, [dataForList, viewer.accountId]);
const handleRoute = useCallback(
() => push(`/list/${dataForList?.on_chain_id}`),
[dataForList?.on_chain_id, push],
);
const handleUpvote = (e: React.MouseEvent) => {
e.stopPropagation();
if (isUpvoted) {
listsContractClient
.remove_upvote({ list_id: dataForList?.on_chain_id })
.then(async ({ txHash }) => {
if (txHash && viewer.accountId) {
await syncApi
.listRemoveUpvote(dataForList?.on_chain_id, txHash, viewer.accountId)
.catch(() => {});
}
})
.catch((error) => console.error("Error removing upvote:", error));
dispatch.listEditor.handleListToast({
name: truncate(dataForList?.name ?? "", 15),
type: ListFormModalType.DOWNVOTE,
});
} else {
listsContractClient
.upvote({ list_id: dataForList?.on_chain_id })
.then(async ({ txHash }) => {
if (txHash && viewer.accountId) {
await syncApi
.listUpvote(dataForList?.on_chain_id, txHash, viewer.accountId)
.catch(() => {});
}
})
.catch((error) => console.error("Error upvoting:", error));
dispatch.listEditor.handleListToast({
name: truncate(dataForList?.name ?? "", 15),
type: ListFormModalType.UPVOTE,
});
}
};
const handleRouteUser = useCallback(
(e: React.MouseEvent) => {
e.stopPropagation();
push(`/profile/${dataForList?.owner?.id}`);
},
[dataForList?.owner],
);
return (
<div
onClick={handleRoute}
className="cursor-pointer transition-all duration-300 hover:translate-y-[-1rem]"
>
<Image
src={dataForList?.cover_image_url ? "/assets/images/default-backdrop.png" : backdrop}
alt="backdrop"
width={500}
height={5}
className={`h-max w-full ${backdrop.endsWith("list_bg_image.png") ? "px-4" : ""} object-cover`}
/>
<div
className=" bg-background overflow-hidden rounded-[12px] border border-gray-300 "
data-testid="list-card"
>
<div className="relative">
<LazyImage
alt="listImage"
className="h-[221px] w-full object-cover"
src={dataForList?.cover_image_url ?? background}
width={500}
height={150}
/>
<div
style={{ boxShadow: "0px 3px 5px 0px rgba(5, 5, 5, 0.08)" }}
className="bg-background absolute bottom-4 right-4 flex items-center gap-1 rounded-[4px] px-4 py-2"
>
<LayersIcon />
<p className="text-[12px] font-[600]">{dataForList?.registrations_count} Accounts</p>
</div>
</div>
<div className="flex h-[112px] flex-col justify-between p-3">
<p className=" text-lg font-[600] leading-tight">
{truncate(dataForList?.name || "", 150)}
</p>
<div className="mt-2 flex items-center justify-between space-x-2">
<div className="flex items-center gap-2 text-[14px]">
<p className="">BY</p>
<div
role="button"
className="flex items-center gap-1 hover:opacity-50"
onClick={handleRouteUser}
>
<AccountProfilePicture accountId={dataForList?.owner?.id} className="h-4 w-4" />
<p className="">{truncate(dataForList.owner?.id ?? "", 25)}</p>
</div>
</div>
<div className="flex items-center justify-center gap-2">
<button onClick={handleUpvote} className="focus:outline-none">
{isUpvoted ? (
<FaHeart className="text-[18px] text-red-500" />
) : (
<LikeIcon className="m-0 fill-red-500 p-0" />
)}
</button>
<p className="m-0 p-0 pt-1 text-[16px] font-semibold text-black">
{dataForList.upvotes?.length}
</p>
</div>
</div>
</div>
</div>
</div>
);
};