Skip to content

Commit

Permalink
Merge remote-tracking branch 'clearcontracts/main' into test
Browse files Browse the repository at this point in the history
  • Loading branch information
mesudip committed Nov 26, 2024
2 parents 62fe1ae + e2d4f67 commit 31095c4
Show file tree
Hide file tree
Showing 20 changed files with 142 additions and 63 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,16 @@ test.skip('alerts user when poll has finished voting', async () => {
id: '1',
stakeAddress: 'stakeAddress',
walletName: 'walletName',
isCoordinator: false,
isDelegate: true,
isAlternate: false,
},
}}
>
<Toaster />
<VoteOnPollButtons
pollName="Test Poll"
pollId="1"
isActiveVoter={true}
hashedText="1dda86a28da3715e618a1605f81c7a3121ce767cdc9bd0d46beec0bef40e8125"
link="https://www.intersectmbo.org/constitution/constitution.html"
/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,16 @@ test.skip('alerts user when poll ID is not provided', async () => {
id: '1',
stakeAddress: 'stakeAddress',
walletName: 'walletName',
isCoordinator: false,
isDelegate: true,
isAlternate: false,
},
}}
>
<Toaster />
<VoteOnPollButtons
pollName="Test Poll"
pollId="1"
isActiveVoter={true}
hashedText="1dda86a28da3715e618a1605f81c7a3121ce767cdc9bd0d46beec0bef40e8125"
link="https://www.intersectmbo.org/constitution/constitution.html"
/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,16 @@ test.skip('alerts user when poll is archived', async () => {
id: '1',
stakeAddress: 'stakeAddress',
walletName: 'walletName',
isCoordinator: false,
isDelegate: true,
isAlternate: false,
},
}}
>
<Toaster />
<VoteOnPollButtons
pollName="Test Poll"
pollId="1"
isActiveVoter={true}
hashedText="1dda86a28da3715e618a1605f81c7a3121ce767cdc9bd0d46beec0bef40e8125"
link="https://www.intersectmbo.org/constitution/constitution.html"
/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,16 @@ test.skip('alerts user when poll is not found', async () => {
id: '1',
stakeAddress: 'stakeAddress',
walletName: 'walletName',
isCoordinator: false,
isDelegate: true,
isAlternate: false,
},
}}
>
<Toaster />
<VoteOnPollButtons
pollName="Test Poll"
pollId="1"
isActiveVoter={true}
hashedText="1dda86a28da3715e618a1605f81c7a3121ce767cdc9bd0d46beec0bef40e8125"
link="https://www.intersectmbo.org/constitution/constitution.html"
/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,16 @@ test.skip('alerts user when poll is not open for voting yet', async () => {
id: '1',
stakeAddress: 'stakeAddress',
walletName: 'walletName',
isCoordinator: false,
isDelegate: true,
isAlternate: false,
},
}}
>
<Toaster />
<VoteOnPollButtons
pollName="Test Poll"
pollId="1"
isActiveVoter={true}
hashedText="1dda86a28da3715e618a1605f81c7a3121ce767cdc9bd0d46beec0bef40e8125"
link="https://www.intersectmbo.org/constitution/constitution.html"
/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,16 @@ test.skip('alerts user when poll has finished voting', async () => {
id: '1',
stakeAddress: 'stakeAddress',
walletName: 'walletName',
isCoordinator: false,
isDelegate: true,
isAlternate: false,
},
}}
>
<Toaster />
<VoteOnPollButtons
pollName="Test Poll"
pollId="1"
isActiveVoter={true}
hashedText="1dda86a28da3715e618a1605f81c7a3121ce767cdc9bd0d46beec0bef40e8125"
link="https://www.intersectmbo.org/constitution/constitution.html"
/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,16 @@ test.skip('alerts user when vote is invalid', async () => {
id: '1',
stakeAddress: 'stakeAddress',
walletName: 'walletName',
isCoordinator: false,
isDelegate: true,
isAlternate: false,
},
}}
>
<Toaster />
<VoteOnPollButtons
pollName="Test Poll"
pollId="1"
isActiveVoter={true}
hashedText="1dda86a28da3715e618a1605f81c7a3121ce767cdc9bd0d46beec0bef40e8125"
link="https://www.intersectmbo.org/constitution/constitution.html"
/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,16 @@ test.skip('alerts user when vote is not provided', async () => {
id: '1',
stakeAddress: 'stakeAddress',
walletName: 'walletName',
isCoordinator: false,
isDelegate: true,
isAlternate: false,
},
}}
>
<Toaster />
<VoteOnPollButtons
pollName="Test Poll"
pollId="1"
isActiveVoter={true}
hashedText="1dda86a28da3715e618a1605f81c7a3121ce767cdc9bd0d46beec0bef40e8125"
link="https://www.intersectmbo.org/constitution/constitution.html"
/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,16 @@ test.skip('successfully votes abstain on a poll', async () => {
id: '1',
stakeAddress: 'stakeAddress',
walletName: 'walletName',
isCoordinator: false,
isDelegate: true,
isAlternate: false,
},
}}
>
<Toaster />
<VoteOnPollButtons
pollName="Test Poll"
pollId="1"
isActiveVoter={true}
hashedText="1dda86a28da3715e618a1605f81c7a3121ce767cdc9bd0d46beec0bef40e8125"
link="https://www.intersectmbo.org/constitution/constitution.html"
/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,16 @@ test.skip('successfully votes no on a poll', async () => {
id: '1',
stakeAddress: 'stakeAddress',
walletName: 'walletName',
isCoordinator: false,
isDelegate: true,
isAlternate: false,
},
}}
>
<Toaster />
<VoteOnPollButtons
pollName="Test Poll"
pollId="1"
isActiveVoter={true}
hashedText="1dda86a28da3715e618a1605f81c7a3121ce767cdc9bd0d46beec0bef40e8125"
link="https://www.intersectmbo.org/constitution/constitution.html"
/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,16 @@ test.skip('successfully votes yes on a poll', async () => {
id: '1',
stakeAddress: 'stakeAddress',
walletName: 'walletName',
isCoordinator: false,
isDelegate: true,
isAlternate: false,
},
}}
>
<Toaster />
<VoteOnPollButtons
pollName="Test Poll"
pollId="1"
isActiveVoter={true}
hashedText="1dda86a28da3715e618a1605f81c7a3121ce767cdc9bd0d46beec0bef40e8125"
link="https://www.intersectmbo.org/constitution/constitution.html"
/>
Expand Down
28 changes: 22 additions & 6 deletions src/components/buttons/voteOnPollButtons.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@ import { useSession } from 'next-auth/react';
import toast from 'react-hot-toast';

