Skip to content

Commit

Permalink
fix candidate votes test
Browse files Browse the repository at this point in the history
  • Loading branch information
lkleuver committed Feb 11, 2025
1 parent f5e34b0 commit 87525a1
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 82 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { ErrorModal } from "app/component/error";
import { PollingStationFormNavigation } from "app/component/pollingstation/PollingStationFormNavigation";

import { ApiError, PoliticalGroup } from "@kiesraad/api";
import { t } from "@kiesraad/i18n";
Expand All @@ -15,19 +16,30 @@ import {
KeyboardKeys,
} from "@kiesraad/ui";

import { formValuesToValues } from "./candidatesVotesValues";
import { useCandidateVotes } from "./useCandidateVotes";

export interface CandidatesVotesFormProps {
group: PoliticalGroup;
}

export function CandidatesVotesForm({ group }: CandidatesVotesFormProps) {
const { error, formRef, onSubmit, currentValues, setValues, formSection, status, setAcceptWarnings, defaultProps } =
useCandidateVotes(group.number);
const {
error,
formRef,
onSubmit,
currentValues,
setValues,
formSection,
status,
setAcceptWarnings,
defaultProps,
pollingStationResults,
} = useCandidateVotes(group.number);

const showAcceptWarnings = formSection.warnings.length > 0 && formSection.errors.length === 0;

const missingTotalError = currentValues.candidate_votes.some((v) => v !== "") && currentValues.total == "0";
const missingTotalError = currentValues.candidate_votes.some((v) => v !== "") && !currentValues.total;

const handleSubmit = (e: React.FormEvent<HTMLFormElement>) => {
e.preventDefault();
Expand All @@ -46,10 +58,14 @@ export function CandidatesVotesForm({ group }: CandidatesVotesFormProps) {
title={`${t("list")} ${group.number} - ${group.name}`}
>
{error instanceof ApiError && <ErrorModal error={error} />}
{/* <PollingStationFormNavigation
<PollingStationFormNavigation
onSubmit={onSubmit}
currentValues={formValuesToValues(currentValues)}
/> */}
currentValues={{
political_group_votes: pollingStationResults.political_group_votes.map((pg) =>
pg.number !== group.number ? pg : formValuesToValues(currentValues),
),
}}
/>
{formSection.isSaved && formSection.errors.length > 0 && (
<Feedback id="feedback-error" type="error" data={formSection.errors.map((error) => error.code)} />
)}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,15 @@ export type CandidateVotesValues = PoliticalGroupVotes;
export function valuesToFormValues(values: PoliticalGroupVotes): CandidateVotesFormValues {
return {
number: values.number,
total: values.total.toString(),
total: formatNumber(values.total),
candidate_votes: values.candidate_votes.map((cv) => formatNumber(cv.votes)),
};
}

export function formValuesToValues(formData: CandidateVotesFormValues): PoliticalGroupVotes {
return {
number: formData.number,
total: parseInt(formData.total),
total: deformatNumber(formData.total),
candidate_votes: formData.candidate_votes.map((votes, index) => ({
number: index + 1,
votes: deformatNumber(votes),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,76 +99,3 @@ export function useCandidateVotes(political_group_number: number) {
defaultProps,
};
}

// export function useDataEntryForm<TValues, TFormValues>(
// sectionReference:FormSectionReference,
// getValues: (pollingStationResults: PollingStationResults) => TValues,
// valuesToFormValues: (values: TValues) => TFormValues,
// getData: (pollingStationResults: PollingStationResults) => Partial<PollingStationResults>,
// ) {
// const { cache, status, pollingStationResults, formState, onSubmitForm, updateFormSection } = useDataEntryContext(sectionReference);

// // local form state
// const defaultValues =
// cache?.key === sectionReference.id
// ? (cache.data as TValues)
// : getValues(pollingStationResults);

// const [currentValues, setCurrentValues] = useState<TFormValues>(valuesToFormValues(defaultValues));

// // derived state
// const section = formState.sections[sectionReference.id];
// if (!section) {
// throw new Error(`Form section not found for ${sectionReference.id}`);
// }

// const { errors, warnings, isSaved, acceptWarnings, hasChanges } = section;

// const defaultProps = {
// errorsAndWarnings: isSaved ? getErrorsAndWarnings(errors, warnings) : undefined,
// warningsAccepted: acceptWarnings,
// };

// // register changes when fields change
// const setValues = (values: TFormValues) => {
// if (!hasChanges) {
// updateFormSection({ hasChanges: true, acceptWarnings: false, acceptWarningsError: false });
// }
// setCurrentValues(values);
// };

// const setAcceptWarnings = (acceptWarnings: boolean) => {
// updateFormSection({ acceptWarnings });
// };

// // form keyboard navigation
// const formRef = useRef<HTMLFormElement>(null);
// useFormKeyboardNavigation(formRef);

// // submit and save to form contents
// const onSubmit = async (options?: SubmitCurrentFormOptions): Promise<boolean> => {
// return await onSubmitForm(
// getData(pollingStationResults),
// options,
// );
// };

// // scroll to top when saved
// useEffect(() => {
// if (isSaved) {
// window.scrollTo(0, 0);
// }
// }, [isSaved]);

// return {
// formRef,
// onSubmit,
// pollingStationResults,
// currentValues,
// formSection: section,
// setValues,
// status,
// setAcceptWarnings,
// defaultProps,
// };
// }
Original file line number Diff line number Diff line change
Expand Up @@ -447,7 +447,7 @@ describe("Polling Station data entry integration tests", () => {
await expectBlockerModal();
});

test.only("Navigating with saved changes goes to correct form", async () => {
test("Navigating with saved changes goes to correct form", async () => {
const router = renderWithRouter();
await startPollingStationInput(router);
await expectRecountedForm();
Expand Down

0 comments on commit 87525a1

Please sign in to comment.