Skip to content

Commit

Permalink
Added IncorrectTotal error for voters_recounts to mock api
Browse files Browse the repository at this point in the history
  • Loading branch information
Lionqueen94 committed Jul 19, 2024
1 parent 93ab121 commit 7ff58b8
Show file tree
Hide file tree
Showing 7 changed files with 79 additions and 46 deletions.
24 changes: 13 additions & 11 deletions backend/src/polling_station/structs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,17 +78,19 @@ impl Validate for PollingStationResults {
validation_results: &mut ValidationResults,
field_name: String,
) {
if self.recounted && self.voters_recounts.is_none() {
validation_results.errors.push(ValidationResult {
fields: vec![
format!("{field_name}.voters_recounts.poll_card_recount"),
format!("{field_name}.voters_recounts.proxy_certificate_recount"),
format!("{field_name}.voters_recounts.voter_card_recount"),
format!("{field_name}.voters_recounts.total_admitted_voters_recount"),
],
code: ValidationResultCode::MissingRecounts,
});
}
// TODO: This needs to be implemented differently, because once you submit empty fields this
// error does not trigger, while it does trigger when you arrive at the empty Voters and Votes page
// if self.recounted && self.voters_recounts.is_none() {
// validation_results.errors.push(ValidationResult {
// fields: vec![
// format!("{field_name}.voters_recounts.poll_card_recount"),
// format!("{field_name}.voters_recounts.proxy_certificate_recount"),
// format!("{field_name}.voters_recounts.voter_card_recount"),
// format!("{field_name}.voters_recounts.total_admitted_voters_recount"),
// ],
// code: ValidationResultCode::MissingRecounts,
// });
// }

self.voters_counts.validate(
election,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ export function CandidatesVotesForm({ group }: CandidatesVotesFormProps) {

const errorsAndWarnings = useErrorsAndWarnings(errors, warnings, inputMaskWarnings);

//const blocker = useBlocker() use const blocker to render confirmation UI.
//const blocker = useBlocker() use const blocker to render confirmation UI.
useBlocker(() => {
if (formRef.current && !isCalled) {
const elements = formRef.current.elements;
Expand Down
15 changes: 8 additions & 7 deletions frontend/app/component/form/differences/DifferencesForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -61,12 +61,9 @@ export function DifferencesForm() {
[deformat],
);

function handleSubmit(event: React.FormEvent<DifferencesFormElement>) {
event.preventDefault();
const elements = event.currentTarget.elements;
setSectionValues(getValues(elements));
}
//const blocker = useBlocker() use const blocker to render confirmation UI.
const errorsAndWarnings = useErrorsAndWarnings(errors, warnings, inputMaskWarnings);

//const blocker = useBlocker() use const blocker to render confirmation UI.
useBlocker(() => {
if (formRef.current && !isCalled) {
const elements = formRef.current.elements as DifferencesFormElement["elements"];
Expand All @@ -79,7 +76,11 @@ export function DifferencesForm() {
return false;
});

const errorsAndWarnings = useErrorsAndWarnings(errors, warnings, inputMaskWarnings);
function handleSubmit(event: React.FormEvent<DifferencesFormElement>) {
event.preventDefault();
const elements = event.currentTarget.elements;
setSectionValues(getValues(elements));
}

React.useEffect(() => {
if (isCalled) {
Expand Down
24 changes: 12 additions & 12 deletions frontend/app/component/form/recounted/RecountedForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -34,18 +34,6 @@ export function RecountedForm() {
return { yes: elements.yes.checked, no: elements.no.checked };
}, []);

function handleSubmit(event: React.FormEvent<RecountedFormElement>) {
event.preventDefault();
const elements = event.currentTarget.elements;
setSectionValues(getValues(elements));

if (!elements.yes.checked && !elements.no.checked) {
setHasValidationError(true);
} else {
setHasValidationError(false);
}
}

useBlocker(() => {
if (formRef.current && !isCalled) {
const elements = formRef.current.elements as RecountedFormElement["elements"];
Expand All @@ -58,6 +46,18 @@ export function RecountedForm() {
return false;
});

function handleSubmit(event: React.FormEvent<RecountedFormElement>) {
event.preventDefault();
const elements = event.currentTarget.elements;
setSectionValues(getValues(elements));

if (!elements.yes.checked && !elements.no.checked) {
setHasValidationError(true);
} else {
setHasValidationError(false);
}
}

React.useEffect(() => {
if (isCalled) {
window.scrollTo(0, 0);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,12 +81,9 @@ export function VotersAndVotesForm() {
[deformat, recounted],
);

function handleSubmit(event: React.FormEvent<VotersAndVotesFormElement>) {
event.preventDefault();
const elements = event.currentTarget.elements;
setSectionValues(getValues(elements));
}
//const blocker = useBlocker() use const blocker to render confirmation UI.
const errorsAndWarnings = useErrorsAndWarnings(errors, warnings, inputMaskWarnings);

//const blocker = useBlocker() use const blocker to render confirmation UI.
useBlocker(() => {
if (formRef.current && !isCalled) {
const elements = formRef.current.elements as VotersAndVotesFormElement["elements"];
Expand All @@ -99,7 +96,11 @@ export function VotersAndVotesForm() {
return false;
});

const errorsAndWarnings = useErrorsAndWarnings(errors, warnings, inputMaskWarnings);
function handleSubmit(event: React.FormEvent<VotersAndVotesFormElement>) {
event.preventDefault();
const elements = event.currentTarget.elements;
setSectionValues(getValues(elements));
}

React.useEffect(() => {
if (isCalled) {
Expand Down
37 changes: 33 additions & 4 deletions frontend/lib/api-mocks/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
POLLING_STATION_DATA_ENTRY_REQUEST_BODY,
POLLING_STATION_DATA_ENTRY_REQUEST_PARAMS,
VotersCounts,
VotersRecounts,
VotesCounts,
} from "@kiesraad/api";

Expand Down Expand Up @@ -58,21 +59,22 @@ export const pollingStationDataEntryHandler = http.post<
};

const {
// recounted,
voters_counts,
votes_counts,
voters_recounts,
// differences_counts,
political_group_votes,
} = json.data;

const votesFields: (keyof VotesCounts)[] = [
const votesCountsFields: (keyof VotesCounts)[] = [
"blank_votes_count",
"invalid_votes_count",
"total_votes_cast_count",
"votes_candidates_counts",
];

votesFields.forEach((field) => {
votesCountsFields.forEach((field) => {
if (field in votes_counts) {
if (valueOutOfRange(votes_counts[field])) {
response.validation_results.errors.push({
Expand All @@ -83,14 +85,14 @@ export const pollingStationDataEntryHandler = http.post<
}
});

const votersFields: (keyof VotersCounts)[] = [
const votersCountsFields: (keyof VotersCounts)[] = [
"poll_card_count",
"proxy_certificate_count",
"total_admitted_voters_count",
"voter_card_count",
];

votersFields.forEach((field) => {
votersCountsFields.forEach((field) => {
if (valueOutOfRange(voters_counts[field])) {
response.validation_results.errors.push({
code: "OutOfRange",
Expand Down Expand Up @@ -135,7 +137,34 @@ export const pollingStationDataEntryHandler = http.post<
});
}

const votersRecountsFields: (keyof VotersRecounts)[] = [
"poll_card_recount",
"proxy_certificate_recount",
"total_admitted_voters_recount",
"voter_card_recount",
];

// TODO: This needs to be implemented differently, because once you submit empty fields this
// error does not trigger, while it does trigger when you arrive at the empty Voters and Votes page
// if (recounted && voters_recounts === undefined) {
// votersRecountsFields.forEach((field) => {
// response.validation_results.errors.push({
// code: "MissingRecounts",
// fields: [`data.voters_recounts.${field}`],
// });
// });
// }

if (voters_recounts) {
votersRecountsFields.forEach((field) => {
if (valueOutOfRange(voters_recounts[field])) {
response.validation_results.errors.push({
code: "OutOfRange",
fields: [`data.voters_recounts.${field}`],
});
}
});

// A2 + B2 + C2 = D2
if (
voters_recounts.poll_card_recount +
Expand Down
8 changes: 4 additions & 4 deletions frontend/lib/api/form/pollingstation/useVotersAndVotes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@ export function useVotersAndVotes() {
};
}, [values, setTemporaryCache, cache]);

const isCalled = React.useMemo(() => {
return sectionValues.votes_counts.total_votes_cast_count > 0;
}, [sectionValues]);

const errors = React.useMemo(() => {
if (data) {
return data.validation_results.errors.filter((err) =>
Expand Down Expand Up @@ -72,10 +76,6 @@ export function useVotersAndVotes() {
}));
};

const isCalled = React.useMemo(() => {
return sectionValues.votes_counts.total_votes_cast_count > 0;
}, [sectionValues]);

return {
loading,
sectionValues,
Expand Down

0 comments on commit 7ff58b8

Please sign in to comment.