@@ -4,19 +4,10 @@ import { useEffect, useRef, useState } from "react";
44import Image from "next/image" ;
55import Link from "next/link" ;
66import BookmarkButton from "./BookmarkButton" ;
7- // import CommentSection from "./CommentSection";
8- // import LikeButton from "./LikedButton";
9- // import { ProfileAddress } from "./ProfileAddress";
10- import {
11- // BookmarkIcon,
12- // ChatBubbleLeftIcon,
13- MagnifyingGlassPlusIcon ,
14- ShareIcon ,
15- ShoppingCartIcon ,
16- XMarkIcon ,
17- } from "@heroicons/react/24/outline" ;
18- // import { ChatBubbleLeftIcon as ChatBubbleLeftSolidIcon } from "@heroicons/react/24/solid";
19- import { useScaffoldReadContract } from "~~/hooks/scaffold-eth" ;
7+ import { formatEther , parseEther } from "viem" ;
8+ import { useAccount } from "wagmi" ;
9+ import { MagnifyingGlassPlusIcon , ShareIcon , ShoppingCartIcon , XMarkIcon } from "@heroicons/react/24/outline" ;
10+ import { useScaffoldReadContract , useScaffoldWriteContract } from "~~/hooks/scaffold-eth" ;
2011import { notification } from "~~/utils/scaffold-eth" ;
2112import { NFTMetaData } from "~~/utils/simpleNFT/nftsMetadata" ;
2213
@@ -25,12 +16,17 @@ export interface Post extends Partial<NFTMetaData> {
2516 postId ?: number ;
2617 uri : string ;
2718 user : string ;
19+ price : string ;
20+ amount : string ;
2821 date ?: string ;
2922}
3023
3124export const PostCard = ( { post } : { post : Post } ) => {
3225 const [ isModalOpen , setIsModalOpen ] = useState ( false ) ;
33- // const [showCommentSection, setShowCommentSection] = useState(false);
26+ const [ loading , setLoading ] = useState ( false ) ;
27+
28+ const { address : connectedAddress } = useAccount ( ) ;
29+ const { writeContractAsync } = useScaffoldWriteContract ( "BasedShop" ) ;
3430
3531 const { data : profileInfo } = useScaffoldReadContract ( {
3632 contractName : "BasedProfile" ,
@@ -39,6 +35,20 @@ export const PostCard = ({ post }: { post: Post }) => {
3935 watch : true ,
4036 } ) ;
4137
38+ const { data : articlePrice } = useScaffoldReadContract ( {
39+ contractName : "BasedShop" ,
40+ functionName : "articlePrices" ,
41+ args : [ BigInt ( post . postId || 0 ) ] ,
42+ watch : true ,
43+ } ) ;
44+
45+ const { data : articleAmount } = useScaffoldReadContract ( {
46+ contractName : "BasedShop" ,
47+ functionName : "articleAmounts" ,
48+ args : [ BigInt ( post . postId || 0 ) ] ,
49+ watch : true ,
50+ } ) ;
51+
4252 const defaultProfilePicture = "/guest-profile.jpg" ;
4353
4454 const profilePicture = profileInfo && profileInfo [ 2 ] ? profileInfo [ 2 ] : defaultProfilePicture ;
@@ -51,10 +61,6 @@ export const PostCard = ({ post }: { post: Post }) => {
5161 setIsModalOpen ( false ) ;
5262 } ;
5363
54- // const toggleCommentSection = () => {
55- // setShowCommentSection(!showCommentSection);
56- // };
57-
5864 const handleShare = async ( ) => {
5965 if ( navigator . share ) {
6066 try {
@@ -77,6 +83,32 @@ export const PostCard = ({ post }: { post: Post }) => {
7783 }
7884 } ;
7985
86+ const handleBuyArticle = async ( ) => {
87+ if ( ! connectedAddress ) {
88+ notification . error ( "Please connect your wallet" ) ;
89+ return ;
90+ }
91+
92+ setLoading ( true ) ;
93+
94+ try {
95+ const contractResponse = await writeContractAsync ( {
96+ functionName : "buyArticle" ,
97+ args : [ BigInt ( post . postId || 0 ) ] ,
98+ value : articlePrice ,
99+ } ) ;
100+
101+ if ( contractResponse ) {
102+ notification . success ( "Posted successfully!" ) ;
103+ }
104+ } catch ( error ) {
105+ console . error ( "Error during posting:" , error ) ;
106+ notification . error ( "Posting failed, please try again." ) ;
107+ } finally {
108+ setLoading ( false ) ;
109+ }
110+ } ;
111+
80112 const formatDate = ( timestamp : number ) : string => {
81113 const date = new Date ( timestamp * 1000 ) ; // Convert to milliseconds
82114 return date . toLocaleDateString ( ) ; // Format the date as needed
@@ -124,7 +156,10 @@ export const PostCard = ({ post }: { post: Post }) => {
124156 </ div >
125157 ) }
126158 < div className = "flex flex-col justify-center w-2/3 pl-4" >
127- < p className = "my-0 text-lg" > { post . description ?? "No description available." } </ p >
159+ < span className = "my-0 text-lg" > { post . description ?? "No description available." } </ span >
160+ < span className = "text-md italic" >
161+ On stock: < span className = "font-bold" > { articleAmount ?. toString ( ) } </ span > units
162+ </ span >
128163 </ div >
129164 </ div >
130165
@@ -139,24 +174,28 @@ export const PostCard = ({ post }: { post: Post }) => {
139174 < BookmarkButton postId = { BigInt ( post . postId || 0 ) } />
140175 </ div >
141176
142- { /* <div className="flex items-center gap-3">
143- <LikeButton postId={BigInt(post.postId || 0)} />
144- <button onClick={toggleCommentSection} className="icon-button">
145- {showCommentSection ? (
146- <ChatBubbleLeftSolidIcon className="comment-icon text-blue-600" />
147- ) : (
148- <ChatBubbleLeftIcon className="comment-icon" />
149- )}
150- </button>
151- </div> */ }
152177 < div className = "flex items-center gap-3" >
153- < button className = "px-4 py-2 rounded-lg bg-red-600 text-white" > Buy</ button >
178+ { articlePrice !== null && (
179+ < div className = "flex items-center" >
180+ < span className = "text-lg font-semibold" >
181+ { articlePrice !== undefined ? formatEther ( articlePrice ) : "N/A" }
182+ </ span >
183+ < Image src = "/ethereumlogo.svg" alt = "ETH Logo" width = { 20 } height = { 20 } className = "ml-2" />
184+ </ div >
185+ ) }
186+ < button
187+ disabled = { loading }
188+ onClick = { handleBuyArticle }
189+ className = "px-4 py-2 rounded-lg bg-green-600 text-white"
190+ >
191+ Buy
192+ </ button >
193+
154194 < button className = "p-2 rounded-full bg-red-600 text-white" >
155195 < ShoppingCartIcon className = "h-5 w-5" />
156196 </ button >
157197 </ div >
158198 </ div >
159- { /* {showCommentSection && <CommentSection postId={BigInt(post.postId || 0)} /> } */ }
160199 </ div >
161200
162201 { /* Modal for fullscreen image */ }
0 commit comments