import { castVote } from '@/lib/helpers/castVote';
import { getActiveVoterFromUserId } from '@/lib/helpers/getActiveVoterFromUserId';
import { getPollVote } from '@/lib/helpers/getPollVote';

interface Props {
pollName: string;
pollId: string;
isActiveVoter: boolean;
hashedText: string;
link: string;
}
Expand All @@ -23,17 +23,33 @@ interface Props {
* Yes, No, Abstain buttons to vote on a poll
* @param pollName - The name of the poll
* @param pollId - The ID of the poll
* @param isActiveVoter - Whether the user is the active voter
* @param hashedText - The hashed text of the poll
* @param link - The link to the poll
* @returns Vote on Poll Buttons
*/
export function VoteOnPollButtons(props: Props): JSX.Element {
const { pollName, pollId, isActiveVoter, hashedText, link } = props;
const [vote, setVote] = useState('');
const [disabled, setDisabled] = useState(false);
const { pollName, pollId, hashedText, link } = props;

const session = useSession();
const theme = useTheme();

const [vote, setVote] = useState('');
const [disabled, setDisabled] = useState(false);
const [isActiveVoter, setIsActiveVoter] = useState(false);
const [fetching, setIsFetching] = useState(
session.status === 'authenticated' ? false : true,
);

useEffect(() => {
if (session.data?.user.id) {
setIsFetching(true);
getActiveVoterFromUserId(session.data.user.id).then((result) => {
setIsActiveVoter(result.activeVoterId === session.data.user.id);
setIsFetching(false);
});
}
}, [session.data?.user.id]);

async function handleVote(vote: string): Promise<void> {
setDisabled(true);
const result = await castVote(
Expand Down Expand Up @@ -92,7 +108,7 @@ export function VoteOnPollButtons(props: Props): JSX.Element {
</Typography>
</Box>
)}
{!isActiveVoter && !session.data?.user.isCoordinator && (
{!isActiveVoter && !session.data?.user.isCoordinator && !fetching && (
<Typography variant="h6" fontWeight="bold">
You are not the active voter for your workshop. Only the active
voter can vote.
Expand Down
22 changes: 19 additions & 3 deletions src/components/coordinator/manageActivePowerTable.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,19 @@ import { getRepresentatives } from '@/lib/helpers/getRepresentatives';
import { getWorkshops } from '@/lib/helpers/getWorkshops';
import { updateActiveVoter } from '@/lib/helpers/updateActiveVoter';

interface Props {
refresh: boolean;
toggleRefresh: () => void;
}

/**
* Allows a workshop coordinator to manage if delegates or alternates have active power from each workhsop
* @param refresh - boolean to refresh the table
* @param toggleRefresh - function to toggle the refresh boolean
* @returns Admin Manage Active Power Table
*/
export function ManageActivePowerTable(): JSX.Element {
export function ManageActivePowerTable(props: Props): JSX.Element {
const { refresh, toggleRefresh } = props;
const [representatives, setRepresentatives] = useState<User[]>([]);
const [workshops, setWorkshops] = useState<Workshop[]>([]);
const [rowModesModel, setRowModesModel] = useState<GridRowModesModel>({});
Expand All @@ -47,7 +55,7 @@ export function ManageActivePowerTable(): JSX.Element {
setRepresentatives(reps);
}
fetchData();
}, [reload]);
}, [reload, refresh]);

function handleRowEditStop(
// eslint-disable-next-line @typescript-eslint/no-explicit-any
Expand All @@ -69,6 +77,7 @@ export function ManageActivePowerTable(): JSX.Element {
toast.error(data.message);
}
setReload(!reload);
toggleRefresh();
return newRow;
}

Expand Down Expand Up @@ -273,7 +282,14 @@ export function ManageActivePowerTable(): JSX.Element {
},
},
];
}, [representatives, rowModesModel, theme.palette.text.primary, reload]);
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [
representatives,
rowModesModel,
theme.palette.text.primary,
reload,
refresh, // TS is not recognizing refresh as a dependency but it is required to properly refresh the table when info in representatives table changes
]);

