Skip to content

Commit

Permalink
[AutoFill] autofill params from ens name
Browse files Browse the repository at this point in the history
  • Loading branch information
petermlyon committed Oct 5, 2024
1 parent 7317a23 commit a768f5a
Showing 1 changed file with 62 additions and 2 deletions.
64 changes: 62 additions & 2 deletions packages/nextjs/components/edit/EditDialog.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { useEffect, useState } from "react";
import { useAccount } from "wagmi";
import { useAccount, useEnsName, useEnsText } from "wagmi";
import { User } from "~~/app/ApiClasses/User";
import { useScaffoldReadContract } from "~~/hooks/scaffold-eth";

Expand All @@ -15,6 +15,10 @@ export const EditDialog = ({ writeFunction, headerText, writeButtonText }: EditD
const [user, setUser] = useState<User>({
displayName: "",
description: "",
tg: "",
company: "",
jobTitle: "",
bioUrl: "",
});

const { data: userData } = useScaffoldReadContract({
Expand All @@ -23,6 +27,29 @@ export const EditDialog = ({ writeFunction, headerText, writeButtonText }: EditD
args: [connectedAddress],
});

const [wasSet, setWasSet] = useState({
displayName: false,
description: false,
tg: false,
});

const { data: ensName } = useEnsName({
address: "0x225f137127d9067788314bc7fcc1f36746a3c3b5",
chainId: 1,
});

const { data: description } = useEnsText({
name: ensName ? ensName : "",
key: "description",
chainId: 1,
});

const { data: telegram } = useEnsText({
name: ensName ? ensName : "",
key: "org.telegram",
chainId: 1,
});

useEffect(() => {
const newUserData = JSON.parse(JSON.stringify(user));
if (userData?.[0]) {
Expand All @@ -46,6 +73,39 @@ export const EditDialog = ({ writeFunction, headerText, writeButtonText }: EditD
setUser(newUserData);
}, [userData]);

useEffect(() => {
if (!wasSet.displayName && user.displayName == "" && ensName) {
const newUser = JSON.parse(JSON.stringify(user));
newUser.displayName = ensName;
setUser(newUser);
const newWasSet = JSON.parse(JSON.stringify(wasSet));
newWasSet.displayName = true;
setWasSet(newWasSet);
}
}, [ensName, user, wasSet]);

useEffect(() => {
if (!wasSet.tg && user.tg == "" && telegram) {
const newUser = JSON.parse(JSON.stringify(user));
newUser.tg = telegram;
setUser(newUser);
const newWasSet = JSON.parse(JSON.stringify(wasSet));
newWasSet.tg = true;
setWasSet(newWasSet);
}
}, [telegram, user, wasSet]);

useEffect(() => {
if (!wasSet.description && user.description == "" && description) {
const newUser = JSON.parse(JSON.stringify(user));
newUser.description = description;
setUser(newUser);
const newWasSet = JSON.parse(JSON.stringify(wasSet));
newWasSet.description = true;
setWasSet(newWasSet);
}
}, [description, user, wasSet]);

return (
<>
<div
Expand Down Expand Up @@ -78,7 +138,7 @@ export const EditDialog = ({ writeFunction, headerText, writeButtonText }: EditD
/>
</div>
<div style={{ display: "flex", justifyContent: "space-between", width: "100%" }}>
<p>Telegram Name</p>
<p>Telegram ID</p>
<input
className="textBox"
type="text"
Expand Down

0 comments on commit a768f5a

Please sign in to comment.