Skip to content
Open
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
66 changes: 64 additions & 2 deletions packages/frontend/src/components/Elections/ElectionsYouManage.tsx
Original file line number Diff line number Diff line change
@@ -1,18 +1,23 @@
import { useEffect, useMemo } from 'react'
import { Election } from "@equal-vote/star-vote-shared/domain_model/Election"
import useAuthSession from '../AuthSessionContextProvider';
import { useClaimElection, useGetElections } from '../../hooks/useAPI';
import { archiveElections, BulkArchiveTarget, useClaimElection, useGetElections } from '../../hooks/useAPI';
import { useNavigate } from 'react-router';
import EnhancedTable from '../EnhancedTable';
import EnhancedTable, { BulkAction } from '../EnhancedTable';
import { PrimaryButton } from '../styles';
import useSnackbar from '../SnackbarContext';
import useConfirm from '../ConfirmationDialogProvider';
import { useSubstitutedTranslation } from '../util';
import { useSessionStorage } from '~/hooks/useSessionStorage';
import { useCookie } from '~/hooks/useCookie';
import ArchiveIcon from '@mui/icons-material/Archive';

const ElectionsYouManage = () => {
const navigate = useNavigate();
const authSession = useAuthSession()
const { setSnack } = useSnackbar()
const confirm = useConfirm()
const { t } = useSubstitutedTranslation()

const { data, isPending, makeRequest: fetchElections } = useGetElections()

Expand Down Expand Up @@ -74,6 +79,61 @@ const ElectionsYouManage = () => {
}
}, [data]);

// Only the owner can change an election's state (permissions.canEditElectionState),
// and this table also lists elections where you're merely an admin or auditor —
// so anything you don't own is reported as skipped rather than sent and 401'd.
const bulkArchive = async (rows: {raw: Election}[]) => {
const targets: BulkArchiveTarget[] = rows
.filter(row => row.raw.owner_id === id && row.raw.state !== 'archived')
.map(row => ({
election_id: String(row.raw.election_id),
title: row.raw.title,
update_date: String(row.raw.update_date),
}))
const ineligible = rows.length - targets.length

if (targets.length === 0) {
setSnack({
message: t('bulk_actions.archive.none_eligible'),
severity: 'warning',
open: true,
autoHideDuration: 6000,
})
return
}

const confirmed = await confirm({
title: t('bulk_actions.archive.confirm_title'),
message: t('bulk_actions.archive.confirm_message', { count: targets.length, ineligible }),
submit: t('bulk_actions.archive.confirm_submit', { count: targets.length }),
})
if (!confirmed) return

const outcome = await archiveElections(targets)
const parts = [t('bulk_actions.archive.done', { count: outcome.archived.length })]
if (ineligible > 0) parts.push(t('bulk_actions.archive.skipped', { count: ineligible }))
if (outcome.failed.length > 0) parts.push(t('bulk_actions.archive.failed', {
count: outcome.failed.length,
first_title: outcome.failed[0].target.title,
first_error: outcome.failed[0].message,
}))

setSnack({
message: parts.join(' '),
severity: outcome.failed.length > 0 ? 'warning' : 'success',
open: true,
autoHideDuration: outcome.failed.length > 0 ? null : 6000,
})
fetchElections()
}

const bulkActions: BulkAction[] = [{
key: 'archive',
label: t('bulk_actions.archive.menu_item'),
icon: <ArchiveIcon fontSize='small'/>,
onAction: bulkArchive,
}]

return <EnhancedTable
title='My Elections & Polls'
headKeys={['title', 'update_date', 'election_state', 'start_time', 'end_time', 'description']}
Expand All @@ -83,6 +143,8 @@ const ElectionsYouManage = () => {
handleOnClick={(row) => navigate(`/${String(row.raw.election_id)}`)}
defaultSortBy='update_date'
emptyContent={<>You don&apos;t have any elections yet<br/><PrimaryButton onClick={() => navigate('/new_election')}>Create Election</PrimaryButton></>}
bulkActions={bulkActions}
getRowId={(row) => String(row.raw.election_id)}
/>
}

Expand Down
Loading
Loading