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

Smol fixes #171

Merged
merged 5 commits into from
Mar 24, 2022
Merged
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
3 changes: 3 additions & 0 deletions packages/react-app/src/routes/party/Party.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,8 @@ export default function Party({
return (
<ViewTable
partyData={partyData}
setPartyData={setPartyData}
userSigner={userSigner}
mainnetProvider={mainnetProvider}
votesData={accountVoteData}
distribution={dist}
Expand All @@ -152,6 +154,7 @@ export default function Party({
return (
<VoteTable
partyData={partyData}
setPartyData={setPartyData}
address={address}
userSigner={userSigner}
targetNetwork={targetNetwork}
Expand Down
70 changes: 51 additions & 19 deletions packages/react-app/src/routes/party/components/ViewTable.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,17 @@ import {
} from "@chakra-ui/react";
import { EditIcon, CheckIcon } from "@chakra-ui/icons";
import React, { useState, useMemo, useRef, useEffect } from "react";
import { useHistory } from "react-router-dom";
import { useParams, useHistory } from "react-router-dom";
import AddressChakra from "../../../components/AddressChakra";
import Confetti from "react-confetti";
import { useLocation } from "react-router-dom";
import { useScreenDimensions } from "../../../hooks/useScreenDimensions";
import {ethers} from "ethers";

export const ViewTable = ({
partyData,
setPartyData,
userSigner,
mainnetProvider,
votesData,
distribution,
Expand All @@ -34,13 +37,16 @@ export const ViewTable = ({
const [castVotes, setCastVotes] = useState(false);
const [isLoading, setIsLoading] = useState(true);
const [candidateNote, setCandidateNote] = useState();
const [noteChars, setNoteChars] = useState(0);
const [noteIsLoading, setNoteIsLoading] = useState(false);
const { isOpen, onOpen, onClose } = useDisclosure();
const initialRef = React.useRef();
const finalRef = React.useRef();

const [showConfetti, setShowConfetti] = useState(false);
const location = useLocation();
const { width, height } = useScreenDimensions();
let { id } = useParams();

useEffect(() => {
const queryParams = new URLSearchParams(location.search);
Expand Down Expand Up @@ -76,11 +82,22 @@ export const ViewTable = ({
/>
</Td>
<Td>
<Text>
{partyData.notes?.filter(n => n.candidate.toLowerCase() === d.toLowerCase()).reverse()[0]?.message}
</Text>
<Box width="12em">
<Text>
{partyData.notes?.filter(n => n.candidate.toLowerCase() === d.toLowerCase()).reverse()[0]?.message}
</Text>
</Box>
{d.toLowerCase() === address.toLowerCase() ? (
<Button size="xs" rightIcon={<EditIcon />} variant="link" ml="1" onClick={onOpen}>
<Button
size="xs"
rightIcon={<EditIcon />}
variant="link"
ml="1"
onClick={_ => {
setNoteChars(0);
onOpen();
}}
>
Edit
</Button>
) : null}
Expand All @@ -106,18 +123,29 @@ export const ViewTable = ({
}, [partyData, votesData, distribution, strategy, amountToDistribute]);

const newCandidateNote = async _ => {
const note = {
candidate: address,
message: candidateNote,
signature: "",
};

const res = await fetch(`${process.env.REACT_APP_API_URL}/party/${partyData.id}/note`, {
method: "put",
headers: { "Content-Type": "application/json" },
body: JSON.stringify(note),
});
onClose();
try {
setNoteIsLoading(true);
const sig = await userSigner.signMessage(ethers.utils.keccak256(ethers.utils.toUtf8Bytes(candidateNote)));
const note = {
candidate: address,
message: candidateNote,
signature: sig,
};
const noteRes = await fetch(`${process.env.REACT_APP_API_URL}/party/${partyData.id}/note`, {
method: "put",
headers: { "Content-Type": "application/json" },
body: JSON.stringify(note),
});
// TODO: Find a more efficient approach instead of re-requesting the whole party
const partyRes = await fetch(`${process.env.REACT_APP_API_URL}/party/${id}`);
const data = await partyRes.json();
setPartyData(data);
setNoteIsLoading(false);
onClose();
} catch(err) {
console.log("error submitting note: ", err);
setNoteIsLoading(false);
}
};

const editNoteModal = (
Expand All @@ -129,14 +157,18 @@ export const ViewTable = ({
<ModalBody pb={6}>
<FormControl>
<Input
onChange={e => setCandidateNote(e.target.value)}
onChange={e => {
setCandidateNote(e.target.value);
setNoteChars(e.target.value.length);
}}
ref={initialRef}
placeholder="Enter your note here"
/>
</FormControl>
<Text>{noteChars}/124</Text>
</ModalBody>
<ModalFooter>
<Button mr={3} onClick={newCandidateNote}>
<Button mr={3} onClick={newCandidateNote} isLoading={noteIsLoading} isDisabled={noteChars > 124}>
Submit
</Button>
<Button onClick={onClose}>Cancel</Button>
Expand Down
50 changes: 43 additions & 7 deletions packages/react-app/src/routes/party/components/VoteTable.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -34,15 +34,26 @@ import {
} from "@chakra-ui/react";
import { EditIcon, CheckIcon } from "@chakra-ui/icons";
import React, { useState, useMemo, useEffect, useRef } from "react";
import { useHistory } from "react-router-dom";
import { useParams, useHistory } from "react-router-dom";
import AddressChakra from "../../../components/AddressChakra";
import { ethers } from "ethers";

export const VoteTable = ({ partyData, address, userSigner, targetNetwork, readContracts, mainnetProvider }) => {
export const VoteTable = ({
partyData,
setPartyData,
address,
userSigner,
targetNetwork,
readContracts,
mainnetProvider,
}) => {
// Init votes data to 0 votes for each candidate
const [votesData, setVotesData] = useState(null);
// Init votes left to nvotes
const [votesLeft, setVotesLeft] = useState(null);
const [candidateNote, setCandidateNote] = useState("");
const [noteChars, setNoteChars] = useState(0);
const [noteIsLoading, setNoteIsLoading] = useState(false);
const [invalidVotesLeft, setInvalidVotesLeft] = useState(false);
const [blockNumber, setBlockNumber] = useState("-1");
const [isCorrectChainId, setIsCorrectChainId] = useState(true);
Expand All @@ -53,6 +64,7 @@ export const VoteTable = ({ partyData, address, userSigner, targetNetwork, readC
setBlockNumber(bn.toString());
});
const history = useHistory();
let { id } = useParams();

useEffect(
_ => {
Expand All @@ -75,18 +87,29 @@ export const VoteTable = ({ partyData, address, userSigner, targetNetwork, readC
};

const newCandidateNote = async _ => {
try {
setNoteIsLoading(true);
const sig = await userSigner.signMessage(ethers.utils.keccak256(ethers.utils.toUtf8Bytes(candidateNote)));
const note = {
candidate: address,
message: candidateNote,
signature: "",
signature: sig,
};

const res = await fetch(`${process.env.REACT_APP_API_URL}/party/${partyData.id}/note`, {
const noteRes = await fetch(`${process.env.REACT_APP_API_URL}/party/${partyData.id}/note`, {
method: "put",
headers: { "Content-Type": "application/json" },
body: JSON.stringify(note),
});
// TODO: Find a more efficient approach instead of re-requesting the whole party
const partyRes = await fetch(`${process.env.REACT_APP_API_URL}/party/${id}`);
const data = await partyRes.json();
setPartyData(data);
setNoteIsLoading(false);
onClose();
} catch {
console.log("error submitting note")
setNoteIsLoading(false);
}
};

const vote = async _ => {
Expand Down Expand Up @@ -169,11 +192,22 @@ export const VoteTable = ({ partyData, address, userSigner, targetNetwork, readC
/>
</Td>
<Td>
<Box width="24em">
<Text>
{partyData.notes?.filter(n => n.candidate.toLowerCase() === d.toLowerCase()).reverse()[0]?.message}
</Text>
</Box>
{d.toLowerCase() === address.toLowerCase() ? (
<Button size="xs" rightIcon={<EditIcon />} variant="link" ml="1" onClick={onOpen}>
<Button
size="xs"
rightIcon={<EditIcon />}
variant="link"
ml="1"
onClick={_ => {
setNoteChars(0);
onOpen();
}}
>
Edit
</Button>
) : null}
Expand Down Expand Up @@ -221,14 +255,16 @@ export const VoteTable = ({ partyData, address, userSigner, targetNetwork, readC
<Input
onChange={e => {
setCandidateNote(e.target.value);
setNoteChars(e.target.value.length);
}}
ref={initialRef}
placeholder="Enter your note here"
/>
<Text>{noteChars}/124</Text>
</FormControl>
</ModalBody>
<ModalFooter>
<Button mr={3} onClick={newCandidateNote}>
<Button mr={3} onClick={newCandidateNote} isLoading={noteIsLoading} isDisabled={noteChars > 124}>
Submit
</Button>
<Button onClick={onClose}>Cancel</Button>
Expand Down