Skip to content

Instantly share code, notes, and snippets.

@kmjones1979
Last active February 9, 2024 23:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kmjones1979/26ef9633b61b17f237e88eb41bb688de to your computer and use it in GitHub Desktop.
Save kmjones1979/26ef9633b61b17f237e88eb41bb688de to your computer and use it in GitHub Desktop.
Example for Full Stack dapp Development Workshop
"use client";
import { useState } from "react";
import { gql } from "@apollo/client";
import { useQuery } from "@apollo/client";
import type { NextPage } from "next";
import { useAccount } from "wagmi";
import { Address, AddressInput, Balance } from "~~/components/scaffold-eth";
import {
useAccountBalance,
useDeployedContractInfo,
useScaffoldContractRead,
useScaffoldContractWrite,
} from "~~/hooks/scaffold-eth";
export const GET_MESSAGES = gql`
query MyQuery {
messages(first: 10, orderDirection: desc, orderBy: createdAt) {
message
_to {
id
}
_from {
id
}
}
}
`;
const Home: NextPage = () => {
const [newGreeting, setNewGreeting] = useState("");
const [newReceiver, setNewReceiver] = useState("");
const [newMessage, setNewMessage] = useState("");
const { address } = useAccount();
const { balance } = useAccountBalance();
const { data: yourContract } = useDeployedContractInfo("YourContract");
const { data: greeting } = useScaffoldContractRead({
contractName: "YourContract",
functionName: "greeting",
});
const { writeAsync: setGreeting } = useScaffoldContractWrite({
contractName: "YourContract",
functionName: "setGreeting",
args: [newGreeting],
});
const { writeAsync: sendMessage } = useScaffoldContractWrite({
contractName: "YourContract",
functionName: "sendMessage",
args: [newReceiver, newMessage],
});
const { loading, error, data: messagesData } = useQuery(GET_MESSAGES);
const messages = messagesData?.messages || [];
return (
<>
<div className="flex items-center flex-col flex-grow pt-10">
<div>
<Address address={address} />
<Balance address={address} />
</div>
<div className="p-5 font-black text-xl">{greeting}</div>
<div>
<Address address={yourContract?.address} />
<Balance address={yourContract?.address} />
</div>
<div className="p-5">
<input
value={newGreeting}
placeholder="Type here"
className="input"
onChange={e => setNewGreeting(e.target.value)}
/>
</div>
<div className="p-5">
<button className="btn btn-primary" onClick={setGreeting}>
Set Greeting
</button>
</div>
<div className="p-5">
<AddressInput value={newReceiver} placeholder="Recepient?" name={address} onChange={setNewReceiver} />
</div>
<div className="p-5">
<input
value={newMessage}
placeholder="Message"
className="input"
onChange={e => setNewMessage(e.target.value)}
/>
</div>
<div className="p-5">
<button className="btn btn-primary" onClick={sendMessage}>
Send Message
</button>
</div>
<h1>Messages</h1>
<table className="min-w-full">
<thead>
<tr>
<th>From</th>
<th>To</th>
<th>Message</th>
</tr>
</thead>
<tbody>
{messages.map(message => (
<tr key={message.id}>
<td>{message._from.id}</td>
<td>{message._to.id}</td>
<td>{message.message}</td>
</tr>
))}
</tbody>
</table>
</div>
</>
);
};
export default Home;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment