Skip to content

Commit

Permalink
Refactor addNewCase to use selectedCases.
Browse files Browse the repository at this point in the history
  • Loading branch information
lublagg committed Oct 2, 2024
1 parent a5380e0 commit 2750d78
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 7 deletions.
32 changes: 26 additions & 6 deletions v3/src/components/case-card/case-card-model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -128,15 +128,35 @@ export const CaseCardModel = TileContentModel
self.attributeColumnWidths.delete(collectionId)
}
},
addNewCase(cases: IGroupedCase[], collection: ICollectionModel, displayedCaseId: string) {
addNewCase(collection: ICollectionModel) {

Check warning on line 131 in v3/src/components/case-card/case-card-model.ts

View check run for this annotation

Codecov / codecov/patch

v3/src/components/case-card/case-card-model.ts#L131

Added line #L131 was not covered by tests
const newCase: ICaseCreation = {}
const selectedCases = self.data?.selection

collection.allParentDataAttrs.forEach(attr => {
if (attr?.id) {
const value = self.data?.getValue(displayedCaseId, attr.id)
newCase[attr.id] = value
function findCommonCases(lineages: (readonly string[])[]) {

Check warning on line 135 in v3/src/components/case-card/case-card-model.ts

View check run for this annotation

Codecov / codecov/patch

v3/src/components/case-card/case-card-model.ts#L135

Added line #L135 was not covered by tests
if (lineages.length === 0) return [];
let commonValues = lineages[0];
for (let i = 1; i < lineages.length; i++) {
commonValues = commonValues.filter(value => lineages[i].includes(value));

Check warning on line 139 in v3/src/components/case-card/case-card-model.ts

View check run for this annotation

Codecov / codecov/patch

v3/src/components/case-card/case-card-model.ts#L137-L139

Added lines #L137 - L139 were not covered by tests
if (commonValues.length === 0) {
return [];

Check warning on line 141 in v3/src/components/case-card/case-card-model.ts

View check run for this annotation

Codecov / codecov/patch

v3/src/components/case-card/case-card-model.ts#L141

Added line #L141 was not covered by tests
}
}
return commonValues;

Check warning on line 144 in v3/src/components/case-card/case-card-model.ts

View check run for this annotation

Codecov / codecov/patch

v3/src/components/case-card/case-card-model.ts#L144

Added line #L144 was not covered by tests
}

if (selectedCases) {
const caseLineages = Array.from(selectedCases).map(caseId => self.caseLineage(caseId) || [])
const commonCaseIds = findCommonCases(caseLineages)

Check warning on line 149 in v3/src/components/case-card/case-card-model.ts

View check run for this annotation

Codecov / codecov/patch

v3/src/components/case-card/case-card-model.ts#L149

Added line #L149 was not covered by tests
if (commonCaseIds.length === 0) {
const nearestCommonParentCaseId = commonCaseIds[commonCaseIds.length - 1]
const parentAttrs = collection.allParentAttrs
parentAttrs.forEach(attr => {
const attrId = attr.id

Check warning on line 154 in v3/src/components/case-card/case-card-model.ts

View check run for this annotation

Codecov / codecov/patch

v3/src/components/case-card/case-card-model.ts#L151-L154

Added lines #L151 - L154 were not covered by tests
const parentValue = nearestCommonParentCaseId && self.data?.getValue(nearestCommonParentCaseId, attrId)
newCase[attrId] = parentValue

Check warning on line 156 in v3/src/components/case-card/case-card-model.ts

View check run for this annotation

Codecov / codecov/patch

v3/src/components/case-card/case-card-model.ts#L156

Added line #L156 was not covered by tests
})
}
})
}

const [newCaseId] = self.data?.addCases([newCase]) ?? []

Expand Down
2 changes: 1 addition & 1 deletion v3/src/components/case-card/case-view.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ export const CaseView = observer(function CaseView(props: ICaseViewProps) {
if (collection) {
let newCaseId: string | undefined
data?.applyModelChange(() => {
const newItemId = cardModel?.addNewCase(cases, collection, displayedCaseId)
const newItemId = cardModel?.addNewCase(collection)
newCaseId = newItemId && data?.getItemCaseIds(newItemId)[level]
newCaseId && onSelectCases([newCaseId])
}, {
Expand Down

0 comments on commit 2750d78

Please sign in to comment.