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

Allow multiple year selections in SAP report #675

Merged
merged 1 commit into from
Aug 10, 2023
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
5 changes: 3 additions & 2 deletions backend/src/components/sap/environmentCodeReport.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { getPool, sql, textToTsQuery } from '@backend/db';
import { EnvironmentCodeReportQuery, environmentCodeReportSchema } from '@shared/schema/sapReport';

function environmentCodeReportFragment(params?: Partial<EnvironmentCodeReportQuery>) {
const years = params?.filters?.years ?? [];
return sql.fragment`
WITH total_actuals AS (
SELECT
Expand All @@ -14,9 +15,9 @@ function environmentCodeReportFragment(params?: Partial<EnvironmentCodeReportQue
sum(value_in_currency_subunit) AS "totalActuals"
FROM app.sap_actuals_item
${
params?.filters?.year != null
years.length > 0
? sql.fragment`
WHERE fiscal_year = ${params.filters.year}
WHERE fiscal_year = ANY(${sql.array(years, 'int4')})
`
: sql.fragment``
}
Expand Down
4 changes: 3 additions & 1 deletion frontend/src/components/forms/MultiSelect.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,9 @@ export function MultiSelect<T>({
const autocompleteRef = useRef<HTMLElement>();

function getOption(id: string | null) {
return options.find((option) => getOptionId?.(option) === id) ?? (id as T);
const option = options.find((option) => getOptionId?.(option) === id);
// If an option wasn't found, return the ID as numeric or string
return option ?? ((isNaN(Number(id)) ? id : Number(id)) as T);
}

function getLabel(optionId: string | null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ export const environmentalCodeReportFilterAtom = atom<EnvironmentCodeReportQuery
text: null,
plants: [],
reasonsForEnvironmentalInvestment: [],
year: null,
years: [],
});

export const textAtom = focusAtom(environmentalCodeReportFilterAtom, (o) => o.prop('text'));
Expand All @@ -18,7 +18,7 @@ export const reasonsForEnvironmentalInvestmentAtom = focusAtom(
environmentalCodeReportFilterAtom,
(o) => o.prop('reasonsForEnvironmentalInvestment')
);
export const yearAtom = focusAtom(environmentalCodeReportFilterAtom, (o) => o.prop('year'));
export const yearsAtom = focusAtom(environmentalCodeReportFilterAtom, (o) => o.prop('years'));

export function useDebouncedEnvironmentalCodeReportFilters() {
const filters = useAtomValue(environmentalCodeReportFilterAtom);
Expand Down
16 changes: 9 additions & 7 deletions frontend/src/views/SapReports/EnvironmentalCodeReportFilters.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import {
reasonsForEnvironmentalInvestmentAtom,
textAtom,
useDebouncedEnvironmentalCodeReportFilters,
yearAtom,
yearsAtom,
} from '@frontend/stores/sapReport/environmentalCodeReportFilters';

import { ReportSummary } from './ReportSummary';
Expand All @@ -36,7 +36,7 @@ export function EnvironmentalCodeReportFilters() {
const [reasonsForEnvironmentalInvestment, setReasonsForEnvironmentalInvestment] = useAtom(
reasonsForEnvironmentalInvestmentAtom
);
const [year, setYear] = useAtom(yearAtom);
const [years, setYears] = useAtom(yearsAtom);
const filters = useAtomValue(environmentalCodeReportFilterAtom);

const { data: allPlants, isLoading: allPlantsLoading } = trpc.sapReport.getPlants.useQuery();
Expand Down Expand Up @@ -111,12 +111,14 @@ export function EnvironmentalCodeReportFilters() {
<FormControl>
<FormLabel htmlFor="year">{tr('sapReports.environmentCodes.year')}</FormLabel>
<MultiSelect
id="year"
options={allYears?.map(String) ?? []}
id="years"
options={allYears ?? []}
loading={allYearsLoading}
value={String(year ?? '')}
onChange={(year) => setYear(year == null ? null : Number(year))}
multiple={false}
value={years}
onChange={(years) => {
setYears(years);
}}
multiple
/>
</FormControl>
</div>
Expand Down
2 changes: 1 addition & 1 deletion shared/src/schema/sapReport.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export const environmentCodeReportFilterSchema = z.object({
text: z.string().nullable(),
plants: z.array(z.string()),
reasonsForEnvironmentalInvestment: z.array(z.string()),
year: z.number().nullable(),
years: z.array(z.number()),
}),
});

Expand Down