Skip to content

Commit

Permalink
Merge pull request #718 from NBISweden/hotfix/bug_in_yearly_submission
Browse files Browse the repository at this point in the history
Refactor YearlyReportForm component to streamline initial values and …
  • Loading branch information
jhagberg authored Jan 7, 2025
2 parents 501f8e6 + b2380ac commit 4b54bd8
Showing 1 changed file with 26 additions and 14 deletions.
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

0 comments on commit 4b54bd8

Please sign in to comment.