Skip to content

Commit

Permalink
Removing duplicate election component, add function to post new elect…
Browse files Browse the repository at this point in the history
…ion in draft phase
  • Loading branch information
mikefranze committed Nov 6, 2023
1 parent b88d42d commit df538e9
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 67 deletions.
3 changes: 0 additions & 3 deletions frontend/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,7 @@ import Header from './components/Header'
import Elections from './components/Elections'
import Login from './components/Login'
import CreateElectionTemplates from './components/ElectionForm/CreateElectionTemplates'
// import AddElection from './components/ElectionForm/AddElection'
import Election from './components/Election/Election'
import DuplicateElection from './components/ElectionForm/DuplicateElection'
import Sandbox from './components/Sandbox'
import DebugPage from './components/DebugPage'
import LandingPage from './components/LandingPage'
Expand Down Expand Up @@ -58,7 +56,6 @@ const App = () => {
<Route path='/Debug' element={<DebugPage authSession={authSession} />} />
<Route path='/CreateElection' element={<CreateElectionTemplates authSession={authSession} />} />
<Route path='/Election/:id/*' element={<Election authSession={authSession} />} />
<Route path='/DuplicateElection/:id' element={<DuplicateElection authSession={authSession} />} />
<Route path='/Sandbox' element={<Sandbox />} />
</Routes>
</Box>
Expand Down
2 changes: 1 addition & 1 deletion frontend/src/components/Election/Admin/Admin.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ const Admin = ({ authSession, election, permissions, fetchElection }) => {
return (
<Container>
<Routes>
<Route path='/' element={<AdminHome />} />
<Route path='/' element={<AdminHome authSession={authSession}/>} />
<Route path='/voters' element={<ViewElectionRolls election={election} permissions={permissions} />} />
<Route path='/roles' element={<EditRoles election={election} permissions={permissions} fetchElection={fetchElection} />} />
<Route path='/ballots' element={<ViewBallots election={election} permissions={permissions} />} />
Expand Down
47 changes: 40 additions & 7 deletions frontend/src/components/Election/Admin/AdminHome.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,18 @@ import Grid from "@mui/material/Grid";
import { Box, Divider, Paper } from "@mui/material";
import { Typography } from "@mui/material";
import { StyledButton } from "../../styles";
import { Link } from 'react-router-dom';
import { Link, useNavigate } from 'react-router-dom';
import { Election } from '../../../../../domain_model/Election';
import ShareButton from "../ShareButton";
import { useArchiveEleciton, useFinalizeEleciton, useSetPublicResults } from "../../../hooks/useAPI";
import { useArchiveEleciton, useFinalizeEleciton, usePostElection, useSetPublicResults } from "../../../hooks/useAPI";
import { formatDate } from '../../util';
import useConfirm from '../../ConfirmationDialogProvider';
import useElection from '../../ElectionContextProvider';
import ElectionDetailsInlineForm from '../../ElectionForm/Details/ElectionDetailsInlineForm';
import Races from '../../ElectionForm/Races/Races';
import ElectionSettings from '../../ElectionForm/ElectionSettings';
import structuredClone from '@ungap/structured-clone';
import { IAuthSession } from '../../../hooks/useAuthSession';
const hasPermission = (permissions: string[], requiredPermission: string) => {
return (permissions && permissions.includes(requiredPermission))
}
Expand Down Expand Up @@ -191,7 +193,7 @@ const PreviewBallotSection = ({ election, permissions }: { election: Election, p
/>
}

const DuplicateElectionSection = ({ election, permissions }: { election: Election, permissions: string[] }) => {
const DuplicateElectionSection = ({ election, permissions, duplicateElection }: { election: Election, permissions: string[], duplicateElection: Function }) => {
return <Section
Description={
(<>
Expand All @@ -208,13 +210,12 @@ const DuplicateElectionSection = ({ election, permissions }: { election: Electio
variant='contained'
disabled={!hasPermission(permissions, 'canEditElectionState')}
fullwidth
component={Link} to={`/DuplicateElection/${election.election_id}`}
onClick={() => duplicateElection()}
>
<Typography align='center' variant="body2">
Duplicate
</Typography>
</StyledButton>

</>)}
/>
}
Expand Down Expand Up @@ -441,7 +442,10 @@ const FinalizeSection = ({ election, permissions, finalizeElection }: { election
}


const AdminHome = () => {
type Props = {
authSession: IAuthSession}

const AdminHome = ({authSession}:Props) => {
const { election, refreshElection: fetchElection, permissions } = useElection()
const { makeRequest } = useSetPublicResults(election.election_id)
const togglePublicResults = async () => {
Expand All @@ -452,6 +456,9 @@ const AdminHome = () => {
const { makeRequest: finalize } = useFinalizeEleciton(election.election_id)
const { makeRequest: archive } = useArchiveEleciton(election.election_id)

const navigate = useNavigate()
const { error, isPending, makeRequest: postElection } = usePostElection()

const confirm = useConfirm()

const finalizeElection = async () => {
Expand Down Expand Up @@ -486,6 +493,32 @@ const AdminHome = () => {
console.log(err)
}
}
const duplicateElection = async () => {
console.log("duplicating election")
const confirmed = await confirm(
{
title: 'Confirm Duplicate Election',
message: "Are you sure you wish to duplicate this election?"
})
if (!confirmed) return
console.log('confirmed')
const copiedElection = structuredClone(election)
copiedElection.title = 'Copy of ' + copiedElection.title
copiedElection.frontend_url = ''
copiedElection.owner_id = authSession.getIdField('sub')
copiedElection.state = 'draft'

const newElection = await postElection(
{
Election: copiedElection,
})

if ((!newElection)) {
throw Error("Error submitting election");
}
navigate(`/Election/${newElection.election.election_id}/admin`)
}


return (
<Box
Expand Down Expand Up @@ -524,7 +557,7 @@ const AdminHome = () => {
<Divider style={{ width: '100%' }} />
<EditRolesSection election={election} permissions={permissions} />
<Divider style={{ width: '100%' }} />
<DuplicateElectionSection election={election} permissions={permissions} />
<DuplicateElectionSection election={election} permissions={permissions} duplicateElection={duplicateElection}/>
<Divider style={{ width: '100%' }} />
<ArchiveElectionSection election={election} permissions={permissions} archiveElection={archiveElection} />
{election.state === 'draft' &&
Expand Down
56 changes: 0 additions & 56 deletions frontend/src/components/ElectionForm/DuplicateElection.tsx

This file was deleted.

0 comments on commit df538e9

Please sign in to comment.