generated from mantinedev/vite-template
-
Notifications
You must be signed in to change notification settings - Fork 1
Store household calculation outputs in Simulation records instead of Report #235
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
Open
SakshiKekre
wants to merge
4
commits into
main
Choose a base branch
from
fix/issue-226-household-simulation-outputs
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
1d266fd
feat/fix: Store household calculation outputs in Simulation records
SakshiKekre e6d3fbb
fix: Update test to expect simulationIds
SakshiKekre cbbaf20
fix: Some more test fixes
SakshiKekre d7933cd
fix: Refactor household report calculation handler to run multiple si…
SakshiKekre File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,8 @@ | ||
import { useQueryClient } from '@tanstack/react-query'; | ||
import { useQuery, useQueryClient } from '@tanstack/react-query'; | ||
import { EconomyReportOutput } from '@/api/economy'; | ||
import { CalculationMeta } from '@/api/reportCalculations'; | ||
import { Household, HouseholdData } from '@/types/ingredients/Household'; | ||
import { fetchSimulationById } from '@/api/simulation'; | ||
import { Household } from '@/types/ingredients/Household'; | ||
import { useReportOutput } from './useReportOutput'; | ||
import { useUserReportById } from './useUserReports'; | ||
|
||
|
@@ -99,14 +100,27 @@ export function useReportData(userReportId: string): ReportDataResult { | |
} = normalizedReport; | ||
const baseReportId = report?.id; | ||
|
||
// Step 2: Fetch report output using base reportId | ||
// Step 2: Get metadata early to determine if we need simulation data | ||
const metadata = queryClient.getQueryData<CalculationMeta>(['calculation-meta', baseReportId]); | ||
const outputType: ReportOutputType | undefined = metadata?.type; | ||
const reformSimId = metadata?.simulationIds?.[1] || metadata?.simulationIds?.[0]; | ||
|
||
// Step 3: Fetch report output using base reportId | ||
// This hook must be called unconditionally to comply with Rules of Hooks | ||
const reportOutputResult = useReportOutput({ | ||
reportId: baseReportId || '', | ||
enabled: !!baseReportId, // Only enable when we have a valid base report ID | ||
}); | ||
|
||
// Step 3: Handle loading and error states | ||
// Step 4: Fetch simulation output for household reports | ||
// This hook must be called unconditionally to comply with Rules of Hooks | ||
const { data: simulationData } = useQuery({ | ||
queryKey: ['simulation', reformSimId || 'none'], | ||
queryFn: () => fetchSimulationById(metadata!.countryId, reformSimId!), | ||
enabled: !!baseReportId && outputType === 'household' && !!reformSimId, | ||
}); | ||
|
||
// Step 5: Handle loading and error states | ||
if (normalizedLoading) { | ||
return LOADING_PROPS; | ||
} | ||
|
@@ -118,7 +132,7 @@ export function useReportData(userReportId: string): ReportDataResult { | |
}; | ||
} | ||
|
||
// Step 4: Process the report output result | ||
// Step 6: Process the report output result | ||
const { status, data, error } = reportOutputResult; | ||
|
||
// Extract progress information if status is pending | ||
|
@@ -132,21 +146,32 @@ export function useReportData(userReportId: string): ReportDataResult { | |
estimatedTimeRemaining: undefined, | ||
}; | ||
|
||
// Determine output type from cached metadata | ||
const metadata = queryClient.getQueryData<CalculationMeta>(['calculation-meta', baseReportId]); | ||
const outputType: ReportOutputType | undefined = metadata?.type; | ||
|
||
// Wrap household data in Household structure | ||
// The API returns raw HouseholdData, but components expect the Household wrapper | ||
// For economy reports, use the report output directly | ||
// For household reports, construct from simulation output | ||
let output: EconomyReportOutput | Household | null | undefined = data; | ||
|
||
if (outputType === 'household' && data) { | ||
const wrappedOutput: Household = { | ||
id: baseReportId, | ||
countryId: metadata?.countryId || 'us', | ||
householdData: data as HouseholdData, | ||
}; | ||
output = wrappedOutput; | ||
if (outputType === 'household' && simulationData?.output_json) { | ||
const parsed = | ||
typeof simulationData.output_json === 'string' | ||
? JSON.parse(simulationData.output_json) | ||
: simulationData.output_json; | ||
|
||
// Check if already wrapped (has countryId and householdData properties) | ||
if (parsed.countryId && parsed.householdData) { | ||
// Already a Household object, just add the ID | ||
output = { | ||
...parsed, | ||
id: baseReportId, | ||
}; | ||
} else { | ||
// Raw HouseholdData, need to wrap it | ||
const wrappedOutput: Household = { | ||
id: baseReportId, | ||
countryId: metadata!.countryId, | ||
householdData: parsed, | ||
}; | ||
output = wrappedOutput; | ||
} | ||
Comment on lines
+160
to
+174
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. question: Why is this necessary? |
||
} | ||
|
||
return { | ||
|
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
question: Are you doing only the reform because in a next PR, you'll add the code for the comparison?