From 1e0b2bfdab03c75ef9443e2cec2b81facb0fbdad Mon Sep 17 00:00:00 2001 From: Joshua Short Date: Tue, 13 Dec 2022 12:23:30 -0800 Subject: [PATCH 01/16] wip snip --- gatsby-config.js | 7 ------- 1 file changed, 7 deletions(-) diff --git a/gatsby-config.js b/gatsby-config.js index c36b0d0707..50f65729b5 100644 --- a/gatsby-config.js +++ b/gatsby-config.js @@ -19,13 +19,6 @@ const plugins = [ name: `orgs`, }, }, - { - resolve: `gatsby-source-filesystem`, - options: { - path: `${__dirname}/_data/handles`, - name: `handles`, - }, - }, { resolve: `gatsby-source-filesystem`, options: { From 9267a8e5baad6dc98a34b8238839a2587252ef1b Mon Sep 17 00:00:00 2001 From: Hannah Tuttle Date: Wed, 14 Dec 2022 10:56:34 +0000 Subject: [PATCH 02/16] added netlify function for call handles, updated judge application by removing hande calls, workin on register --- netlify/functions/handles.ts | 46 +++++++++++++ src/pages/judge-application.js | 37 +--------- src/pages/manage-team.tsx | 1 + src/pages/register-team.tsx | 1 + src/pages/register.tsx | 122 ++++++++++++++++++++------------- 5 files changed, 125 insertions(+), 82 deletions(-) create mode 100644 netlify/functions/handles.ts diff --git a/netlify/functions/handles.ts b/netlify/functions/handles.ts new file mode 100644 index 0000000000..a7d9389dc2 --- /dev/null +++ b/netlify/functions/handles.ts @@ -0,0 +1,46 @@ +import fs, { readFileSync, readdir, readdirSync } from "fs"; + +//need to set up the members portion of the handles data (i think the members field is for teams) +//so my plan is to add a call to get the teams data here +const getHandles = () => { + const allHandles : {handle : string, link : string, moralisId: string, image : string}[] = []; + const data = readdirSync(`./_data/handles`); + data.forEach((file) => { + if(file.endsWith('.json')){ + const buffer = readFileSync(`./_data/handles/${file}`); + const wardenFileData = JSON.parse(buffer.toString()); + if (wardenFileData.image) { + const imagePath = wardenFileData.image.slice(2); + wardenFileData.imageUrl = `https://raw.githubusercontent.com/${process.env.GITHUB_REPO_OWNER}/${process.env.REPO}/${process.env.BRANCH_NAME}/_data/handles/${imagePath}`; + } + allHandles.push({...wardenFileData}) + } + }); + + return allHandles; +} + +exports.handler = async (event) => { + // only allow GET + if (event.httpMethod !== "GET") { + return { + statusCode: 405, + body: "Method not allowed", + headers: { Allow: "GET" }, + }; + } + + try { + // handle + const allHandles = getHandles(); + return { + statusCode: 200, + body: JSON.stringify(allHandles), + }; + } catch (error) { + return { + statusCode: 500, + body: JSON.stringify({ error: error.toString(), details: error.stack }), + }; + } + }; \ No newline at end of file diff --git a/src/pages/judge-application.js b/src/pages/judge-application.js index 5db6ce8a21..be4aef2762 100644 --- a/src/pages/judge-application.js +++ b/src/pages/judge-application.js @@ -1,5 +1,5 @@ -import React, { useState } from "react"; -import { StaticQuery, graphql } from "gatsby"; +import React, { useState, useEffect } from "react"; +// import { StaticQuery, graphql } from "gatsby"; import Widgets from "../components/reporter/widgets/Widgets"; import useUser from "../hooks/UserContext"; @@ -87,26 +87,6 @@ const FormStatus = { Error: "error", }; -const wardenListQuery = graphql` - query WardenList { - allHandlesJson(sort: { fields: handle, order: ASC }) { - edges { - node { - id - handle - image { - childImageSharp { - resize(width: 80) { - src - } - } - } - } - } - } - } -`; - const JudgeApplication = () => { const [state, setState] = useState(initialState); const [status, setStatus] = useState("unsubmitted"); @@ -127,6 +107,7 @@ const JudgeApplication = () => { const url = `/.netlify/functions/apply-for-judge`; setStatus(FormStatus.Submitting); const sessionToken = user.attributes.sessionToken; + console.log(data); const response = await fetch(url, { method: "POST", headers: { @@ -168,15 +149,6 @@ const JudgeApplication = () => { }; return ( - { - const wardens = data.allHandlesJson.edges.map(({ node }) => { - return { value: node.handle, image: node.image }; - }); - fields[0].options = wardens; - - return ( { )} - ); - }} - /> ); }; diff --git a/src/pages/manage-team.tsx b/src/pages/manage-team.tsx index f353a7a7a0..71980930c3 100644 --- a/src/pages/manage-team.tsx +++ b/src/pages/manage-team.tsx @@ -216,6 +216,7 @@ export default function TeamManagement({ data, location }) { ); } +//update querry here for new enpoint related to handles export const query = graphql` query { handles: allHandlesJson(sort: { fields: handle, order: ASC }) { diff --git a/src/pages/register-team.tsx b/src/pages/register-team.tsx index d1b3501bcc..688b48235d 100644 --- a/src/pages/register-team.tsx +++ b/src/pages/register-team.tsx @@ -59,6 +59,7 @@ export default function TeamRegistration({ data }) { ); } +//update call here with new endpoint for dealing with handles export const query = graphql` query { handles: allHandlesJson(sort: { fields: handle, order: ASC }) { diff --git a/src/pages/register.tsx b/src/pages/register.tsx index 57692b89ed..4e6b8fb60c 100644 --- a/src/pages/register.tsx +++ b/src/pages/register.tsx @@ -1,5 +1,5 @@ import clsx from "clsx"; -import { graphql, navigate } from "gatsby"; +import { graphql,navigate } from "gatsby"; import Moralis from "moralis-v1"; import React, { useEffect, useState } from "react"; import { useMoralis } from "react-moralis"; @@ -12,41 +12,67 @@ import RegistrationForm from "../components/RegistrationForm"; import * as styles from "../components/form/Form.module.scss"; export default function UserRegistration({ data }) { - const handles = new Set(data.handles.edges.map((h) => h.node.handle)); + // const handles = new Set(data.handles.edges.map((h) => h.node.handle)); + const [handles, setHandles] = useState([]); const [wardens, setWardens] = useState([]); const { isInitialized } = useMoralis(); const { currentUser } = useUser(); + console.log(data.handles.edges[601].node.image); useEffect((): void => { if (currentUser.isLoggedIn) { navigate("/"); } }, [currentUser.isLoggedIn]); - useEffect((): void => { - async function filterWardens(): Promise { - if (!isInitialized) { - return; + +//this needs to be updated to use the handles from the netlify function. + + // useEffect((): void => { + // async function filterWardens(): Promise { + // if (!isInitialized) { + // return; + // } + // const wardensWithSubmissions = await Moralis.Cloud.run( + // "getWardensWithSubmissions" + // ); + // console.log(data.handles.edges); + // const wardens = data.handles.edges + // .filter(({ node }) => { + // if (node.members) { + // return false; + // } + // if (wardensWithSubmissions.includes(node.handle)) { + // return false; + // } + // return true; + // }) + // .map(({ node }) => ({ value: node.handle, image: node.image })); + // setWardens(wardens); + // } + // filterWardens(); + // }, [wardens, isInitialized]); + + // this is for getting handles from netlify function. + useEffect(() => { + (async () => { + const result = await fetch(`/.netlify/functions/handles`, { + headers: { + "Content-Type": "application/json", + }, + }).then((res) => res.json()) + .then((res) => { + return res; + }); + if (result) { + setHandles(result); + } else { + throw "Unable to fetch handle results."; } - const wardensWithSubmissions = await Moralis.Cloud.run( - "getWardensWithSubmissions" - ); + })(); + }, []); - const wardens = data.handles.edges - .filter(({ node }) => { - if (node.members) { - return false; - } - if (wardensWithSubmissions.includes(node.handle)) { - return false; - } - return true; - }) - .map(({ node }) => ({ value: node.handle, image: node.image })); - setWardens(wardens); - } - filterWardens(); - }, [data, isInitialized]); + return ( ); } - -export const query = graphql` - query { - handles: allHandlesJson(sort: { fields: handle, order: ASC }) { - edges { - node { - handle - link - moralisId - members { - handle - } - image { - childImageSharp { - resize(width: 80) { - src - } - } - } - } - } - } - } -`; +// update call here with new endpoint for handles +// export const query = graphql` +// query { +// handles: allHandlesJson(sort: { fields: handle, order: ASC }) { +// edges { +// node { +// handle +// link +// moralisId +// members { +// handle +// } +// image { +// childImageSharp { +// resize(width: 80) { +// src +// } +// } +// } +// } +// } +// } +// } +// `; From aa6087dba40775148a27ce2cfc0ccba48573219b Mon Sep 17 00:00:00 2001 From: Joshua Short Date: Thu, 15 Dec 2022 02:46:15 -0800 Subject: [PATCH 03/16] cleanup judge page --- src/pages/judge-application.js | 186 ++++++++++++++++----------------- 1 file changed, 92 insertions(+), 94 deletions(-) diff --git a/src/pages/judge-application.js b/src/pages/judge-application.js index be4aef2762..071ac8364e 100644 --- a/src/pages/judge-application.js +++ b/src/pages/judge-application.js @@ -1,5 +1,4 @@ -import React, { useState, useEffect } from "react"; -// import { StaticQuery, graphql } from "gatsby"; +import React, { useState } from "react"; import Widgets from "../components/reporter/widgets/Widgets"; import useUser from "../hooks/UserContext"; @@ -107,7 +106,6 @@ const JudgeApplication = () => { const url = `/.netlify/functions/apply-for-judge`; setStatus(FormStatus.Submitting); const sessionToken = user.attributes.sessionToken; - console.log(data); const response = await fetch(url, { method: "POST", headers: { @@ -149,101 +147,101 @@ const JudgeApplication = () => { }; return ( - -
- {(status === FormStatus.Unsubmitted || - status === FormStatus.Submitting) && ( - <> -

Judge Application

-
- {" "} - {status === FormStatus.Error && ( -
-

- An error occurred; please try again. If you continue - to receive this error, let us know in{" "} - Discord. - All fields are required. -

-
- )} - - - - )} - - {status === FormStatus.Submitted && ( -
-

Thanks for applying!

-

- Here's what happens next: -

-
    -
  1. - Judge applications are reviewed by the C4 judge selection - committee, which includes top leaderboard wardens and past - judges. The committee will review your application and - give you a "yes" or "not yet". -
  2. -
  3. - The review process begins after the application window - closes, and we expect it to take about a week, depending - on the number of applications the committee receives. -
  4. -
  5. - You'll be contacted via DM to let you know if your - application has been successful this time around. -
  6. -
  7. - If you're accepted as a judge, an organizer will onboard - you and get you set up to judge your first contest! -
  8. -
+ +
+ {(status === FormStatus.Unsubmitted || + status === FormStatus.Submitting) && ( + <> +

Judge Application

+
+ {" "} + {status === FormStatus.Error && ( +

- In the meantime, if you have questions, feel free to reach - out to us in the C4 Discord, or have a closer look at the{" "} - - How to judge a contest - {" "} - section in the Code4rena docs. + An error occurred; please try again. If you continue + to receive this error, let us know in{" "} + Discord. + All fields are required.

)} - {status === FormStatus.Error && ( -
-

Whoops!

-

An error occurred while processing your application.

- {errorMessage &&

{errorMessage}

} - -
- )} -
-
+ + + + )} + + {status === FormStatus.Submitted && ( +
+

Thanks for applying!

+

+ Here's what happens next: +

+
    +
  1. + Judge applications are reviewed by the C4 judge selection + committee, which includes top leaderboard wardens and past + judges. The committee will review your application and + give you a "yes" or "not yet". +
  2. +
  3. + The review process begins after the application window + closes, and we expect it to take about a week, depending + on the number of applications the committee receives. +
  4. +
  5. + You'll be contacted via DM to let you know if your + application has been successful this time around. +
  6. +
  7. + If you're accepted as a judge, an organizer will onboard + you and get you set up to judge your first contest! +
  8. +
+

+ In the meantime, if you have questions, feel free to reach + out to us in the C4 Discord, or have a closer look at the{" "} + + How to judge a contest + {" "} + section in the Code4rena docs. +

+
+ )} + {status === FormStatus.Error && ( +
+

Whoops!

+

An error occurred while processing your application.

+ {errorMessage &&

{errorMessage}

} + +
+ )} +
+ ); }; From 857833712a4dbe3d7ab284d0c523797d94f8ec55 Mon Sep 17 00:00:00 2001 From: Joshua Short Date: Thu, 15 Dec 2022 03:29:49 -0800 Subject: [PATCH 04/16] Disable query in manage-team --- src/pages/manage-team.tsx | 72 +++++++++++++++++++-------------------- 1 file changed, 35 insertions(+), 37 deletions(-) diff --git a/src/pages/manage-team.tsx b/src/pages/manage-team.tsx index 71980930c3..dab8455c3c 100644 --- a/src/pages/manage-team.tsx +++ b/src/pages/manage-team.tsx @@ -1,4 +1,3 @@ -import { graphql } from "gatsby"; import isEqual from "lodash/isEqual"; import React, { useCallback, useEffect, useState } from "react"; @@ -29,6 +28,41 @@ export default function TeamManagement({ data, location }) { WardenFieldOption[] | undefined >(); + // XXX: switching from gatsby graph + const [handles, setHandles] = useState>(new Set()); + const [wardens, setWardens] = useState([]); + + // fetch wardens + useEffect(() => { + // // fetch get-user + // (async () => { + // const result = await fetch(`/.netlify/functions/get-user`, { + // headers: { + // "Content-Type": "application/json", + // // "X-Authorization": `Bearer ${sessionToken}`, + // // "C4-User": currentUser.username, + // }, + // }); + // if (result.ok) { + // let users = await result.json(); + + // let handles = new Set(users.slice().map((e) => e.handle)); + // setHandles(handles); + + // let wardens = users.slice.map((e) => { + // return { + // value: e.handle, + // image: e.image ?? "", + // } + // }); + // setWardens(wardens); + // } else { + // // @TODO: what to do here? + // throw "Unable to fetch leaderboard results."; + // } + // })(); + }, []); + useEffect(() => { // @todo: show error message when user tries to manage a team they are not on (async () => { @@ -95,17 +129,6 @@ export default function TeamManagement({ data, location }) { setTeamState(state); }; - const handles: Set = new Set( - data.handles.edges.map((h) => h.node.handle) - ); - - let wardens: { value: string; image: unknown }[] = []; - data.handles.edges.forEach(({ node }) => { - if (!node.members) { - wardens.push({ value: node.handle, image: node.image }); - } - }); - const onSubmit = useCallback( async (data: TeamCreateRequest, user) => { const sessionToken = user.attributes.sessionToken; @@ -215,28 +238,3 @@ export default function TeamManagement({ data, location }) { ); } - -//update querry here for new enpoint related to handles -export const query = graphql` - query { - handles: allHandlesJson(sort: { fields: handle, order: ASC }) { - edges { - node { - handle - link - moralisId - members { - handle - } - image { - childImageSharp { - resize(width: 80) { - src - } - } - } - } - } - } - } -`; From 70eddfc90d28c9f8b81efeb3a0dca7d5e5fa1496 Mon Sep 17 00:00:00 2001 From: Joshua Short Date: Thu, 15 Dec 2022 03:30:15 -0800 Subject: [PATCH 05/16] Disable query in register-team --- src/pages/register-team.tsx | 71 ++++++++++++++++++------------------- 1 file changed, 35 insertions(+), 36 deletions(-) diff --git a/src/pages/register-team.tsx b/src/pages/register-team.tsx index 688b48235d..17ef9744d6 100644 --- a/src/pages/register-team.tsx +++ b/src/pages/register-team.tsx @@ -1,5 +1,4 @@ -import { graphql } from "gatsby"; -import React, { useCallback } from "react"; +import React, { useCallback, useEffect, useState } from "react"; import useUser from "../hooks/UserContext"; import ProtectedPage from "../components/ProtectedPage"; @@ -7,16 +6,41 @@ import TeamForm from "../components/TeamForm"; export default function TeamRegistration({ data }) { const { currentUser } = useUser(); - const handles: Set = new Set( - data.handles.edges.map((h) => h.node.handle) - ); - let wardens: { value: string; image: unknown }[] = []; - data.handles.edges.forEach(({ node }) => { - if (!node.members) { - wardens.push({ value: node.handle, image: node.image }); - } - }); + // XXX: switching from gatsby graph + const [handles, setHandles] = useState>(new Set()); + const [wardens, setWardens] = useState([]); + + // fetch wardens + useEffect(() => { + // // fetch get-user + // (async () => { + // const result = await fetch(`/.netlify/functions/get-user`, { + // headers: { + // "Content-Type": "application/json", + // // "X-Authorization": `Bearer ${sessionToken}`, + // // "C4-User": currentUser.username, + // }, + // }); + // if (result.ok) { + // let users = await result.json(); + + // let handles = new Set(users.slice().map((e) => e.handle)); + // setHandles(handles); + + // let wardens = users.slice.map((e) => { + // return { + // value: e.handle, + // image: e.image ?? "", + // } + // }); + // setWardens(wardens); + // } else { + // // @TODO: what to do here? + // throw "Unable to fetch leaderboard results."; + // } + // })(); + }, []); const onSubmit = useCallback( async (requestBody, user) => { @@ -58,28 +82,3 @@ export default function TeamRegistration({ data }) { ); } - -//update call here with new endpoint for dealing with handles -export const query = graphql` - query { - handles: allHandlesJson(sort: { fields: handle, order: ASC }) { - edges { - node { - handle - link - moralisId - members { - handle - } - image { - childImageSharp { - resize(width: 80) { - src - } - } - } - } - } - } - } -`; From 144f87602d8cff8fb19cb37b95f2ed5a64a12c8a Mon Sep 17 00:00:00 2001 From: Joshua Short Date: Thu, 15 Dec 2022 03:33:34 -0800 Subject: [PATCH 06/16] wip get-user endpoint full list --- netlify/functions/get-user.ts | 12 +++++++++--- netlify/util/user-utils.ts | 13 ++++++++++++- 2 files changed, 21 insertions(+), 4 deletions(-) diff --git a/netlify/functions/get-user.ts b/netlify/functions/get-user.ts index 80b388fbd7..df68a05d3d 100644 --- a/netlify/functions/get-user.ts +++ b/netlify/functions/get-user.ts @@ -1,4 +1,4 @@ -import { findUser } from "../util/user-utils"; +import { findUser, getUsers } from "../util/user-utils"; exports.handler = async (event) => { // only allow GET @@ -12,11 +12,17 @@ exports.handler = async (event) => { const userHandle = event.queryStringParameters.id; try { - const user = await findUser(userHandle); + let res; + if (userHandle === undefined) { + res = getUsers(); + } + else { + res = await findUser(userHandle); + } return { statusCode: 200, - body: JSON.stringify(user), + body: JSON.stringify(res), }; } catch (error) { return { diff --git a/netlify/util/user-utils.ts b/netlify/util/user-utils.ts index 8239dbca74..e2f87ed916 100644 --- a/netlify/util/user-utils.ts +++ b/netlify/util/user-utils.ts @@ -1,4 +1,4 @@ -import { readFileSync } from "fs"; +import { readFileSync, readdirSync } from "fs"; import Moralis from "moralis-v1/node"; import fetch from "node-fetch"; import uniq from "lodash/uniq"; @@ -43,6 +43,17 @@ export async function findUser(userHandle) { } } +export async function getUsers() { + const data = readdirSync(`./_data/handles`); + // just return the objects? + // without memebers +} + +export async function getTeams() { + // just return the objects? + // with memebers +} + export async function getUserTeams(username: string): Promise { let teamHandles: string[] = []; From bef3800f7a40fa827398fd13fe41157c22df3b48 Mon Sep 17 00:00:00 2001 From: Hannah Tuttle Date: Thu, 15 Dec 2022 17:24:23 +0000 Subject: [PATCH 07/16] testing new handles --- netlify/functions/handles.ts | 1 + src/pages/register.tsx | 55 ++++++++++++++++++------------------ 2 files changed, 28 insertions(+), 28 deletions(-) diff --git a/netlify/functions/handles.ts b/netlify/functions/handles.ts index a7d9389dc2..0e37571c4f 100644 --- a/netlify/functions/handles.ts +++ b/netlify/functions/handles.ts @@ -13,6 +13,7 @@ const getHandles = () => { const imagePath = wardenFileData.image.slice(2); wardenFileData.imageUrl = `https://raw.githubusercontent.com/${process.env.GITHUB_REPO_OWNER}/${process.env.REPO}/${process.env.BRANCH_NAME}/_data/handles/${imagePath}`; } + if(wardenFileData.members){console.log('warden exists', wardenFileData)} allHandles.push({...wardenFileData}) } }); diff --git a/src/pages/register.tsx b/src/pages/register.tsx index 4e6b8fb60c..204be6f52b 100644 --- a/src/pages/register.tsx +++ b/src/pages/register.tsx @@ -12,13 +12,12 @@ import RegistrationForm from "../components/RegistrationForm"; import * as styles from "../components/form/Form.module.scss"; export default function UserRegistration({ data }) { - // const handles = new Set(data.handles.edges.map((h) => h.node.handle)); - const [handles, setHandles] = useState([]); + const handles = new Set(data.handles.edges.map((h) => h.node.handle)); + const [handlesData, setHandlesData] = useState([]); const [wardens, setWardens] = useState([]); const { isInitialized } = useMoralis(); const { currentUser } = useUser(); - console.log(data.handles.edges[601].node.image); useEffect((): void => { if (currentUser.isLoggedIn) { navigate("/"); @@ -28,30 +27,30 @@ export default function UserRegistration({ data }) { //this needs to be updated to use the handles from the netlify function. - // useEffect((): void => { - // async function filterWardens(): Promise { - // if (!isInitialized) { - // return; - // } - // const wardensWithSubmissions = await Moralis.Cloud.run( - // "getWardensWithSubmissions" - // ); - // console.log(data.handles.edges); - // const wardens = data.handles.edges - // .filter(({ node }) => { - // if (node.members) { - // return false; - // } - // if (wardensWithSubmissions.includes(node.handle)) { - // return false; - // } - // return true; - // }) - // .map(({ node }) => ({ value: node.handle, image: node.image })); - // setWardens(wardens); - // } - // filterWardens(); - // }, [wardens, isInitialized]); + useEffect((): void => { + async function filterWardens(): Promise { + if (!isInitialized) { + return; + } + const wardensWithSubmissions = await Moralis.Cloud.run( + "getWardensWithSubmissions" + ); + console.log(data.handles.edges); + const wardens = data.handles.edges + .filter(({ node }) => { + if (node.members) { + return false; + } + if (wardensWithSubmissions.includes(node.handle)) { + return false; + } + return true; + }) + .map(({ node }) => ({ value: node.handle, image: node.image })); + setWardens(wardens); + } + filterWardens(); + }, [wardens, isInitialized]); // this is for getting handles from netlify function. useEffect(() => { @@ -65,7 +64,7 @@ export default function UserRegistration({ data }) { return res; }); if (result) { - setHandles(result); + setHandlesData(result); } else { throw "Unable to fetch handle results."; } From 4b3c53f884c3dc30b0215e06b4ac85ecfb95df83 Mon Sep 17 00:00:00 2001 From: Hannah Tuttle Date: Sun, 18 Dec 2022 11:27:49 +0000 Subject: [PATCH 08/16] Merge branch '6039-feature-remove-handles-from-graph' of https://github.com/code-423n4/code423n4.com into 6039-feature-remove-handles-from-graph --- _data/handles/D-squared.json | 6 ------ 1 file changed, 6 deletions(-) delete mode 100644 _data/handles/D-squared.json diff --git a/_data/handles/D-squared.json b/_data/handles/D-squared.json deleted file mode 100644 index b1016d1da4..0000000000 --- a/_data/handles/D-squared.json +++ /dev/null @@ -1,6 +0,0 @@ -{ - "handle": "D-squared", - "link": "https://www.youtube.com/@d-squared8469", - "moralisId": "6DWdGUGwciq4ryQ4AJHm3q6k", - "image": "./avatars/D-squared.jpeg" -} \ No newline at end of file From 78757b5110af27199cda46c820247320990caab5 Mon Sep 17 00:00:00 2001 From: Hannah Tuttle Date: Sun, 18 Dec 2022 13:15:33 +0000 Subject: [PATCH 09/16] updated register.tsx to use netlify handles --- netlify/functions/handles.ts | 1 - src/pages/register.tsx | 56 +++++++++++------------------------- 2 files changed, 16 insertions(+), 41 deletions(-) diff --git a/netlify/functions/handles.ts b/netlify/functions/handles.ts index 0e37571c4f..a7d9389dc2 100644 --- a/netlify/functions/handles.ts +++ b/netlify/functions/handles.ts @@ -13,7 +13,6 @@ const getHandles = () => { const imagePath = wardenFileData.image.slice(2); wardenFileData.imageUrl = `https://raw.githubusercontent.com/${process.env.GITHUB_REPO_OWNER}/${process.env.REPO}/${process.env.BRANCH_NAME}/_data/handles/${imagePath}`; } - if(wardenFileData.members){console.log('warden exists', wardenFileData)} allHandles.push({...wardenFileData}) } }); diff --git a/src/pages/register.tsx b/src/pages/register.tsx index 204be6f52b..ac71e379b1 100644 --- a/src/pages/register.tsx +++ b/src/pages/register.tsx @@ -1,5 +1,5 @@ import clsx from "clsx"; -import { graphql,navigate } from "gatsby"; +import { navigate } from "gatsby"; import Moralis from "moralis-v1"; import React, { useEffect, useState } from "react"; import { useMoralis } from "react-moralis"; @@ -11,10 +11,10 @@ import RegistrationForm from "../components/RegistrationForm"; import * as styles from "../components/form/Form.module.scss"; -export default function UserRegistration({ data }) { - const handles = new Set(data.handles.edges.map((h) => h.node.handle)); - const [handlesData, setHandlesData] = useState([]); - const [wardens, setWardens] = useState([]); +export default function UserRegistration() { + const [handles, setHandles] = useState>(new Set()); + const [wardens, setWardens] = useState<{value: any; image: any; }[]> ([]); + const [handleData, setHandleData] = useState([]); const { isInitialized } = useMoralis(); const { currentUser } = useUser(); @@ -24,9 +24,8 @@ export default function UserRegistration({ data }) { } }, [currentUser.isLoggedIn]); - -//this needs to be updated to use the handles from the netlify function. +//do we need this function?? It setting wardens with a filtered set of wardens but then wardens is never used?? What am I missing?? useEffect((): void => { async function filterWardens(): Promise { if (!isInitialized) { @@ -35,22 +34,21 @@ export default function UserRegistration({ data }) { const wardensWithSubmissions = await Moralis.Cloud.run( "getWardensWithSubmissions" ); - console.log(data.handles.edges); - const wardens = data.handles.edges - .filter(({ node }) => { - if (node.members) { + const wardens = handleData + .filter((warden) => { + if (warden.warden) { return false; } - if (wardensWithSubmissions.includes(node.handle)) { + if (wardensWithSubmissions.includes(warden.handle)) { return false; } return true; }) - .map(({ node }) => ({ value: node.handle, image: node.image })); + .map(( warden ) => ({ value: warden.handle, image: warden.image })); setWardens(wardens); } filterWardens(); - }, [wardens, isInitialized]); + }, [isInitialized, handleData]); // this is for getting handles from netlify function. useEffect(() => { @@ -64,12 +62,13 @@ export default function UserRegistration({ data }) { return res; }); if (result) { - setHandlesData(result); + setHandles(new Set(result.map((h) => h.handle))); + setHandleData(result); } else { throw "Unable to fetch handle results."; } })(); - }, []); + }, [isInitialized]); @@ -87,27 +86,4 @@ export default function UserRegistration({ data }) { ); } -// update call here with new endpoint for handles -// export const query = graphql` -// query { -// handles: allHandlesJson(sort: { fields: handle, order: ASC }) { -// edges { -// node { -// handle -// link -// moralisId -// members { -// handle -// } -// image { -// childImageSharp { -// resize(width: 80) { -// src -// } -// } -// } -// } -// } -// } -// } -// `; + From 25f945ead5bf17ff6a0ce7bb876082a64504d727 Mon Sep 17 00:00:00 2001 From: Hannah Tuttle Date: Sun, 18 Dec 2022 13:59:18 +0000 Subject: [PATCH 10/16] updated register team to use handles endpoint --- src/pages/register-team.tsx | 55 ++++++++++++++++++------------------- src/pages/register.tsx | 2 +- 2 files changed, 28 insertions(+), 29 deletions(-) diff --git a/src/pages/register-team.tsx b/src/pages/register-team.tsx index 17ef9744d6..1595dd60b5 100644 --- a/src/pages/register-team.tsx +++ b/src/pages/register-team.tsx @@ -4,42 +4,41 @@ import useUser from "../hooks/UserContext"; import ProtectedPage from "../components/ProtectedPage"; import TeamForm from "../components/TeamForm"; -export default function TeamRegistration({ data }) { +export default function TeamRegistration() { const { currentUser } = useUser(); // XXX: switching from gatsby graph const [handles, setHandles] = useState>(new Set()); - const [wardens, setWardens] = useState([]); + const [wardens, setWardens] = useState<{value: any; image: any; }[]>([]); // fetch wardens useEffect(() => { - // // fetch get-user - // (async () => { - // const result = await fetch(`/.netlify/functions/get-user`, { - // headers: { - // "Content-Type": "application/json", - // // "X-Authorization": `Bearer ${sessionToken}`, - // // "C4-User": currentUser.username, - // }, - // }); - // if (result.ok) { - // let users = await result.json(); - - // let handles = new Set(users.slice().map((e) => e.handle)); - // setHandles(handles); + // // fetch handles + (async () => { + const result = await fetch(`/.netlify/functions/handles`, { + headers: { + "Content-Type": "application/json", + // "X-Authorization": `Bearer ${sessionToken}`, + // "C4-User": currentUser.username, + }, + }); + if (result.ok) { + let users = await result.json(); + let handles : Set = new Set(users.map((e) => e.handle)); + setHandles(handles); - // let wardens = users.slice.map((e) => { - // return { - // value: e.handle, - // image: e.image ?? "", - // } - // }); - // setWardens(wardens); - // } else { - // // @TODO: what to do here? - // throw "Unable to fetch leaderboard results."; - // } - // })(); + let wardens : {value: any; image: any; }[] = users.map((e) => { + return { + value: e.handle, + image: e.image ?? "", + } + }); + setWardens(wardens); + } else { + // @TODO: what to do here? + throw "Unable to fetch user data."; + } + })(); }, []); const onSubmit = useCallback( diff --git a/src/pages/register.tsx b/src/pages/register.tsx index ac71e379b1..b7b7829520 100644 --- a/src/pages/register.tsx +++ b/src/pages/register.tsx @@ -44,7 +44,7 @@ export default function UserRegistration() { } return true; }) - .map(( warden ) => ({ value: warden.handle, image: warden.image })); + .map(( warden ) => ({ value: warden.handle, image: warden.image ?? "" })); setWardens(wardens); } filterWardens(); From fddfc0fb1c7636c6c740ea0f07249fe098ea8e1e Mon Sep 17 00:00:00 2001 From: Hannah Tuttle Date: Sun, 18 Dec 2022 14:16:15 +0000 Subject: [PATCH 11/16] updated manage teams, started testing the get-user function, updated get users and get teams in user utils --- netlify/functions/handles.ts | 4 +-- netlify/util/user-utils.ts | 38 ++++++++++++++++++++++++-- src/pages/manage-team.tsx | 53 ++++++++++++++++++------------------ 3 files changed, 64 insertions(+), 31 deletions(-) diff --git a/netlify/functions/handles.ts b/netlify/functions/handles.ts index a7d9389dc2..5a52d37b5c 100644 --- a/netlify/functions/handles.ts +++ b/netlify/functions/handles.ts @@ -3,7 +3,7 @@ import fs, { readFileSync, readdir, readdirSync } from "fs"; //need to set up the members portion of the handles data (i think the members field is for teams) //so my plan is to add a call to get the teams data here const getHandles = () => { - const allHandles : {handle : string, link : string, moralisId: string, image : string}[] = []; + const allHandles : {handle : string, link : string, moralisId: string, image : string, members: string[], }[] = []; const data = readdirSync(`./_data/handles`); data.forEach((file) => { if(file.endsWith('.json')){ @@ -13,7 +13,7 @@ const getHandles = () => { const imagePath = wardenFileData.image.slice(2); wardenFileData.imageUrl = `https://raw.githubusercontent.com/${process.env.GITHUB_REPO_OWNER}/${process.env.REPO}/${process.env.BRANCH_NAME}/_data/handles/${imagePath}`; } - allHandles.push({...wardenFileData}) + allHandles.push({...wardenFileData}) } }); diff --git a/netlify/util/user-utils.ts b/netlify/util/user-utils.ts index e2f87ed916..d2f325d66b 100644 --- a/netlify/util/user-utils.ts +++ b/netlify/util/user-utils.ts @@ -44,14 +44,48 @@ export async function findUser(userHandle) { } export async function getUsers() { + const allHandles : {handle : string, link : string, moralisId: string, image : string}[] = []; const data = readdirSync(`./_data/handles`); // just return the objects? - // without memebers + // without members + data.forEach((file) => { + if(file.endsWith('.json')){ + const buffer = readFileSync(`./_data/handles/${file}`); + const wardenFileData = JSON.parse(buffer.toString()); + if (wardenFileData.image) { + const imagePath = wardenFileData.image.slice(2); + wardenFileData.imageUrl = `https://raw.githubusercontent.com/${process.env.GITHUB_REPO_OWNER}/${process.env.REPO}/${process.env.BRANCH_NAME}/_data/handles/${imagePath}`; + } + if(!wardenFileData.members){ + allHandles.push({...wardenFileData}) + } + } +}); +return allHandles; } export async function getTeams() { // just return the objects? - // with memebers + // with members + const teamHandles : {handle : string, link : string, moralisId: string, image : string}[] = []; + const data = readdirSync(`./_data/handles`); + // just return the objects? + // without members + data.forEach((file) => { + if(file.endsWith('.json')){ + const buffer = readFileSync(`./_data/handles/${file}`); + const wardenFileData = JSON.parse(buffer.toString()); + if (wardenFileData.image) { + const imagePath = wardenFileData.image.slice(2); + wardenFileData.imageUrl = `https://raw.githubusercontent.com/${process.env.GITHUB_REPO_OWNER}/${process.env.REPO}/${process.env.BRANCH_NAME}/_data/handles/${imagePath}`; + } + if(wardenFileData.members){ + teamHandles.push({...wardenFileData}) + } + } +}); + +return teamHandles; } export async function getUserTeams(username: string): Promise { diff --git a/src/pages/manage-team.tsx b/src/pages/manage-team.tsx index dab8455c3c..8c3773bac3 100644 --- a/src/pages/manage-team.tsx +++ b/src/pages/manage-team.tsx @@ -19,7 +19,7 @@ import { WardenFieldOption } from "../components/reporter/widgets/WardenField"; // styles import * as styles from "../components/form/Form.module.scss"; -export default function TeamManagement({ data, location }) { +export default function TeamManagement({ location }) { const { currentUser } = useUser(); const [unauthorized, setIsUnauthorized] = useState(false); @@ -30,37 +30,36 @@ export default function TeamManagement({ data, location }) { // XXX: switching from gatsby graph const [handles, setHandles] = useState>(new Set()); - const [wardens, setWardens] = useState([]); + const [wardens, setWardens] = useState<{value: any; image: any; }[]>([]); // fetch wardens useEffect(() => { // // fetch get-user - // (async () => { - // const result = await fetch(`/.netlify/functions/get-user`, { - // headers: { - // "Content-Type": "application/json", - // // "X-Authorization": `Bearer ${sessionToken}`, - // // "C4-User": currentUser.username, - // }, - // }); - // if (result.ok) { - // let users = await result.json(); - - // let handles = new Set(users.slice().map((e) => e.handle)); - // setHandles(handles); + (async () => { + const result = await fetch(`/.netlify/functions/handles`, { + headers: { + "Content-Type": "application/json", + // "X-Authorization": `Bearer ${sessionToken}`, + // "C4-User": currentUser.username, + }, + }); + if (result.ok) { + let users = await result.json(); + let handles : Set = new Set(users.map((e) => e.handle)); + setHandles(handles); - // let wardens = users.slice.map((e) => { - // return { - // value: e.handle, - // image: e.image ?? "", - // } - // }); - // setWardens(wardens); - // } else { - // // @TODO: what to do here? - // throw "Unable to fetch leaderboard results."; - // } - // })(); + let wardens : {value: any; image: any; }[] = users.map((e) => { + return { + value: e.handle, + image: e.image ?? "", + } + }); + setWardens(wardens); + } else { + // @TODO: what to do here? + throw "Unable to fetch user data."; + } + })(); }, []); useEffect(() => { From ce048f6499f8518998e60db66941d584510a84c7 Mon Sep 17 00:00:00 2001 From: Hannah Tuttle Date: Mon, 19 Dec 2022 15:38:12 +0000 Subject: [PATCH 12/16] added file back in that was deleted durig the last merge --- _data/handles/D-squared.json | 6 ++++++ 1 file changed, 6 insertions(+) create mode 100644 _data/handles/D-squared.json diff --git a/_data/handles/D-squared.json b/_data/handles/D-squared.json new file mode 100644 index 0000000000..e1e67ff1fd --- /dev/null +++ b/_data/handles/D-squared.json @@ -0,0 +1,6 @@ +{ + "handle": "D-squared", + "link": "https://www.youtube.com/@d-squared8469", + "moralisId": "6DWdGUGwciq4ryQ4AJHm3q6k", + "image": "./avatars/D-squared.jpeg" +} \ No newline at end of file From 59b669c736d8923c3b144262807ddf980dfd4a0c Mon Sep 17 00:00:00 2001 From: Hannah Tuttle Date: Mon, 19 Dec 2022 15:49:46 +0000 Subject: [PATCH 13/16] added formatting to json handle --- _data/handles/D-squared.json | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/_data/handles/D-squared.json b/_data/handles/D-squared.json index e1e67ff1fd..fbf9d00c45 100644 --- a/_data/handles/D-squared.json +++ b/_data/handles/D-squared.json @@ -1,6 +1,6 @@ { - "handle": "D-squared", - "link": "https://www.youtube.com/@d-squared8469", - "moralisId": "6DWdGUGwciq4ryQ4AJHm3q6k", - "image": "./avatars/D-squared.jpeg" -} \ No newline at end of file + "handle": "D-squared", + "link": "https://www.youtube.com/@d-squared8469", + "moralisId": "6DWdGUGwciq4ryQ4AJHm3q6k", + "image": "./avatars/D-squared.jpeg" +} From aaaaf28b4460ceabac33a2a2c8a05d505b08c7ce Mon Sep 17 00:00:00 2001 From: Hannah Tuttle Date: Mon, 19 Dec 2022 15:50:41 +0000 Subject: [PATCH 14/16] removed extra space --- _data/handles/D-squared.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/_data/handles/D-squared.json b/_data/handles/D-squared.json index fbf9d00c45..b1016d1da4 100644 --- a/_data/handles/D-squared.json +++ b/_data/handles/D-squared.json @@ -3,4 +3,4 @@ "link": "https://www.youtube.com/@d-squared8469", "moralisId": "6DWdGUGwciq4ryQ4AJHm3q6k", "image": "./avatars/D-squared.jpeg" -} +} \ No newline at end of file From 67f2757ba6e067326bb4cd3630e57d120108db34 Mon Sep 17 00:00:00 2001 From: simon busch Date: Tue, 24 Jan 2023 13:40:49 +0100 Subject: [PATCH 15/16] [UPD] identation --- netlify/functions/get-user.ts | 3 +- netlify/functions/handles.ts | 84 ++++++++++++++++++---------------- netlify/util/user-utils.ts | 62 ++++++++++++++----------- src/pages/judge-application.js | 36 +++++++-------- src/pages/manage-team.tsx | 52 ++++++++++----------- src/pages/register-team.tsx | 10 ++-- src/pages/register.tsx | 19 ++++---- 7 files changed, 137 insertions(+), 129 deletions(-) diff --git a/netlify/functions/get-user.ts b/netlify/functions/get-user.ts index df68a05d3d..cace787fbd 100644 --- a/netlify/functions/get-user.ts +++ b/netlify/functions/get-user.ts @@ -15,8 +15,7 @@ exports.handler = async (event) => { let res; if (userHandle === undefined) { res = getUsers(); - } - else { + } else { res = await findUser(userHandle); } diff --git a/netlify/functions/handles.ts b/netlify/functions/handles.ts index 5a52d37b5c..b3fdc67a90 100644 --- a/netlify/functions/handles.ts +++ b/netlify/functions/handles.ts @@ -1,46 +1,52 @@ -import fs, { readFileSync, readdir, readdirSync } from "fs"; +import fs, { readFileSync, readdirSync } from "fs"; //need to set up the members portion of the handles data (i think the members field is for teams) //so my plan is to add a call to get the teams data here const getHandles = () => { - const allHandles : {handle : string, link : string, moralisId: string, image : string, members: string[], }[] = []; - const data = readdirSync(`./_data/handles`); - data.forEach((file) => { - if(file.endsWith('.json')){ - const buffer = readFileSync(`./_data/handles/${file}`); - const wardenFileData = JSON.parse(buffer.toString()); - if (wardenFileData.image) { - const imagePath = wardenFileData.image.slice(2); - wardenFileData.imageUrl = `https://raw.githubusercontent.com/${process.env.GITHUB_REPO_OWNER}/${process.env.REPO}/${process.env.BRANCH_NAME}/_data/handles/${imagePath}`; - } - allHandles.push({...wardenFileData}) - } - }); + const allHandles: { + handle: string; + link: string; + moralisId: string; + image: string; + members: string[]; + }[] = []; + const data = readdirSync(`./_data/handles`); + data.forEach((file) => { + if (file.endsWith(".json")) { + const buffer = readFileSync(`./_data/handles/${file}`); + const wardenFileData = JSON.parse(buffer.toString()); + if (wardenFileData.image) { + const imagePath = wardenFileData.image.slice(2); + wardenFileData.imageUrl = `https://raw.githubusercontent.com/${process.env.GITHUB_REPO_OWNER}/${process.env.REPO}/${process.env.BRANCH_NAME}/_data/handles/${imagePath}`; + } + allHandles.push({ ...wardenFileData }); + } + }); - return allHandles; -} + return allHandles; +}; exports.handler = async (event) => { - // only allow GET - if (event.httpMethod !== "GET") { - return { - statusCode: 405, - body: "Method not allowed", - headers: { Allow: "GET" }, - }; - } - - try { - // handle - const allHandles = getHandles(); - return { - statusCode: 200, - body: JSON.stringify(allHandles), - }; - } catch (error) { - return { - statusCode: 500, - body: JSON.stringify({ error: error.toString(), details: error.stack }), - }; - } - }; \ No newline at end of file + // only allow GET + if (event.httpMethod !== "GET") { + return { + statusCode: 405, + body: "Method not allowed", + headers: { Allow: "GET" }, + }; + } + + try { + // handle + const allHandles = getHandles(); + return { + statusCode: 200, + body: JSON.stringify(allHandles), + }; + } catch (error) { + return { + statusCode: 500, + body: JSON.stringify({ error: error.toString(), details: error.stack }), + }; + } +}; diff --git a/netlify/util/user-utils.ts b/netlify/util/user-utils.ts index d2f325d66b..81609fa1e6 100644 --- a/netlify/util/user-utils.ts +++ b/netlify/util/user-utils.ts @@ -44,48 +44,58 @@ export async function findUser(userHandle) { } export async function getUsers() { - const allHandles : {handle : string, link : string, moralisId: string, image : string}[] = []; + const allHandles: { + handle: string; + link: string; + moralisId: string; + image: string; + }[] = []; const data = readdirSync(`./_data/handles`); // just return the objects? // without members data.forEach((file) => { - if(file.endsWith('.json')){ - const buffer = readFileSync(`./_data/handles/${file}`); - const wardenFileData = JSON.parse(buffer.toString()); - if (wardenFileData.image) { - const imagePath = wardenFileData.image.slice(2); - wardenFileData.imageUrl = `https://raw.githubusercontent.com/${process.env.GITHUB_REPO_OWNER}/${process.env.REPO}/${process.env.BRANCH_NAME}/_data/handles/${imagePath}`; - } - if(!wardenFileData.members){ - allHandles.push({...wardenFileData}) - } + if (file.endsWith(".json")) { + const buffer = readFileSync(`./_data/handles/${file}`); + const wardenFileData = JSON.parse(buffer.toString()); + if (wardenFileData.image) { + const imagePath = wardenFileData.image.slice(2); + wardenFileData.imageUrl = `https://raw.githubusercontent.com/${process.env.GITHUB_REPO_OWNER}/${process.env.REPO}/${process.env.BRANCH_NAME}/_data/handles/${imagePath}`; + } + if (!wardenFileData.members) { + allHandles.push({ ...wardenFileData }); + } } -}); -return allHandles; + }); + return allHandles; } export async function getTeams() { // just return the objects? // with members - const teamHandles : {handle : string, link : string, moralisId: string, image : string}[] = []; + const teamHandles: { + handle: string; + link: string; + moralisId: string; + image: string; + }[] = []; const data = readdirSync(`./_data/handles`); // just return the objects? // without members data.forEach((file) => { - if(file.endsWith('.json')){ - const buffer = readFileSync(`./_data/handles/${file}`); - const wardenFileData = JSON.parse(buffer.toString()); - if (wardenFileData.image) { - const imagePath = wardenFileData.image.slice(2); - wardenFileData.imageUrl = `https://raw.githubusercontent.com/${process.env.GITHUB_REPO_OWNER}/${process.env.REPO}/${process.env.BRANCH_NAME}/_data/handles/${imagePath}`; - } - if(wardenFileData.members){ - teamHandles.push({...wardenFileData}) - } + if (file.endsWith(".json")) { + const buffer = readFileSync(`./_data/handles/${file}`); + const wardenFileData = JSON.parse(buffer.toString()); + if (wardenFileData.image) { + const imagePath = wardenFileData.image.slice(2); + wardenFileData.imageUrl = `https://raw.githubusercontent.com/${process.env.GITHUB_REPO_OWNER}/${process.env.REPO}/${process.env.BRANCH_NAME}/_data/handles/${imagePath}`; + } + if (wardenFileData.members) { + teamHandles.push({ ...wardenFileData }); + } } -}); + }); -return teamHandles; + return teamHandles; } export async function getUserTeams(username: string): Promise { diff --git a/src/pages/judge-application.js b/src/pages/judge-application.js index 071ac8364e..c21a7eb333 100644 --- a/src/pages/judge-application.js +++ b/src/pages/judge-application.js @@ -163,16 +163,12 @@ const JudgeApplication = () => { fieldState={state} />{" "} {status === FormStatus.Error && ( -
+

- An error occurred; please try again. If you continue - to receive this error, let us know in{" "} - Discord. - All fields are required. + An error occurred; please try again. If you continue to + receive this error, let us know in{" "} + Discord. All + fields are required.

)} @@ -199,26 +195,26 @@ const JudgeApplication = () => {
  • Judge applications are reviewed by the C4 judge selection committee, which includes top leaderboard wardens and past - judges. The committee will review your application and - give you a "yes" or "not yet". + judges. The committee will review your application and give you + a "yes" or "not yet".
  • - The review process begins after the application window - closes, and we expect it to take about a week, depending - on the number of applications the committee receives. + The review process begins after the application window closes, + and we expect it to take about a week, depending on the number + of applications the committee receives.
  • - You'll be contacted via DM to let you know if your - application has been successful this time around. + You'll be contacted via DM to let you know if your application + has been successful this time around.
  • - If you're accepted as a judge, an organizer will onboard - you and get you set up to judge your first contest! + If you're accepted as a judge, an organizer will onboard you and + get you set up to judge your first contest!
  • - In the meantime, if you have questions, feel free to reach - out to us in the C4 Discord, or have a closer look at the{" "} + In the meantime, if you have questions, feel free to reach out to + us in the C4 Discord, or have a closer look at the{" "} How to judge a contest {" "} diff --git a/src/pages/manage-team.tsx b/src/pages/manage-team.tsx index 8c3773bac3..3d7a339850 100644 --- a/src/pages/manage-team.tsx +++ b/src/pages/manage-team.tsx @@ -30,37 +30,37 @@ export default function TeamManagement({ location }) { // XXX: switching from gatsby graph const [handles, setHandles] = useState>(new Set()); - const [wardens, setWardens] = useState<{value: any; image: any; }[]>([]); + const [wardens, setWardens] = useState<{ value: any; image: any }[]>([]); // fetch wardens useEffect(() => { // // fetch get-user - (async () => { - const result = await fetch(`/.netlify/functions/handles`, { - headers: { - "Content-Type": "application/json", - // "X-Authorization": `Bearer ${sessionToken}`, - // "C4-User": currentUser.username, - }, + (async () => { + const result = await fetch(`/.netlify/functions/handles`, { + headers: { + "Content-Type": "application/json", + // "X-Authorization": `Bearer ${sessionToken}`, + // "C4-User": currentUser.username, + }, + }); + if (result.ok) { + let users = await result.json(); + let handles: Set = new Set(users.map((e) => e.handle)); + setHandles(handles); + + let wardens: { value: any; image: any }[] = users.map((e) => { + return { + value: e.handle, + image: e.image ?? "", + }; }); - if (result.ok) { - let users = await result.json(); - let handles : Set = new Set(users.map((e) => e.handle)); - setHandles(handles); - - let wardens : {value: any; image: any; }[] = users.map((e) => { - return { - value: e.handle, - image: e.image ?? "", - } - }); - setWardens(wardens); - } else { - // @TODO: what to do here? - throw "Unable to fetch user data."; - } - })(); - }, []); + setWardens(wardens); + } else { + // @TODO: what to do here? + throw "Unable to fetch user data."; + } + })(); + }, []); useEffect(() => { // @todo: show error message when user tries to manage a team they are not on diff --git a/src/pages/register-team.tsx b/src/pages/register-team.tsx index 1595dd60b5..df57d949e3 100644 --- a/src/pages/register-team.tsx +++ b/src/pages/register-team.tsx @@ -9,11 +9,11 @@ export default function TeamRegistration() { // XXX: switching from gatsby graph const [handles, setHandles] = useState>(new Set()); - const [wardens, setWardens] = useState<{value: any; image: any; }[]>([]); + const [wardens, setWardens] = useState<{ value: any; image: any }[]>([]); // fetch wardens useEffect(() => { - // // fetch handles + // // fetch handles (async () => { const result = await fetch(`/.netlify/functions/handles`, { headers: { @@ -24,14 +24,14 @@ export default function TeamRegistration() { }); if (result.ok) { let users = await result.json(); - let handles : Set = new Set(users.map((e) => e.handle)); + let handles: Set = new Set(users.map((e) => e.handle)); setHandles(handles); - let wardens : {value: any; image: any; }[] = users.map((e) => { + let wardens: { value: any; image: any }[] = users.map((e) => { return { value: e.handle, image: e.image ?? "", - } + }; }); setWardens(wardens); } else { diff --git a/src/pages/register.tsx b/src/pages/register.tsx index b7b7829520..d919d66b84 100644 --- a/src/pages/register.tsx +++ b/src/pages/register.tsx @@ -13,7 +13,7 @@ import * as styles from "../components/form/Form.module.scss"; export default function UserRegistration() { const [handles, setHandles] = useState>(new Set()); - const [wardens, setWardens] = useState<{value: any; image: any; }[]> ([]); + const [wardens, setWardens] = useState<{ value: any; image: any }[]>([]); const [handleData, setHandleData] = useState([]); const { isInitialized } = useMoralis(); const { currentUser } = useUser(); @@ -24,8 +24,7 @@ export default function UserRegistration() { } }, [currentUser.isLoggedIn]); - -//do we need this function?? It setting wardens with a filtered set of wardens but then wardens is never used?? What am I missing?? + //do we need this function?? It setting wardens with a filtered set of wardens but then wardens is never used?? What am I missing?? useEffect((): void => { async function filterWardens(): Promise { if (!isInitialized) { @@ -44,23 +43,24 @@ export default function UserRegistration() { } return true; }) - .map(( warden ) => ({ value: warden.handle, image: warden.image ?? "" })); + .map((warden) => ({ value: warden.handle, image: warden.image ?? "" })); setWardens(wardens); } filterWardens(); }, [isInitialized, handleData]); - // this is for getting handles from netlify function. + // this is for getting handles from netlify function. useEffect(() => { (async () => { const result = await fetch(`/.netlify/functions/handles`, { headers: { "Content-Type": "application/json", }, - }).then((res) => res.json()) - .then((res) => { + }) + .then((res) => res.json()) + .then((res) => { return res; - }); + }); if (result) { setHandles(new Set(result.map((h) => h.handle))); setHandleData(result); @@ -70,8 +70,6 @@ export default function UserRegistration() { })(); }, [isInitialized]); - - return ( ); } - From afb4a6d437cdbfdb45a39dceeb5ad0130618b1bf Mon Sep 17 00:00:00 2001 From: simon busch Date: Tue, 24 Jan 2023 14:26:44 +0100 Subject: [PATCH 16/16] [UPD] refactor getHandles --- netlify/functions/handles.ts | 35 +++++++++++++++++++++-------------- 1 file changed, 21 insertions(+), 14 deletions(-) diff --git a/netlify/functions/handles.ts b/netlify/functions/handles.ts index b3fdc67a90..5756f6b138 100644 --- a/netlify/functions/handles.ts +++ b/netlify/functions/handles.ts @@ -1,28 +1,35 @@ import fs, { readFileSync, readdirSync } from "fs"; -//need to set up the members portion of the handles data (i think the members field is for teams) -//so my plan is to add a call to get the teams data here +const readAndParseJSONFile = (filePath: string) => { + const buffer = readFileSync(filePath); + return JSON.parse(buffer.toString()); +}; + +const getImageUrl = (fileData: any) => { + if (fileData.image) { + const imagePath = fileData.image.slice(2); + return `https://raw.githubusercontent.com/${process.env.GITHUB_REPO_OWNER}/${process.env.REPO}/${process.env.BRANCH_NAME}/_data/handles/${imagePath}`; + } + return null; +}; + const getHandles = () => { const allHandles: { handle: string; link: string; moralisId: string; - image: string; + imageUrl: string; members: string[]; }[] = []; - const data = readdirSync(`./_data/handles`); - data.forEach((file) => { + + const files = readdirSync(`./_data/handles`); + for (const file of files) { if (file.endsWith(".json")) { - const buffer = readFileSync(`./_data/handles/${file}`); - const wardenFileData = JSON.parse(buffer.toString()); - if (wardenFileData.image) { - const imagePath = wardenFileData.image.slice(2); - wardenFileData.imageUrl = `https://raw.githubusercontent.com/${process.env.GITHUB_REPO_OWNER}/${process.env.REPO}/${process.env.BRANCH_NAME}/_data/handles/${imagePath}`; - } - allHandles.push({ ...wardenFileData }); + const fileData = readAndParseJSONFile(`./_data/handles/${file}`); + const imageUrl = getImageUrl(fileData); + allHandles.push({ ...fileData, imageUrl }); } - }); - + } return allHandles; };