Skip to content

Commit

Permalink
fix: kids-first#2556 bug
Browse files Browse the repository at this point in the history
  • Loading branch information
adipaul1981 committed Jul 2, 2020
1 parent 49bccaf commit 36404bc
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 21 deletions.
19 changes: 14 additions & 5 deletions src/components/OntologyBrowser/Test/store.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -151,12 +151,21 @@ describe('Phenotype Store', () => {
});

it('/removeSameTerms should prohibit selecting multiple times the same term', () => {
const selecteKeys = ['ONE-two-three (123)'];
const targetKeys = ['two-ONE-three (123)', 'five-six-seven (567)', 'one-zero-zero (100)'];
let selecteKeys = ['ONE-two-three (123)'];
let targetKeys = ['two-ONE-three (123)', 'five-six-seven (567)', 'one-zero-zero (100)'];

const res = removeSameTerms(selecteKeys, targetKeys);
expect(res.sort()).toEqual(
['ONE-two-three (123)', 'five-six-seven (567)', 'one-zero-zero (100)'].sort(),
//not expected to change
const res_one = removeSameTerms(selecteKeys, targetKeys);
expect(res_one.sort()).toEqual(
['two-ONE-three (123)', 'five-six-seven (567)', 'one-zero-zero (100)'].sort(),
);
selecteKeys = ['ONE-two-three (123)', 'two-ONE-three (123)'];
targetKeys = ['five-six-seven (567)', 'one-zero-zero (100)'];

//expected to add last item change
const res_two = removeSameTerms(selecteKeys, targetKeys);
expect(res_two.sort()).toEqual(
['two-ONE-three (123)', 'five-six-seven (567)', 'one-zero-zero (100)'].sort(),
);
});

Expand Down
2 changes: 1 addition & 1 deletion src/components/OntologyBrowser/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,7 @@ class OntologyModal extends React.Component<ModalProps, ModalState> {
return <Spinner className={'spinner'} size={'large'} />;
}
if (direction === 'left' && treeSource) {
const checkedKeys = [...selectedKeys, ...removeSameTerms(selectedKeys, targetKeys)];
const checkedKeys = [...removeSameTerms(selectedKeys, targetKeys)];
return (
<SelectionTree
dataSource={treeSource || []}
Expand Down
20 changes: 5 additions & 15 deletions src/components/OntologyBrowser/store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,24 +26,14 @@ const dotToUnderscore = (str: string) => str.replace('.', '__');

const termRegex = new RegExp('[^-]+$');

//pattern of term with escaped parentheses
const termPattern = (term: string) => new RegExp(`.*${term?.replace(/(?=[()])/g, '\\')}$`);

export const removeSameTerms = (selectedKeys: string[], targetKeys: string[]) => {
let updatedTargetKeys = targetKeys;

selectedKeys.forEach((t) => {
const match = t.match(termRegex);
if (match) {
const term = match.pop();
let allSelectedAndChecked = {};

if (term) {
const pattern = termPattern(term);
updatedTargetKeys = updatedTargetKeys.filter((t) => !pattern.test(t));
}
}
selectedKeys.concat(targetKeys).forEach((t) => {
allSelectedAndChecked = { ...allSelectedAndChecked, [`${t.match(termRegex)}`]: t };
});
return [...updatedTargetKeys, ...selectedKeys];

return [...Object.values(allSelectedAndChecked)];
};

export const selectSameTerms = (selectedKeys: string[], tree: TreeNode[] | undefined) => {
Expand Down

0 comments on commit 36404bc

Please sign in to comment.