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

Fix: Prevent NaN error in Campaign Overview Stat Widget #7698

Open
wants to merge 3 commits into
base: epic/campaigns
Choose a base branch
from

Conversation

kjohnson
Copy link
Member

@kjohnson kjohnson commented Jan 27, 2025

Resolves GIVE-2031

Description

This PR updates the <StatWidget> component to prevent a NaN error while fetching data.

Additionally, a non-breaking space is used to prevent content shifting.

Visuals

Peek 2025-01-27 15-39-2

Testing Instructions

  • Hard-reload the page (with cache disabled via browser tools)
  • Toggle between tabs in the Overview screen

Pre-review Checklist

  • Acceptance criteria satisfied and marked in related issue
  • Relevant @unreleased tags included in DocBlocks
  • Includes unit tests
  • Reviewed by the designer (if follows a design)
  • Self Review of code and UX completed

@kjohnson kjohnson marked this pull request as ready for review January 27, 2025 21:00
@kjohnson kjohnson requested a review from glaubersilva January 27, 2025 21:00
@@ -93,7 +93,7 @@ const StatWidget = ({label, values, description, formatter = null}) => {
</header>
<div className={styles.statWidgetAmount}>
<DisplayText>
{formatter?.format(values[0]) ?? values[0]}
{!isNaN(values[0]) ? formatter?.format(values[0]) ?? values[0] : (<span>&nbsp;</span>)}
Copy link
Contributor

Choose a reason for hiding this comment

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

@kjohnson can you help us understand what could cause one of these values to be considered NaN? Is it an issue with data still loading or is it possible for a value to resolve as a non number?

Copy link
Member Author

Choose a reason for hiding this comment

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

The isssue here is that the data is loading. The endpoint would only return a numeric value.

Copy link
Contributor

@jonwaldstein jonwaldstein Jan 28, 2025

Choose a reason for hiding this comment

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

Okay, If that's the case then I feel it would be more sensible for the stat widget to have a loading state (or conditionally rendered all together). You could then add a css class for the loading state to be styled.

Without the loading state we are making the assumption the value is always resolved when passed to the component, which is not true here - raising questions of why it could be a non number.

Copy link
Member Author

Choose a reason for hiding this comment

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

This passes a non-breaking space as a placeholder if the value is not provided, which is during loading. While not a proper loading state, this does cut down on the amount of visual change while loading. I think it would be more distracting for a row of widgets to flash loading spinners or something.

Note that I did update the check from a NaN check to an undefined check, see d3077e7.

Copy link
Contributor

@jonwaldstein jonwaldstein Jan 29, 2025

Choose a reason for hiding this comment

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

Regardless of the design, what is the reason for not having a loading state in the logic?

Loading states tell us when our data is resolved and ready to be used. They are useful in the application's logic for conditionally rendering data. In this scenario we have data that is not resolved yet but we are attempting to render it regardless. There's lots of ways we can go about this visually but to me the important part is we are being intentional about our data loading and rendering.

Copy link
Contributor

@glaubersilva glaubersilva Jan 29, 2025

Choose a reason for hiding this comment

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

About the design for the "loading state", maybe we can use something similar to the give-skeleton class.

Here are a couple of links for reference:

https://github.com/impress-org/givewp/blob/develop/src/DonationForms/AsyncData/AsyncDataHelpers.php#L34

https://github.com/impress-org/givewp/blob/develop/src/DonationForms/AsyncData/resources/loadAsyncData.scss

And here is the screenshot with the class in action:

image

Copy link
Contributor

Choose a reason for hiding this comment

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

Or even better, we can use a package like this one: https://www.npmjs.com/package/react-loading-skeleton

Copy link
Member Author

Choose a reason for hiding this comment

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

I really don't think this needs a special loading state. It is a simple widget which shows a number returned by the server. We aren't running exspensive queries and we aren't loading tables of data. There isn't a fancy display to be stubbed by a skeleton.

This PR prevents a flashed NaN and additionally prevents a content shift when the data is loaded.

@glaubersilva glaubersilva removed their request for review January 31, 2025 16:54
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