fix(contentpicker): correctly clear old item when maxSelectable=1 and parent reloaded#1706
fix(contentpicker): correctly clear old item when maxSelectable=1 and parent reloaded#1706cgrdavies wants to merge 6 commits into
Conversation
|
Hi @cgrdavies, thanks for the pull request. Before we can merge it, we need you to sign our Contributor License Agreement. You can do so electronically here: http://opensource.box.com/cla Once you have signed, just add a comment to this pull request saying, "CLA signed". Thanks! |
|
CLA signed |
| const priorSelection = selected[priorSelectionIndex]; | ||
| const matchingItem = items.find(potentialMatch => potentialMatch.id === priorSelection.id); | ||
|
|
||
| if (matchingItem) delete matchingItem.selected; |
There was a problem hiding this comment.
if (matchingItem) {
...
}
|
BTW there is a possibility this issue applies to multi-selection. Trying to see when it broke, likely when we switched to paging. |
|
In order to properly fix this, and frankly make it less confusing, we may have to get rid of the reference mutations and keep selected isolated from currentCollection.items. I am trying a few things, will report back. |
|
Roughly something along these lines might be better https://github.com/box/box-ui-elements/pull/1709/files. We still can use a map to prevent multiple find()s so that its a O(1) lookup or we can just keep the find()s as in practice |
@priyajeet Thanks for taking a look at this! I agree that removing the reference mutations is a better approach, but I was originally hesitant to do so given the magnitude of the change that would be required. With your outline though I'm happy to update this PR. |
6b4c481 to
4c32a45
Compare
|
@priyajeet: should be all set for another look on this! |
priyajeet
left a comment
There was a problem hiding this comment.
lgtm mostly, some minor stylistic comments. Thanks for pulling this in.
| * @author Box | ||
| */ | ||
|
|
||
| export function containsItem(item: BoxItem) { |
There was a problem hiding this comment.
Can you move these exports to the bottom. It's our new convention. So
function foo() {}
function bar() {}
export { foo, bar };
There was a problem hiding this comment.
Also if you have time can you add a couple of unit tests for this file for these new functions
| return (selected: BoxItem) => selected.id === id && selected.type === type; | ||
| } | ||
|
|
||
| export function isSelected(item: BoxItem, selectedItems: Array<BoxItem>) { |
There was a problem hiding this comment.
For both of these functions probably should add a return type, so : boolean here and : (item: BoxItem) => boolean above
| const { name = '', selected = false } = rowData; | ||
| const { name = '' } = rowData; | ||
| const Component = isRadio ? RadioButton : Checkbox; | ||
| const isSelected = !!isRowSelected(rowData, selected); |
There was a problem hiding this comment.
!! is probably unnecessary since the function returns a boolean
| const { selected, type } = items[index]; | ||
| const isSelectable = isRowSelectable(selectableType, extensionsWhitelist, hasHitSelectionLimit, items[index]); | ||
| const { type } = items[index]; | ||
| const isSelected = !!isRowSelected(items[index], selected); |
There was a problem hiding this comment.
!! not needed here either
| index: number, | ||
| rowData: BoxItem, | ||
| }) => { | ||
| const isSelected = !!isRowSelected(rowData, selected); |
| const cacheKey = this.api.getAPI(type).getCacheKey(id); | ||
| // if shared link already exists, update the collection in state | ||
| this.updateItemInCollection(item); | ||
| if (item.selected && item !== selected[cacheKey]) { |
There was a problem hiding this comment.
Actually removing this part may cause a problem, since the item object in question has changed, it needs to be the one that is returned, so one will have to change the choose() function to return items from the updated collection and not whats inside selected.
Further simplification would be to make selected array just an array of IDs and Types, since thats what is really used everywhere to check if a row is selected or not. So that when choose button is pressed, then we build the returned array with latest item objects.
There was a problem hiding this comment.
Alternatively, updateItemInCollection() function can be changed to also update selected array with the updated item.
|
BTW, if you dont have time to make more changes, we can take over the PR... |
…le-select-multiple-selection-bug
f82b52d to
5a28b52
Compare
|
@priyajeet should be all set from a code perspective. I changed I do have a failing test though that seems unrelated, and I can't immediately figure out what's going on. Any chance someone could take a look at that? I don't have enough overall context to be able to fix it expediently. |
|
|
|
Hey @cgrdavies, sorry we've taken so long to merge this PR. I'm looking into the failing tests right now. Is it okay with you if I add commits to your PR? Also, could you sign the new CLA? Thanks! |
This change fixes #1705, which was preventing the prior selection from being properly cleared when the content picker was in single file selection mode and the folder enclosing the previously selected item had been reloaded before changing the selection.
Problem
Because the content picker relied on a mutating a reference to the previously selected item, when the items were reloaded from the server, that reference was no longer valid and the item within
currentCollectionwas not updated.Solution
Scan the
currentCollectionfor an item that matches the prior selection based on the item's id. If found, remove theselectedkeyword from that item directly.