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

Update Gist according to the provided account information #9

Open
wants to merge 1 commit into
base: ethereum-sign
Choose a base branch
from
Open
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
15 changes: 10 additions & 5 deletions helpers/github.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,10 @@ const GITHUB_ADDRESS_FILE_PATH =

export function writeAddressbook(newEntry, currentEntry) {
try {
const name = newEntry.name;
const discordId = newEntry.id;
const address = newEntry.address;
const github = newEntry.github;
const github = '@' + newEntry.githubUsername;
let userExists = null;
fetch(`${GITHUB_API_URL}/repos/${GITHUB_ADDRESS_FILE_PATH}`, {
method: 'GET',
Expand All @@ -23,7 +24,6 @@ export function writeAddressbook(newEntry, currentEntry) {
})
.then((res) => res.json())
.then((body) => {
console.log('body: ', body);
const encodedContent = body.content;
const fileSha = body.sha;
console.log(`fetched file with sha ${fileSha} for user ${name}`);
Expand All @@ -32,13 +32,16 @@ export function writeAddressbook(newEntry, currentEntry) {
const decodedContent = decodeData(encodedContent); // Manipulated the decoded content:
// First, check if the user already exists.
if (!!currentEntry) {
const index = decodedContent.indexOf(currentEntry);
if (currentEntry.address !== address) {
const index = decodedContent.findIndex(
(e) => e.address === currentEntry.address
);

if (address && currentEntry.address !== address) {
decodedContent[index].address = address;
} else {
console.log('address already there');
}
if (currentEntry.github !== github) {
if (github && currentEntry.github !== github) {
decodedContent[index].github = github;
} else {
console.log('github already there');
Expand All @@ -57,6 +60,7 @@ export function writeAddressbook(newEntry, currentEntry) {

// We encode the updated content to base64.
const updatedContent = encodeData(decodedContent);

// We prepare the body to be sent to the API.
const marshalledBody = marshallFileUpdate({
message: 'Update addressbook.json',
Expand All @@ -74,6 +78,7 @@ export function writeAddressbook(newEntry, currentEntry) {
console.log('Updated file on GitHub successfully.');
});
});
return { name, address, github };
} catch (err) {
console.log(error);
}
Expand Down
1 change: 1 addition & 0 deletions next.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ module.exports = {
env: {
GITHUB_CLIENT_ID: process.env.GITHUB_CLIENT_ID,
GITHUB_CLIENT_SECRET: process.env.GITHUB_CLIENT_SECRET,
GITHUB_API_TOKEN: process.env.GITHUB_API_TOKEN,
NEXTAUTH_URL: process.env.NEXTAUTH_URL,
},
};
60 changes: 39 additions & 21 deletions pages/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,27 @@ export default function Home() {
const [addBookEntry, setAddBookEntry] = useState({});
const [isLoading, setIsLoading] = useState(false);
const [githubUsername, setGithubUsername] = useState('');
const [updateSuccessfull, setUpdateSuccessful] = useState(false);
const [signature, setSignature] = useState('');

const { connect, address, web3Provider } = useWeb3Modal();
const { data: session, status } = useSession({ required: true });
const { addressbook } = useAddressbook(session.user.id);

const onSubmit = async (ethAddress, githubUsername) => {
const canSubmit = (address && signature) || githubUsername;

const onSubmit = async (e) => {
e.preventDefault();
const newEntry = { discordId: session.user.id, address, githubUsername };
writeAddressbook(newEntry, addBookEntry);
const newEntry = {
name: session.user.name,
discordId: session.user.id,
address,
githubUsername,
};
const { address, github } = writeAddressbook(newEntry, addBookEntry);
address && setAddBookEntry({ ...addBookEntry, address });
github && setAddBookEntry({ ...addBookEntry, github });
setUpdateSuccessful(true);
};

const brightIDMessage = session.hasBrightId
Expand Down Expand Up @@ -118,30 +129,17 @@ export default function Home() {
<>
<div className="w-full flex flex-row justify-center items-center">
<div className="w-1/2 flex flex-col gap-5 justify-center items-start ml-10">
<AccountBanner src={'/assets/github.png'}>
{addBookEntry.github ? addBookEntry.github : '❌ Not Linked'}
</AccountBanner>
<AccountBanner src={'/assets/ethereum.png'}>
{addBookEntry.address
? `${formatAddress(addBookEntry.address)}`
: '❌ Not Linked'}
</AccountBanner>
<AccountBanner src={'/assets/github.png'}>
{addBookEntry.github ? addBookEntry.github : '❌ Not Linked'}
</AccountBanner>
</div>

<div className="w-1/2 flex flex-col gap-5 flex-row justify-center items-center">
<div className="flex justify-center w-60 p-2">
<button
className=" w-[calc(100%-10px)] h-12 px-3.5 text-white p-2 text-xl bg-cornflowerblue rounded shadow"
onClick={address ? signMessage : connect}
>
{signature
? `✅ ${formatAddress(address)}`
: address
? `Link ${formatAddress(address)}`
: 'Connect Ethereum'}
</button>
</div>

<div className="w-60 p-2">
{githubUsername ? (
<GithubLoginButton
Expand All @@ -161,11 +159,31 @@ export default function Home() {
</a>
)}
</div>
<div className="flex justify-center w-60 p-2">
<button
className=" w-[calc(100%-10px)] h-12 px-3.5 text-white p-2 text-xl bg-cornflowerblue rounded shadow-md"
onClick={address ? signMessage : connect}
>
{signature
? `✅ ${formatAddress(address)}`
: address
? `Link ${formatAddress(address)}`
: 'Connect Ethereum'}
</button>
</div>
</div>
</div>

<button className="absolute bottom-10 w-60 text-white p-2 text-xl font-bold bg-she-pink rounded">
{!!addBookEntry ? 'Update Account' : 'Sign Up'}
<button
className={`shadow-md disabled:bg-slate-200 absolute bottom-10 w-60 text-white p-2 text-xl font-bold bg-she-pink rounded`}
disabled={!canSubmit}
onClick={onSubmit}
>
{updateSuccessfull
? '✅'
: !!addBookEntry
? 'Update Account'
: 'Sign Up'}
</button>

<a
Expand Down