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

LF-4410: Add limit on creating animals and batches and tests #3542

Open
wants to merge 3 commits into
base: integration
Choose a base branch
from

Conversation

Tbrid
Copy link
Collaborator

@Tbrid Tbrid commented Nov 20, 2024

Description

Put 1000 limit on creating animals and animal batches. Added tests on api package. Added max limit on UI count input.

Jira link: https://lite-farm.atlassian.net/browse/LF-4410

Type of change

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • This change requires a documentation update

How Has This Been Tested?

Please describe the tests that you ran to verify your changes. Provide instructions so we can reproduce. Please also list any relevant details for your test configuration

  • Passes test case
  • UI components visually reviewed on desktop view
  • UI components visually reviewed on mobile view
  • Other (please explain)

Checklist:

  • I have commented my code, particularly in hard-to-understand areas
  • My changes generate no new warnings
  • I have added tests that prove my fix is effective or that my feature works
  • New and existing unit tests pass locally with my changes
  • The precommit and linting ran successfully
  • I have added or updated language tags for text that's part of the UI
  • I have added "MISSING" for all new language tags to languages I don't speak
  • I have added the GNU General Public License to all new files

@Tbrid Tbrid requested review from a team as code owners November 20, 2024 23:49
@Tbrid Tbrid requested review from antsgar and SayakaOno and removed request for a team November 20, 2024 23:49
@@ -112,6 +114,7 @@ export default function AddAnimalsFormCard({
<NumberInput
name={`${namePrefix}${BasicsFields.COUNT}`}
control={control}
max={ANIMAL_COUNT_LIMIT}
Copy link
Collaborator

Choose a reason for hiding this comment

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

Thank you for working on this!

Screenshot 2024-11-20 at 3 55 23 PM

Sorry, we should've explained this first... When the checkbox is selected and the count is 1000, 1000 animals will be created. If the checkbox is not selected, a batch with 1000 animals (one record in the DB) will be created instead. The latter is a valid case, so it would be great if you could add a condition to set a maximum limit when the checkbox is selected 🙏

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Hi, Sayaka. So I put a hook to deal with this, but then I found it wouldn't change the displayed value in the input when I only use setValue, (i.e. if number is 2000 and user clicks the checkbox, the value will be set to the limit, but still displayed as 2000) so I put a "value" prop for the NumberedInput, let me know what you think about it.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Sorry for the late reply! Sending value to NumberInput seems good, but it looks like value will be automatically passed through without modifying NumberInput, since ...props is added to InputBase. Could you double check?

Would it work if we move the logic from the useEffect to the checkbox's onChange instead?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I found that it doesn't propagate to the InputBase without specifying the value as a prop.

Copy link
Collaborator

@SayakaOno SayakaOno Nov 28, 2024

Choose a reason for hiding this comment

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

Oh right, inputProps's value overrides it.

@navDhammu The number in the NumberInput doesn't update when the value is changed via setValue. Do you think we should update the logic in useNumberInput to handle this? (I believe that's the right thing to do!)

Copy link
Collaborator

Choose a reason for hiding this comment

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

@SayakaOno The reason it doesn't update with setValue is because NumberInput manages its own state, so when changing value with setValue it doesn't update NumberInput's internal state, which is why you still see the previous number.

I don't think its a good idea to update the logic inside the useNumberInput to handle this because then you would probably have to add a useEffect to update the state based on changes to some prop, and this isn't best practices for react.

Better ways of handling this in my opinion:

  1. Lift the state up to where its needed. This would involve refactoring out the useNumberInput hook from NumberInput and using it in each component that uses NumberInput.
  2. Use a key to rerender NumberInput.

Also, sending value to NumberInput is not a good idea because it overrides the value given by useNumberInput which is a formatted string based on the locale. So overriding that breaks the localization feature.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Thank you for your input, I'll discuss the solution with the team!

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

@navDhammu Thanks for explaining. @SayakaOno I think the 2nd way of using a key is the easiest.

Copy link
Collaborator

Choose a reason for hiding this comment

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

I agree, but I'm hesitant to go with that approach because we may encounter the same issue in the future. Needing to add a key each time doesn't seem like a sustainable solution...

I'll bring this up in tech daily early next week (feel free to join if you'd like) and get back to you!

@Tbrid Tbrid changed the title LF-4415: Add limit on creating animals and batches and tests LF-4410: Add limit on creating animals and batches and tests Nov 21, 2024
@Tbrid Tbrid requested a review from SayakaOno November 25, 2024 21:01
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