return (
<Box display="flex" flexDirection="column" gap={1}>
Expand Down
9 changes: 8 additions & 1 deletion src/components/coordinator/manageRepresentativesTable.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,17 @@ import { User } from '@/types';
import { getRepresentatives } from '@/lib/helpers/getRepresentatives';
import { updateUser } from '@/lib/helpers/updateUser';

interface Props {
toggleRefresh: () => void;
}

/**
* Table for admin to manage representative information
* @param toggleRefresh - function to toggle the refresh boolean
* @returns Admin Manage Representatives Table
*/
export function ManageRepresentativesTable(): JSX.Element {
export function ManageRepresentativesTable(props: Props): JSX.Element {
const { toggleRefresh } = props;
const [representatives, setRepresentatives] = useState<User[]>([]);
const [rowModesModel, setRowModesModel] = useState<GridRowModesModel>({});
const [reload, setReload] = useState(false);
Expand Down Expand Up @@ -64,6 +70,7 @@ export function ManageRepresentativesTable(): JSX.Element {
toast.error(data.message);
}
setReload(!reload);
toggleRefresh();
return newRow;
}

Expand Down
10 changes: 9 additions & 1 deletion src/components/representatives/votingHistoryTable.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { pollPhases } from '@/constants/pollPhases';
import DoDisturbRounded from '@mui/icons-material/DoDisturbRounded';
import ThumbDownRounded from '@mui/icons-material/ThumbDownRounded';
import ThumbUpRounded from '@mui/icons-material/ThumbUpRounded';
Expand Down Expand Up @@ -42,6 +43,7 @@ export function VotingHistoryTable(props: Props): JSX.Element {
const userVoteData = votes.find(
(vote) => vote.poll_id === params.row.id,
);
const poll = polls.find((poll) => poll.id === params.row.id);
const userVote = userVoteData?.vote;
return (
<Box
Expand All @@ -66,7 +68,13 @@ export function VotingHistoryTable(props: Props): JSX.Element {
{userVote === 'abstain' && (
<DoDisturbRounded data-testid={`abstain-${params.row.id}`} />
)}
{!userVote && (
{poll?.status === pollPhases.pending ||
(poll?.status === pollPhases.voting && (
<Typography data-testid={`none-${params.row.id}`}>
In Progress
</Typography>
))}
{poll?.status === pollPhases.concluded && !userVote && (
<Typography data-testid={`none-${params.row.id}`}>
None
</Typography>
Expand Down
9 changes: 9 additions & 0 deletions src/lib/getStringBytes.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
/**
* Gets the byte size of a string
* @param str - The string to get the byte size of
* @returns Number of bytes
*/
export function getStringBytes(str: string): number {
const byteSize = new Blob([str]).size;
return byteSize;
}
5 changes: 3 additions & 2 deletions src/pages/api/updateUser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { getServerSession } from 'next-auth';

import { checkIfCO } from '@/lib/checkIfCO';
import { checkIfVoting } from '@/lib/checkIfVoting';
import { getStringBytes } from '@/lib/getStringBytes';

type Data = {
userId: string;
Expand Down Expand Up @@ -66,10 +67,10 @@ export default async function updateUser(
message: 'Name must be provided.',
});
}
if (name.length > 100) {
if (getStringBytes(name) > 64) {
return res.status(400).json({
userId: BigInt(-1).toString(),
message: 'Name must be less than 100 characters.',
message: 'Name must be less than 64 characters.',
});
}
// validate email
Expand Down
Loading

0 comments on commit 31095c4

Please sign in to comment.