Skip to content

Commit

Permalink
Made code cleaner, minor bugs remain
Browse files Browse the repository at this point in the history
  • Loading branch information
luloxi committed Jul 4, 2023
1 parent 0f7236e commit 27b6eca
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 81 deletions.
86 changes: 28 additions & 58 deletions packages/nextjs/pages/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ const Home: NextPage = () => {
challengeId: event.args[0].toString(),
proposingTeam: event.args[1],
newReferee: event.args[2],
}));
})) as UpdateRefereeRequestProps[];
setUpdateRefereeRequestHistory(mappedHistory);
}
}, [UpdateRefereeRequestHistory]);
Expand All @@ -145,7 +145,7 @@ const Home: NextPage = () => {
challengeId: event.args[0].toString(),
newReferee: event.args[1],
updateAccepted: event.args[2],
}));
})) as UpdateRefereeResponseProps[];
setUpdateRefereeResponseHistory(mappedHistory);
}
}, [UpdateRefereeResponseHistory]);
Expand All @@ -155,21 +155,15 @@ const Home: NextPage = () => {
contractName: "Sportsbook",
eventName: "ChallengeCreated",
listener: (challengeId, team1, team2, referee, bet) => {
setChallengeHistory(previousHistory => {
const newChallengeId = parseInt(challengeId.toString());
if (previousHistory.some(challenge => challenge.challengeId === newChallengeId)) {
return previousHistory;
}

const newChallenge: ChallengeCreatedProps = {
challengeId: newChallengeId,
setChallengeHistory(indexedHistory => {
const newChallengeCreated: ChallengeCreatedProps = {
challengeId: parseInt(challengeId.toString()),
team1,
team2,
referee,
bet: parseInt(bet.toString()),
};

return [newChallenge, ...previousHistory];
return [newChallengeCreated, ...indexedHistory];
});
},
});
Expand All @@ -178,19 +172,13 @@ const Home: NextPage = () => {
contractName: "Sportsbook",
eventName: "ChallengeAccepted",
listener: (challengeId, team1, team2) => {
setChallengeAcceptedHistory(previousHistory => {
const newChallengeId = parseInt(challengeId.toString());
if (previousHistory.some(challenge => challenge.challengeId === newChallengeId)) {
return previousHistory;
}

const newChallenge: ChallengeAcceptedProps = {
challengeId: newChallengeId,
setChallengeAcceptedHistory(indexedHistory => {
const newChallengeAccepted: ChallengeAcceptedProps = {
challengeId: parseInt(challengeId.toString()),
team1,
team2,
};

return [newChallenge, ...previousHistory];
return [newChallengeAccepted, ...indexedHistory];
});
},
});
Expand All @@ -199,20 +187,14 @@ const Home: NextPage = () => {
contractName: "Sportsbook",
eventName: "ChallengeStarted",
listener: (challengeId, referee, team1, team2) => {
setChallengeStartedHistory(previousHistory => {
const newChallengeId = parseInt(challengeId.toString());
if (previousHistory.some(challenge => challenge.challengeId === newChallengeId)) {
return previousHistory;
}

const newChallenge: ChallengeStartedProps = {
challengeId: newChallengeId,
setChallengeStartedHistory(indexedHistory => {
const newChallengeStarted: ChallengeStartedProps = {
challengeId: parseInt(challengeId.toString()),
team1,
team2,
referee,
};

return [newChallenge, ...previousHistory];
return [newChallengeStarted, ...indexedHistory];
});
},
});
Expand All @@ -221,21 +203,15 @@ const Home: NextPage = () => {
contractName: "Sportsbook",
eventName: "ChallengeResult",
listener: (challengeId, team1, team2, team1Result, team2Result) => {
setChallengeResultHistory(previousHistory => {
const newChallengeId = parseInt(challengeId.toString());
if (previousHistory.some(challenge => challenge.challengeId === newChallengeId)) {
return previousHistory;
}

const newChallenge: ChallengeResultProps = {
challengeId: newChallengeId,
setChallengeResultHistory(indexedHistory => {
const newChallengeResult: ChallengeResultProps = {
challengeId: parseInt(challengeId.toString()),
team1,
team2,
team1Result,
team2Result,
};

return [newChallenge, ...previousHistory];
return [newChallengeResult, ...indexedHistory];
});
},
});
Expand All @@ -244,18 +220,12 @@ const Home: NextPage = () => {
contractName: "Sportsbook",
eventName: "ChallengeCanceled",
listener: (challengeId, canceledBy) => {
setChallengeCanceledHistory(previousHistory => {
const newChallengeId = parseInt(challengeId.toString());
if (previousHistory.some(challenge => challenge.challengeId === newChallengeId)) {
return previousHistory;
}

const newChallenge: ChallengeCanceledProps = {
challengeId: newChallengeId,
setChallengeCanceledHistory(indexedHistory => {
const newChallengeCanceled: ChallengeCanceledProps = {
challengeId: parseInt(challengeId.toString()),
canceledBy: canceledBy,
};

return [newChallenge, ...previousHistory];
return [newChallengeCanceled, ...indexedHistory];
});
},
});
Expand All @@ -264,13 +234,13 @@ const Home: NextPage = () => {
contractName: "Sportsbook",
eventName: "UpdateRefereeRequest",
listener: (challengeId, proposingTeam, newReferee) => {
setUpdateRefereeRequestHistory(previousHistory => {
const newChallenge: UpdateRefereeRequestProps = {
setUpdateRefereeRequestHistory(indexedHistory => {
const newUpdateRefereeRequest: UpdateRefereeRequestProps = {
challengeId: parseInt(challengeId.toString()),
proposingTeam,
newReferee,
};
return [newChallenge, ...previousHistory];
return [newUpdateRefereeRequest, ...indexedHistory];
});
},
});
Expand All @@ -279,13 +249,13 @@ const Home: NextPage = () => {
contractName: "Sportsbook",
eventName: "UpdateRefereeResponse",
listener: (challengeId, newReferee, updateAccepted) => {
setUpdateRefereeResponseHistory(previousHistory => {
const newChallenge: UpdateRefereeResponseProps = {
setUpdateRefereeResponseHistory(indexedHistory => {
const newUpdateRefereeResponse: UpdateRefereeResponseProps = {
challengeId: parseInt(challengeId.toString()),
newReferee,
updateAccepted,
};
return [newChallenge, ...previousHistory];
return [newUpdateRefereeResponse, ...indexedHistory];
});
},
});
Expand Down
51 changes: 28 additions & 23 deletions packages/nextjs/pages/sportsbook/ChallengeCard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,39 +17,38 @@ const ChallengeCard = ({
updateRefereeRequest,
updateRefereeAccepted,
}: ChallengeCardProps) => {
const [refereeAddress, setRefereeAddress] = useState(challenge.referee);
const [updateRefereeAddress, setUpdateRefereeAddress] = useState<string>("");
const [updateRefereeResponse, setUpdateRefereeResponse] = useState<boolean | undefined>(undefined);
const [completeChallengeTeam1Score, setCompleteChallengeTeam1Score] = useState<string>("");
const [completeChallengeTeam2Score, setCompleteChallengeTeam2Score] = useState<string>("");
const [refereeAddress, setRefereeAddress] = useState(challenge.referee);

const [challengeIdArg, setChallengeIdArg] = useState<string>(challenge.challengeId.toString());

const { address } = useAccount();

const { writeAsync: acceptChallenge } = useScaffoldContractWrite({
contractName: "Sportsbook",
functionName: "acceptChallenge",
args: [challengeIdArg ? BigNumber.from(challengeIdArg) : BigNumber.from(0)],
args: [challenge.challengeId.toString() ? BigNumber.from(challenge.challengeId.toString()) : BigNumber.from(0)],
value: challenge.bet ? ethers.utils.formatEther(challenge.bet.toString()) : undefined,
});

const { writeAsync: deleteChallenge } = useScaffoldContractWrite({
contractName: "Sportsbook",
functionName: "deleteChallenge",
args: [challengeIdArg ? BigNumber.from(challengeIdArg) : BigNumber.from(0)],
args: [challenge.challengeId.toString() ? BigNumber.from(challenge.challengeId.toString()) : BigNumber.from(0)],
});

const { writeAsync: startChallenge } = useScaffoldContractWrite({
contractName: "Sportsbook",
functionName: "startChallenge",
args: [challengeIdArg ? BigNumber.from(challengeIdArg) : undefined],
args: [challenge.challengeId.toString() ? BigNumber.from(challenge.challengeId.toString()) : undefined],
});

const { writeAsync: completeChallenge } = useScaffoldContractWrite({
contractName: "Sportsbook",
functionName: "completeChallenge",
args: [
challengeIdArg ? BigNumber.from(challengeIdArg) : undefined,
challenge.challengeId.toString() ? BigNumber.from(challenge.challengeId.toString()) : undefined,
completeChallengeTeam1Score ? parseInt(completeChallengeTeam1Score) : undefined,
completeChallengeTeam2Score ? parseInt(completeChallengeTeam2Score) : undefined,
],
Expand All @@ -58,19 +57,19 @@ const ChallengeCard = ({
const { writeAsync: updateReferee } = useScaffoldContractWrite({
contractName: "Sportsbook",
functionName: "updateReferee",
args: [challengeIdArg ? BigNumber.from(challengeIdArg) : undefined, updateRefereeAddress],
});

const { writeAsync: answerNoToUpdateReferee } = useScaffoldContractWrite({
contractName: "Sportsbook",
functionName: "answerUpdateReferee",
args: [challengeIdArg ? BigNumber.from(challengeIdArg) : undefined, false],
args: [
challenge.challengeId.toString() ? BigNumber.from(challenge.challengeId.toString()) : undefined,
updateRefereeAddress,
],
});

const { writeAsync: answerYesToUpdateReferee } = useScaffoldContractWrite({
const { writeAsync: answerToUpdateReferee } = useScaffoldContractWrite({
contractName: "Sportsbook",
functionName: "answerUpdateReferee",
args: [challengeIdArg ? BigNumber.from(challengeIdArg) : undefined, true],
args: [
challenge.challengeId.toString() ? BigNumber.from(challenge.challengeId.toString()) : undefined,
updateRefereeResponse,
],
});

useEffect(() => {
Expand All @@ -79,11 +78,7 @@ const ChallengeCard = ({
} else {
setRefereeAddress(challenge.referee);
}
}, [updateRefereeAccepted, challenge.referee]);

if (challenge && challenge.challengeId && challengeIdArg != challenge.challengeId.toString()) {
setChallengeIdArg(challenge.challengeId.toString());
}
}, [updateRefereeAccepted]);

return (
<div key={challenge.challengeId}>
Expand Down Expand Up @@ -294,13 +289,23 @@ const ChallengeCard = ({
{updateRefereeRequest?.proposingTeam != address && (
<Flex justifyContent={"space-around"}>
<Button
onClick={answerYesToUpdateReferee}
onClick={() => {
setUpdateRefereeResponse(true);
answerToUpdateReferee();
}}
backgroundColor={"green.500"}
textColor={"white"}
>
Accept new referee
</Button>
<Button onClick={answerNoToUpdateReferee} backgroundColor={"red.500"} textColor={"white"}>
<Button
onClick={() => {
setUpdateRefereeResponse(false);
answerToUpdateReferee();
}}
backgroundColor={"red.500"}
textColor={"white"}
>
Decline proposal
</Button>
</Flex>
Expand Down

0 comments on commit 27b6eca

Please sign in to comment.