-
Notifications
You must be signed in to change notification settings - Fork 79
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-4528: Hide created in error option for animals with completed tasks #3554
LF-4528: Hide created in error option for animals with completed tasks #3554
Conversation
* include animals and batches in GET tasks API response
* update stories' mock data
…eturn hasFinalizedTasks * send hideDeleteOption prop to RemoveAnimalsModal from AnimalInventory
for (let { id, tasks } of inventory) { | ||
if (selectedInventoryIdsSet.has(id)) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should we do this the other way round and iterate over the selected IDs and check if the corresponding entity exists in the inventory? The selected IDs would be a subset of the whole inventory so I think that should be faster than checking the whole inventory
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I initially considered iterating over selectedInventoryIds
instead of inventory
as suggested, and I came up with two approaches:
- Format
inventory
into a Map:- Convert
inventory
([{id, tasks}, {id, tasks}]
) to a map like{[id]: tasks, [id]: tasks}
- Then iterate over
selectedInventoryIds
to check if the corresponding tasks exist:
for (let id of selectedInventoryIds) { if (inventoryMap[id].length) { return true; } }
- Convert
- Directly iterate
selectedInventoryIds
without formatting:for (let selectedInventoryId of selectedInventoryIds) { const { tasks } = inventory.find(({ id }) => id === selectedInventoryId); if (tasks.length) { return true; } }
With the first approach, we’d be iterating over inventory
, so it might be more efficient to return the result early while iterating. The second approach felt a bit less efficient, though it should work fine for this feature.
Which option would you go with?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actually now that you described the two I think your implementation is probably the most performant one! I think option 2 presented here would be the simplest one / easier to read, but I'd keep this as is
…for_animals_with_completed_Tasks
Resolved conflicts and adjusted the |
…for_animals_with_completed_Tasks
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This logic looks great and also really efficient
The one issue I am running into is that the property tasks
on animals/batches is not populated if I add the task after I've last fetched my animal inventory, and then whether I complete the task or not, the 'Created in Error' option persists since it depends on the nested task list.
The easiest fix sounds like just a tag invalidation on animals when the task is created, but I wanted to check with you if this sounds like the right approach based on what you were thinking of the nested tasks array and its other uses.
For instance, I don't know how that compares to never nesting the property on the API level and assocating entirely on the frontend (not via selector... that has been nightmare!.. but just associating via logic here). This would avoid having to re-pull animals every time a Movement Task is created or a Custom Task is created, but it would be more complex logic compared to the re-fetch 🤔
Thank you Joyce! Could you elaborate on your idea of associating animals with tasks on the frontend? Were you thinking of creating a dedicated API to fetch these relationships? |
@SayakaOno I was thinking of an actual search on the animals/animal_batches property of the completed tasks + abandoned tasks! I wasn't thinking of a dedicated API, would that be more efficient? 😅 I guess my logic was that the tasks are already up to date, and the animals are already up to date, so it shouldn't be necessary to request anything more from the API. Animals is also going to be a fairly large request (potentially). But I don't necessarily mind re-fetching because I think we have been in favour of making the backend do complicated computations and with the size of tasks, this probably qualifies! I guess the two in-between options would be a dedicated API as you said, or maybe a selector...? Does RTK (regular, not query) have some properties that would make a "tasksByAnimalId" selector more efficient? I'm fine with the tag invalidation / re-fetch for now, though. Maybe we could table those other approaches as possibilities if it seems we want to optimize later? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry it was sort of review scope creep on my part to bring up the refetching here, it can definitely be a different ticket since the logic for the Created in Error good! 🙏
Description
RemoveAnimalsModal
to accepthideDeleteOption
prop and hide the "Created in error" optionuseAnimalInventory
hook to include tasksuseAnimalOrBatchRemoval
hook to check if selected animals/batches have abandoned or completed taskshideDeleteOption
prop toRemoveAnimalsModal
fromAnimalInventory
to hide the delete optionJira link: https://lite-farm.atlassian.net/browse/LF-4528
Type of change
How Has This Been Tested?
Checklist: