Skip to content

Commit

Permalink
Fix child item still visible after parent item created in library view.
Browse files Browse the repository at this point in the history
Fix #600
  • Loading branch information
tnajdek committed Feb 20, 2025
1 parent 1053ee9 commit f69707b
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 14 deletions.
2 changes: 1 addition & 1 deletion src/js/actions/recognize.js
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ const undoRetrieveMetadata = (itemKey, libraryKey) => {
}

const collections = item.collections;
await dispatch(updateItem(originalItemKey, { parentItem: null, collections }, libraryKey));
await dispatch(updateItem(originalItemKey, { parentItem: false, collections }, libraryKey));
await dispatch(deleteItem(item));
dispatch({
type: COMPLETE_UNRECOGNIZE_DOCUMENT,
Expand Down
61 changes: 51 additions & 10 deletions src/js/reducers/libraries/items-top.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,21 @@
import { indexByKey } from '../../utils';
import { injectExtraItemKeys, filterItemKeys, populateItemKeys, sortItemKeysOrClear,
updateFetchingState } from '../../common/reducers';
import {
injectExtraItemKeys, filterItemKeys, populateItemKeys, sortItemKeysOrClear,
updateFetchingState
} from '../../common/reducers';

import {
DROP_TOP_ITEMS, ERROR_TOP_ITEMS, RECEIVE_CREATE_ITEM, RECEIVE_CREATE_ITEMS, RECEIVE_DELETE_ITEM,
RECEIVE_DELETE_ITEMS, RECEIVE_DELETED_CONTENT, RECEIVE_FETCH_ITEMS, RECEIVE_MOVE_ITEMS_TRASH,
RECEIVE_RECOVER_ITEMS_TRASH, RECEIVE_TOP_ITEMS, REQUEST_TOP_ITEMS, SORT_ITEMS,
RECEIVE_RECOVER_ITEMS_TRASH, RECEIVE_TOP_ITEMS, RECEIVE_UPDATE_ITEM, RECEIVE_UPDATE_MULTIPLE_ITEMS,
REQUEST_TOP_ITEMS, SORT_ITEMS
} from '../../constants/actions.js';

// TODO: This currenlty only handles RECEIVE_FETCH_ITEMS, but logic for
// RECEIVE_UPDATE_ITEM and RECEIVE_UPDATE_MULTIPLE_ITEMS is very similar, so it
// should be refactored to use a common function
const detectChangesInTop = (mappings, state, action, items) => {
if(!('keys' in state)) {
if (!('keys' in state)) {
return state;
}

Expand All @@ -18,29 +24,29 @@ const detectChangesInTop = (mappings, state, action, items) => {
const keysToRemove = [];

action.items.forEach(item => {
if(!item.deleted && !item.parentItem && !newState.keys.includes(item.key)) {
if (!item.deleted && !item.parentItem && !newState.keys.includes(item.key)) {
keysToInject.push(item.key);
} else if((item.deleted || item.parentItem) && newState.keys.includes(item.key)) {
} else if ((item.deleted || item.parentItem) && newState.keys.includes(item.key)) {
keysToRemove.push(item.key);
}
});

if(keysToInject.length > 0) {
if (keysToInject.length > 0) {
const allItems = { ...items, ...indexByKey(action.items) };
newState = injectExtraItemKeys(mappings, newState, keysToInject, allItems);
}

if(keysToRemove.length > 0) {
if (keysToRemove.length > 0) {
newState = filterItemKeys(newState, keysToRemove)
}

return newState;
}

const itemsTop = (state = {}, action, { items, meta }) => {
switch(action.type) {
switch (action.type) {
case RECEIVE_CREATE_ITEM:
if(!action.item.parentItem) {
if (!action.item.parentItem) {
return injectExtraItemKeys(
meta.mappings,
state,
Expand Down Expand Up @@ -91,6 +97,41 @@ const itemsTop = (state = {}, action, { items, meta }) => {
return detectChangesInTop(meta.mappings, state, action, items);
case SORT_ITEMS:
return sortItemKeysOrClear(meta.mappings, state, action.items, action.sortBy, action.sortDirection);
case RECEIVE_UPDATE_ITEM: {
const item = action.item;
if (!('keys' in state)) {
return state;
}
if (!('deleted' in action.patch || 'parentItem' in action.patch)) {
return state;
}
if (!items[action.item.key]) {
return state;
}
if (!item.deleted && !item.parentItem && !state.keys.includes(item.key)) {
return injectExtraItemKeys(meta.mappings, state, [item.key], items)
} else if ((item.deleted || item.parentItem) && state.keys.includes(item.key)) {
return filterItemKeys(state, [item.key])
}
return state;
}
case RECEIVE_UPDATE_MULTIPLE_ITEMS: {
if (!('keys' in state)) {
return state;
}
const affectedItemsPatch = action.multiPatch.filter(patch => patch.key in items && ('deleted' in patch || 'parentItem' in patch))
const patchedItems = affectedItemsPatch.map(patch => ({ ...items[patch.key], ...patch }))
const keysToInject = patchedItems.filter(i => !i.parentItem && !i.deleted).map(i => i.key).filter(k => !state.keys.includes(k));
const keysToRemove = patchedItems.filter(i => i.parentItem || i.deleted).map(i => i.key).filter(k => state.keys.includes(k));
let newState = { ...state };
if (keysToInject.length > 0) {
newState = injectExtraItemKeys(meta.mappings, newState, keysToInject, items);
}
if (keysToRemove.length > 0) {
newState = filterItemKeys(newState, keysToRemove)
}
return newState;
}
default:
return state;
}
Expand Down
6 changes: 3 additions & 3 deletions test/recognize.test.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ describe('Metadata Retrieval', () => {
}),
http.get('https://api.zotero.org/users/1/collections/CSB4KZUU/items/top/tags', () => {
return HttpResponse.json([], { headers: { 'Total-Results': '0' } });
}),
}),
];
const server = setupServer(...handlers)
applyAdditionalJestTweaks();
Expand Down Expand Up @@ -114,7 +114,7 @@ describe('Metadata Retrieval', () => {
expect(item.parentItem).toBe('S8CIV6VJ');
expect(item.collections).toEqual([]);
} else {
expect(item.parentItem).toBeNull();
expect(item.parentItem).toBe(false);
expect(item.collections).toEqual(['CSB4KZUU']);
}
hasPatchedAttachmentItem = true;
Expand All @@ -135,7 +135,7 @@ describe('Metadata Retrieval', () => {
return { getRecognizerData: mockedGetRecognizerData };
});

expect(screen.getByRole('row', { name: 'attention-is-all-you-need.pdf' }) ).toHaveAttribute('aria-selected', 'true');
expect(screen.getByRole('row', { name: 'attention-is-all-you-need.pdf' })).toHaveAttribute('aria-selected', 'true');
const recognizeBtn = screen.getByRole('button',
{ name: 'Retrieve Metadata' }
);
Expand Down

0 comments on commit f69707b

Please sign in to comment.