Skip to content

Commit

Permalink
Use quick poll data if it exists in CreateElection
Browse files Browse the repository at this point in the history
  • Loading branch information
mikefranze committed Nov 22, 2023
1 parent e5516bb commit be6ca13
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 27 deletions.
28 changes: 22 additions & 6 deletions frontend/src/components/ElectionForm/CreateElectionTemplates.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,15 @@ import { Election } from '../../../../domain_model/Election';
import { usePostElection } from '../../hooks/useAPI';
import { DateTime } from 'luxon'
import { Card, CardActionArea, CardMedia, CardContent, Typography, Box, Grid } from '@mui/material';
import { useLocalStorage } from '../../hooks/useLocalStorage';



const CreateElectionTemplates = ({ authSession }: { authSession: IAuthSession }) => {

const navigate = useNavigate()
const { error, isPending, makeRequest: postElection } = usePostElection()
const [quickPoll, setQuickPoll] = useLocalStorage<Election>('QuickPoll', null)

const defaultElection: Election = {
title: '',
Expand Down Expand Up @@ -41,6 +43,19 @@ const CreateElectionTemplates = ({ authSession }: { authSession: IAuthSession })
election.frontend_url = ''
election.owner_id = authSession.getIdField('sub')
election.state = 'draft'
if (quickPoll) {
// If quick poll exists grab the title from it, if quick poll not filled in will just be empty string
election.title = quickPoll.title
// Grab candidates from quick poll that have a candidate name filled out
let candidates = quickPoll.races[0].candidates.filter(candidate => candidate.candidate_name.length > 0)
if (candidates.length > 0) {
// Set the race to use the same title as the quick poll so it's easy to identify, add candidates
election.races = quickPoll.races
election.races[0].title = quickPoll.title
election.races[0].candidates = candidates
}

}

const newElection = await postElection(
{
Expand All @@ -49,6 +64,7 @@ const CreateElectionTemplates = ({ authSession }: { authSession: IAuthSession })
if ((!newElection)) {
throw Error("Error submitting election");
}
setQuickPoll(null)
navigate(`/Election/${newElection.election.election_id}/admin`)
}
const cardHeight = 220
Expand Down Expand Up @@ -85,7 +101,7 @@ const CreateElectionTemplates = ({ authSession }: { authSession: IAuthSession })
</Card>
</Grid>
<Grid item xs={12} sm={6} md={3}>
<Card sx={{ backgroundColor: 'brand.gray1', minHeight: cardHeight }}>
<Card sx={{ backgroundColor: 'brand.gray1', minHeight: cardHeight }}>
<CardActionArea
onClick={() => onAddElection(election => {
election.settings.voter_access = 'open'
Expand All @@ -106,7 +122,7 @@ const CreateElectionTemplates = ({ authSession }: { authSession: IAuthSession })
</Card>
</Grid>
<Grid item xs={12} sm={6} md={3}>
<Card sx={{ backgroundColor: 'brand.gray1', minHeight: cardHeight }}>
<Card sx={{ backgroundColor: 'brand.gray1', minHeight: cardHeight }}>
<CardActionArea
onClick={() => onAddElection(election => {
election.settings.voter_access = 'open'
Expand All @@ -127,7 +143,7 @@ const CreateElectionTemplates = ({ authSession }: { authSession: IAuthSession })
</Card>
</Grid>
<Grid item xs={12} sm={6} md={3}>
<Card sx={{ backgroundColor: 'brand.gray1', minHeight: cardHeight }}>
<Card sx={{ backgroundColor: 'brand.gray1', minHeight: cardHeight }}>
<CardActionArea
onClick={() => onAddElection(election => {
election.settings.voter_access = 'registration'
Expand All @@ -148,7 +164,7 @@ const CreateElectionTemplates = ({ authSession }: { authSession: IAuthSession })
</Card>
</Grid>
<Grid item xs={12} sm={6} md={3}>
<Card sx={{ backgroundColor: 'brand.gray1', minHeight: cardHeight }}>
<Card sx={{ backgroundColor: 'brand.gray1', minHeight: cardHeight }}>
<CardActionArea
onClick={() => onAddElection(election => {
election.settings.voter_access = 'closed'
Expand All @@ -169,7 +185,7 @@ const CreateElectionTemplates = ({ authSession }: { authSession: IAuthSession })
</Card>
</Grid>
<Grid item xs={12} sm={6} md={3}>
<Card sx={{ backgroundColor: 'brand.gray1', minHeight: cardHeight }}>
<Card sx={{ backgroundColor: 'brand.gray1', minHeight: cardHeight }}>
<CardActionArea
onClick={() => onAddElection(election => {
election.settings.voter_access = 'closed'
Expand All @@ -190,7 +206,7 @@ const CreateElectionTemplates = ({ authSession }: { authSession: IAuthSession })
</Card>
</Grid>
<Grid item xs={12} sm={6} md={3}>
<Card sx={{ backgroundColor: 'brand.gray1', minHeight: cardHeight }}>
<Card sx={{ backgroundColor: 'brand.gray1', minHeight: cardHeight }}>
<CardActionArea
onClick={() => onAddElection(election => {
election.settings.voter_access = 'closed'
Expand Down
36 changes: 20 additions & 16 deletions frontend/src/components/ElectionForm/QuickPoll.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,29 +11,22 @@ import { useLocalStorage } from '../../hooks/useLocalStorage';
import Typography from '@mui/material/Typography';
import { usePostElection } from '../../hooks/useAPI';
import { useCookie } from '../../hooks/useCookie';
import { Election } from '../../../../domain_model/Election.js';

const QuickPoll = ({ authSession }) => {
const [tempID, setTempID] = useCookie('temp_id', '0')
const navigate = useNavigate()
const { error, isPending, makeRequest: postElection } = usePostElection()
const onSubmitElection = async (election) => {
// calls post election api, throws error if response not ok
const newElection = await postElection(
{
Election: election,
})
if ((!newElection)) {
throw Error("Error submitting election");
}
localStorage.removeItem('Election')
navigate(`/Election/${newElection.election.election_id}`)
}

const QuickPollTemplate = {
const QuickPollTemplate: Election = {
title: '',
election_id: '0',
state: 'open',
frontend_url: '',
owner_id: '0',
races: [
{
{
title: '',
race_id: '0',
num_winners: 1,
voting_method: 'STAR',
Expand Down Expand Up @@ -67,9 +60,20 @@ const QuickPoll = ({ authSession }) => {
}


const [election, setElectionData] = useLocalStorage('Election', QuickPollTemplate)
const [election, setElectionData] = useLocalStorage<Election>('QuickPoll', QuickPollTemplate)
const [titleError, setTitleError] = useState(false)

const onSubmitElection = async (election) => {
// calls post election api, throws error if response not ok
const newElection = await postElection(
{
Election: election,
})
if ((!newElection)) {
throw Error("Error submitting election");
}
setElectionData(null)
navigate(`/Election/${newElection.election.election_id}`)
}
const applyElectionUpdate = (updateFunc) => {
const electionCopy = structuredClone(election)
updateFunc(electionCopy)
Expand Down
10 changes: 5 additions & 5 deletions frontend/src/hooks/useLocalStorage.ts
Original file line number Diff line number Diff line change
@@ -1,25 +1,25 @@
import { useState, useEffect } from "react";

export const useLocalStorage = (key: string, defaultValue: any, updateRate: number|null = null): [any, (any)=>void] => {
export const useLocalStorage = <data>(key: string, defaultValue: data|null, updateRate: number|null = null): [data|null,(newValue:data|null)=>void] => {
// This hook behaves similarly to useState however the state is also stored in local storage
// If the value in local storage doesn't exist it is set to defaultValue
// The optional input updateRate allows for periodic checking to see if the value in local storage has changed
// to allow multiple components using the same key to be updated
const getStoredValue = (key: string, defaultValue: any) => {
const getStoredValue = (key: string, defaultValue: data|null) => {
// getting stored value
const saved = localStorage.getItem(key);
const initial = JSON.parse(saved);
const initial: data|null = JSON.parse(saved);
if (!initial) {
localStorage.setItem(key, JSON.stringify(defaultValue));
return defaultValue
}
return initial;
}
const [value, setStoredValue] = useState(() => {
const [value, setStoredValue] = useState<data|null>(() => {
return getStoredValue(key, defaultValue);
});

const setValue = (newValue) => {
const setValue = (newValue: data|null) => {
localStorage.setItem(key, JSON.stringify(newValue));
setStoredValue(newValue)
}
Expand Down

0 comments on commit be6ca13

Please sign in to comment.