Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor YearlyReportForm component to streamline initial values and … #718

Merged
merged 1 commit into from
Jan 7, 2025
Merged
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
40 changes: 26 additions & 14 deletions frontend/src/YearlyReportForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -114,19 +114,13 @@ const YearlyReportForm: React.FC<YearlyReportFormProps> = ({
): ReportValues => {
// Start with default values
const values: ReportValues = {
// User-editable fields - can come from existing data
genebankNumber: herdData.herd || "",
breed: herdData.breed || "",
gotlandskanin: herdData.genebank === 1,
mellerudskanin: herdData.genebank === 2,
breedingYear: reportYear || new Date().getFullYear(),
endingGenbank: existingData?.endingGenbank || false,
numberOfLitters: 0,
totalBorn: 0,
totalAliveAfterSixWeeks: 0,
numberOfFemalesUsedInBreeding: 0,
numberOfMalesUsedInBreeding: 0,
numberOfFemalesWithCertificate: 0,
numberOfMalesWithCertificate: 0,
allowPublication: existingData?.allowPublication || [],
eligibleForSupport: existingData?.eligibleForSupport || false,
notEligibleForSupport: existingData?.notEligibleForSupport || false,
Expand All @@ -150,6 +144,14 @@ const YearlyReportForm: React.FC<YearlyReportFormProps> = ({
age: "",
},
},
// Computed fields - always start at 0, will be calculated below
numberOfLitters: 0,
totalBorn: 0,
totalAliveAfterSixWeeks: 0,
numberOfFemalesUsedInBreeding: 0,
numberOfMalesUsedInBreeding: 0,
numberOfFemalesWithCertificate: 0,
numberOfMalesWithCertificate: 0,
};

// Calculate values based on current herd data
Expand Down Expand Up @@ -193,8 +195,6 @@ const YearlyReportForm: React.FC<YearlyReportFormProps> = ({
);
values.numberOfFemalesUsedInBreeding = uniqueMothers.size;
values.numberOfMalesUsedInBreeding = uniqueFathers.size;
values.numberOfFemalesWithCertificate = femaleRabbits.length;
values.numberOfMalesWithCertificate = maleRabbits.length;

return values;
};
Expand All @@ -220,18 +220,30 @@ const YearlyReportForm: React.FC<YearlyReportFormProps> = ({
reportYear || new Date().getFullYear()
} ${herdResponse.herd} ${herdName}`;

// Recalculate all computed values before submitting
const computedValues = calculateInitialValues(herdResponse, null);
const payload = {
data: {
...values,
...computedValues,
// Only include user-editable fields from values
// Use the values from the form - they are already correctly calculated
// and are read-only in the form
numberOfLitters: values.numberOfLitters,
totalBorn: values.totalBorn,
totalAliveAfterSixWeeks: values.totalAliveAfterSixWeeks,
numberOfFemalesUsedInBreeding: values.numberOfFemalesUsedInBreeding,
numberOfMalesUsedInBreeding: values.numberOfMalesUsedInBreeding,
numberOfFemalesWithCertificate:
values.numberOfFemalesWithCertificate,
numberOfMalesWithCertificate: values.numberOfMalesWithCertificate,
// User-editable fields
allowPublication: values.allowPublication,
diseases: values.diseases,
defectsMalformations: values.defectsMalformations,
endingGenbank: values.endingGenbank,
eligibleForSupport: values.eligibleForSupport,
// Other required fields
genebankNumber: values.genebankNumber,
breed: values.breed,
gotlandskanin: values.gotlandskanin,
mellerudskanin: values.mellerudskanin,
breedingYear: values.breedingYear,
},
name: reportName,
version: "1.0",
Expand Down
Loading