diff --git a/helpers/github.js b/helpers/github.js index 50f3cb2..0a56e2f 100644 --- a/helpers/github.js +++ b/helpers/github.js @@ -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', @@ -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}`); @@ -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'); @@ -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', @@ -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); } diff --git a/next.config.js b/next.config.js index dce2124..3dd9755 100644 --- a/next.config.js +++ b/next.config.js @@ -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, }, }; diff --git a/pages/index.js b/pages/index.js index 168ca58..d7deb6a 100644 --- a/pages/index.js +++ b/pages/index.js @@ -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 @@ -118,30 +129,17 @@ export default function Home() { <>