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

Add total row in report tables #179

Merged
merged 13 commits into from
Oct 22, 2024
Merged

Add total row in report tables #179

merged 13 commits into from
Oct 22, 2024

Conversation

K-Markopoulos
Copy link
Collaborator

@K-Markopoulos K-Markopoulos commented Oct 17, 2024

Issue #178

Add total row at the top of the table with the sum of the values.

  • Calculate the aggregated values and ad extra row to the data
  • Adjust sorting in order to always stay on top

Copy link

github-actions bot commented Oct 17, 2024

Build succeeded and deployed at https://wfp-dmp-179.surge.sh
(hash 880279d deployed at 2024-10-22T13:46:22)

@K-Markopoulos K-Markopoulos marked this pull request as ready for review October 18, 2024 13:33
@@ -85,7 +89,7 @@ const getLocationColumnSetup = (
<FormattedMessage id={`forms_table.headers.${params.field}`} />
</Typography>
),
valueFormatter: value =>
valueGetter: value =>
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you explicit why this change is needed?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The return value of valueGetter is used for sorting and it's being passed in the valueFormatter or renderCell. With this change:

  1. We can use params.value in the renderCell without the need to duplicate the logic to construct the value.
  2. The sorting is based on the displayed value (the actual name of the location) and not on the code ("01-002") and behaves as expected (unless we need a custom order based on the code and not alphanumerical order).

From docs:

Note, that the value returned by valueFormatter is only used for rendering purposes. Filtering and sorting are based on the raw value (row[field]) or the value returned by valueGetter.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can use the same approach here to calculate formattedList once in the valueGetter

valueFormatter: value => {
const villageList = value as string[] | undefined;
const formattedList = villageList
?.map(village =>
intl.formatMessage({ id: `${field}.${village}` }),
)
.sort((a, b) => a.localeCompare(b, intl.locale)) // Sort alphabetically
.join(', ');
return formattedList;
},
renderCell: (params: GridRenderCellParams) => {
const villageList = params.value as string[] | undefined;
const formattedList = villageList
?.map(village =>
intl.formatMessage({ id: `${field}.${village}` }),
)
.sort((a, b) => a.localeCompare(b, intl.locale)) // Sort alphabetically
.join(', ');

I skipped this as I thought it was out of scope and required more testing, but we can make that change since we are working on this area

Copy link
Collaborator

@ericboucher ericboucher Oct 18, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, worth exploring. Two things come to mind though:

  1. Ideally, for sorting, we might actually want to order by number of villages instead of the alphabetical order of the first village (which doesn't really mean anything). Noting that sorting doesn't really work for the village column at the moment, esp. in "Commune" reports.

  2. We still need to display the tooltip

@ericboucher
Copy link
Collaborator

@K-Markopoulos let's use the translation files so that we can get a translated version in Khmer: សរុប

@wadhwamatic
Copy link
Member

Thanks for this @K-Markopoulos and @ericboucher. Is there a reason there's a discrepancy in the total when you're showing Province vs Commune level views? I expected the values to be the same in the total row, but they are not.

Province level:
Screenshot 2024-10-21 at 14 35 42

Commune level:
Screenshot 2024-10-21 at 14 35 49

@K-Markopoulos
Copy link
Collaborator Author

@wadhwamatic thanks for the catch! Indeed in commune level some rows are already aggregated and our logic would not take that into account. Hopefully the last commit will address this issue, the total numbers are now the same between these views.

@wadhwamatic
Copy link
Member

Looks good now, thanks @K-Markopoulos and @ericboucher!

@ericboucher ericboucher merged commit 5d43761 into new-ui-2024 Oct 22, 2024
3 checks passed
@ericboucher ericboucher deleted the add-total-row branch October 22, 2024 18:02
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants