Skip to content
Merged
Show file tree
Hide file tree
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
4 changes: 3 additions & 1 deletion app/src/adapters/ReportAdapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ export class ReportAdapter {
return {
id: String(metadata.id),
countryId: metadata.country_id,
year: metadata.year,
apiVersion: metadata.api_version,
simulationIds,
status: this.mapApiStatusToReportStatus(metadata.status),
Expand All @@ -48,7 +49,7 @@ export class ReportAdapter {

/**
* Converts Report to format for API POST request
* API expects snake_case format - only simulation IDs needed for creation
* API expects snake_case format - simulation IDs and year needed for creation
*/
static toCreationPayload(report: Report): ReportCreationPayload {
// Extract simulation IDs from array
Expand All @@ -57,6 +58,7 @@ export class ReportAdapter {
return {
simulation_1_id: parseInt(simulation1Id, 10),
simulation_2_id: simulation2Id ? parseInt(simulation2Id, 10) : null,
year: report.year,
};
}

Expand Down
7 changes: 3 additions & 4 deletions app/src/api/reportCalculations.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { CURRENT_YEAR } from '@/constants';
import { countryIds } from '@/libs/countries';
import { fetchHouseholdCalculation } from './householdCalculation';
import {
Expand All @@ -19,6 +18,7 @@ export interface CalculationMeta {
};
populationId: string;
region?: string;
year: string; // Report calculation year (e.g., '2025')
}

/**
Expand All @@ -45,13 +45,12 @@ export async function fetchCalculationWithMeta(meta: CalculationMeta) {
}
} else {
console.log('[fetchCalculationWithMeta] Type is economy');
// TODO: Update to use dynamic time_period when available in UI
console.log(
`[fetchCalculationWithMeta] Temporarily using ${CURRENT_YEAR} as time_period for economy calculation`
`[fetchCalculationWithMeta] Using year ${meta.year} as time_period for economy calculation`
);
const params: SocietyWideCalculationParams = {
region: meta.region || meta.countryId,
time_period: CURRENT_YEAR,
time_period: meta.year,
};

console.log('[fetchCalculationWithMeta] Society-wide calculation params:');
Expand Down
7 changes: 4 additions & 3 deletions app/src/components/household/VariableArithmetic.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ import { useState } from 'react';
import { IconCircleMinus, IconCirclePlus, IconTriangleFilled } from '@tabler/icons-react';
import { useSelector } from 'react-redux';
import { ActionIcon, Box, Group, Text } from '@mantine/core';
import { CURRENT_YEAR } from '@/constants';
import { spacing, typography } from '@/designTokens';
import { useReportYear } from '@/hooks/useReportYear';
import { RootState } from '@/store';
import { Household } from '@/types/ingredients/Household';
import { calculateVariableComparison } from '@/utils/householdComparison';
Expand Down Expand Up @@ -39,6 +39,7 @@ export default function VariableArithmetic({
childrenOnly = false,
}: VariableArithmeticProps) {
const [expanded, setExpanded] = useState(defaultExpanded);
const reportYear = useReportYear();
const metadata = useSelector((state: RootState) => state.metadata);

const variable = metadata.variables[variableName];
Expand All @@ -59,7 +60,7 @@ export default function VariableArithmetic({
// It's a parameter name - resolve it
const parameter = metadata.parameters[variable.adds];
if (parameter) {
addsArray = getParameterAtInstant(parameter, `${CURRENT_YEAR}-01-01`) || [];
addsArray = getParameterAtInstant(parameter, `${reportYear}-01-01`) || [];
}
} else if (Array.isArray(variable.adds)) {
addsArray = variable.adds;
Expand All @@ -71,7 +72,7 @@ export default function VariableArithmetic({
// It's a parameter name - resolve it
const parameter = metadata.parameters[variable.subtracts];
if (parameter) {
subtractsArray = getParameterAtInstant(parameter, `${CURRENT_YEAR}-01-01`) || [];
subtractsArray = getParameterAtInstant(parameter, `${reportYear}-01-01`) || [];
}
} else if (Array.isArray(variable.subtracts)) {
subtractsArray = variable.subtracts;
Expand Down
39 changes: 26 additions & 13 deletions app/src/components/policyParameterSelectorFrame/ValueSetter.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import {
} from '@mantine/core';
import { DatePickerInput, YearPickerInput } from '@mantine/dates';
import { FOREVER } from '@/constants';
import { getDateRange } from '@/libs/metadataUtils';
import { getDateRange, getTaxYears } from '@/libs/metadataUtils';
import { selectActivePolicy, selectCurrentPosition } from '@/reducers/activeSelectors';
import { addPolicyParamAtPosition } from '@/reducers/policyReducer';
import { RootState } from '@/store';
Expand Down Expand Up @@ -112,8 +112,9 @@ export default function PolicyParameterSelectorValueSetterContainer(
const [intervals, setIntervals] = useState<ValueInterval[]>([]);

// Hoisted date state for all non-multi-year selectors
const [startDate, setStartDate] = useState<string>('2025-01-01');
const [endDate, setEndDate] = useState<string>('2025-12-31');
const currentYear = new Date().getFullYear();
const [startDate, setStartDate] = useState<string>(`${currentYear}-01-01`);
const [endDate, setEndDate] = useState<string>(`${currentYear}-12-31`);

function resetValueSettingState() {
setIntervals([]);
Expand Down Expand Up @@ -407,22 +408,34 @@ export function DateValueSelector(props: ValueSetterProps) {
}

export function MultiYearValueSelector(props: ValueSetterProps) {
const { param, setIntervals, maxDate } = props;
const { param, setIntervals } = props;

// Get active policy to check for user-set reform values
const activePolicy = useSelector(selectActivePolicy);

const MAX_YEARS = 10;
// Get available years from metadata
const availableYears = useSelector(getTaxYears);
const countryId = useSelector((state: RootState) => state.metadata.currentCountry);

// Generate years from minDate to maxDate, starting from 2025
// Country-specific max years configuration
const MAX_YEARS_BY_COUNTRY: Record<string, number> = {
us: 10,
uk: 5,
};

// Generate years from metadata, starting from current year
const generateYears = () => {
const startYear = 2025;
const endYear = dayjs(maxDate).year();
const years = [];
for (let year = startYear; year <= endYear; year++) {
years.push(year);
}
return years.slice(0, MAX_YEARS);
const currentYear = new Date().getFullYear();
const maxYears = MAX_YEARS_BY_COUNTRY[countryId || 'us'] || 10;

// Filter available years from metadata to only include current year onwards
const futureYears = availableYears
.map((option) => parseInt(option.value, 10))
.filter((year) => year >= currentYear)
.sort((a, b) => a - b);

// Take only the configured max years for this country
return futureYears.slice(0, maxYears);
};

const years = generateYears();
Expand Down
24 changes: 21 additions & 3 deletions app/src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,28 @@ export const CURRENT_YEAR = '2025';
export const TEMP_REPORT_TIME = 2025;

/**
* Default date for parameter definition lookups when no specific date is provided
* Used for querying current law parameter values from metadata
* Get parameter definition date for a given year
* Used for querying parameter values from metadata at a specific point in time
*
* @param year - The year to get the parameter definition date for (e.g., '2025')
* @returns Date string in format 'YYYY-01-01' for querying parameters
*
* @example
* ```typescript
* const date = getParamDefinitionDate('2025'); // Returns '2025-01-01'
* ```
*/
export const UNCONFIRMED_PARAM_DEFINITION_DATE = '2025-01-01';
export function getParamDefinitionDate(year?: string): string {
if (!year) {
console.error(
'[getParamDefinitionDate] No year provided - this is likely a bug. ' +
`Falling back to CURRENT_YEAR (${CURRENT_YEAR}). ` +
'Please ensure year is passed from report context.'
);
return `${CURRENT_YEAR}-01-01`;
}
return `${year}-01-01`;
}

/**
* Mock user ID used for anonymous/unauthenticated users
Expand Down
Loading
Loading