Skip to content

Commit

Permalink
Refactored code to use useState and useEffect
Browse files Browse the repository at this point in the history
  • Loading branch information
danitome24 committed Sep 18, 2024
1 parent 54a44f8 commit 2f02f21
Showing 1 changed file with 11 additions and 8 deletions.
19 changes: 11 additions & 8 deletions packages/nextjs/app/builders/page.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
"use client";

import { useEffect, useState } from "react";
import { BuilderPicture, MentorPicture } from "./_components/index";
import type { NextPage } from "next";
import { useScaffoldEventHistory } from "~~/hooks/scaffold-eth";
Expand Down Expand Up @@ -50,7 +51,10 @@ const mentors: Mentor[] = [
];

const Builders: NextPage = () => {
const { data: events } = useScaffoldEventHistory({
const [buildersCheckedIn, setBuildersCheckedIn] = useState(0);
const [builders, setBuilders] = useState<Builder[]>([]);

const { data: events, isLoading: isLoadingEvents } = useScaffoldEventHistory({
contractName: "BatchRegistry",
eventName: "CheckedIn",
fromBlock: 31231n,
Expand All @@ -61,13 +65,12 @@ const Builders: NextPage = () => {
receiptData: true,
});

let buildersCheckedIn = 0;
let builders: Builder[] = [];

if (events != undefined) {
buildersCheckedIn = events.length;
builders = events.map(e => getBuilderFromEvent(e));
}
useEffect(() => {
if (!isLoadingEvents && events != undefined) {
setBuildersCheckedIn(events.length);
setBuilders(events.map(e => getBuilderFromEvent(e)));
}
}, [isLoadingEvents, events]);

return (
<>
Expand Down

0 comments on commit 2f02f21

Please sign in to comment.