-
Notifications
You must be signed in to change notification settings - Fork 39
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
187651946 v3 DI Get Multidata Plugin Running (#1277)
* Handle get collectionList requests. * Support adding attributes to collections when handling create attribute API requests. * Handle update attributeLocation requests. * Handle create collection requests.
- Loading branch information
1 parent
0e63cf2
commit 294daf1
Showing
16 changed files
with
440 additions
and
83 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
65 changes: 65 additions & 0 deletions
65
v3/src/data-interactive/handlers/attribute-location-handler.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
import { ICollectionPropsModel } from "../../models/data/collection" | ||
import { diAttributeLocationHandler } from "./attribute-location-handler" | ||
import { setupTestDataset } from "./handler-test-utils" | ||
|
||
describe("DataInteractive AttributeLocationHandler", () => { | ||
const handler = diAttributeLocationHandler | ||
|
||
it("update works as expected", () => { | ||
const { dataset, c1, c2, a1, a2 } = setupTestDataset() | ||
const a4 = dataset.addAttribute({ name: "a4" }, { collection: c1.id }) | ||
const a5 = dataset.addAttribute({ name: "a5" }, { collection: c2.id }) | ||
const a6 = dataset.addAttribute({ name: "a6" }) | ||
const dataContext = dataset | ||
const resources = { attributeLocation: a1, dataContext } | ||
|
||
const collectionAttributes = (collection: ICollectionPropsModel) => | ||
dataset.getGroupedCollection(collection.id)?.attributes | ||
|
||
expect(handler.update?.({})?.success).toBe(false) | ||
// Missing dataContext | ||
expect(handler.update?.( | ||
{ attributeLocation: a2 }, { collection: c1.name, position: 0 } | ||
).success).toBe(false) | ||
// Missing attributeLocation | ||
expect(handler.update?.({ dataContext }, { collection: c1.name, position: 0 }).success).toBe(false) | ||
// Parent of leftmost collection | ||
expect(handler.update?.( | ||
resources, { collection: "parent", position: 0 } | ||
).success).toBe(false) | ||
|
||
// Move attribute within the ungrouped collection | ||
// Indexes snap to the end of the array | ||
expect(dataset.ungroupedAttributes[1].id).toBe(a6.id) | ||
expect(handler.update?.({ attributeLocation: a6, dataContext }, { position: -1 }).success).toBe(true) | ||
expect(dataset.ungroupedAttributes[0].id).toBe(a6.id) | ||
expect(handler.update?.({ attributeLocation: a6, dataContext }, { position: 10 }).success).toBe(true) | ||
expect(dataset.ungroupedAttributes[1].id).toBe(a6.id) | ||
|
||
// Move attribute within a grouped collection | ||
// If not specified, move the attribute to the far right | ||
expect(collectionAttributes(c2)?.[1]?.id).toBe(a5.id) | ||
expect(handler.update?.({ attributeLocation: a5, dataContext }, { position: 0 }).success).toBe(true) | ||
expect(collectionAttributes(c2)?.[0]?.id).toBe(a5.id) | ||
expect(handler.update?.({ attributeLocation: a5, dataContext }).success).toBe(true) | ||
expect(collectionAttributes(c2)?.[1]?.id).toBe(a5.id) | ||
|
||
// Move attribute from ungrouped collection to middle of grouped collection | ||
expect(collectionAttributes(c1)?.[1]?.id).toBe(a4.id) | ||
expect(collectionAttributes(c1)?.length).toBe(2) | ||
expect(handler.update?.({ attributeLocation: a6, dataContext }, { collection: c1.name, position: 1 }).success) | ||
.toBe(true) | ||
expect(collectionAttributes(c1)?.length).toBe(3) | ||
expect(dataset.ungroupedAttributes.length).toBe(1) | ||
expect(collectionAttributes(c1)?.[1]?.id).toBe(a6.id) | ||
expect(collectionAttributes(c1)?.[2]?.id).toBe(a4.id) | ||
|
||
// Move attribute from grouped collection to middle of its parent collection | ||
// Round the position | ||
expect(handler.update?.({ attributeLocation: a5, dataContext }, { collection: "parent", position: 1.2 }).success) | ||
.toBe(true) | ||
expect(collectionAttributes(c1)?.length).toBe(4) | ||
expect(collectionAttributes(c2)?.length).toBe(1) | ||
expect(collectionAttributes(c1)?.[1]?.id).toBe(a5.id) | ||
}) | ||
}) |
40 changes: 38 additions & 2 deletions
40
v3/src/data-interactive/handlers/attribute-location-handler.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,44 @@ | ||
import { moveAttribute } from "../../models/data/data-set-utils" | ||
import { registerDIHandler } from "../data-interactive-handler" | ||
import { DIHandler, diNotImplementedYet } from "../data-interactive-types" | ||
import { DIAttributeLocationValues, DIHandler, DIResources, DIValues } from "../data-interactive-types" | ||
import { getCollection } from "../data-interactive-utils" | ||
import { attributeNotFoundResult, collectionNotFoundResult, dataContextNotFoundResult } from "./di-results" | ||
|
||
export const diAttributeLocationHandler: DIHandler = { | ||
update: diNotImplementedYet | ||
update(resources: DIResources, values?: DIValues) { | ||
const { attributeLocation, dataContext } = resources | ||
if (!dataContext) return dataContextNotFoundResult | ||
if (!attributeLocation) return attributeNotFoundResult | ||
const sourceCollection = dataContext.getCollectionForAttribute(attributeLocation.id) | ||
|
||
const { collection, position } = (values ?? {}) as DIAttributeLocationValues | ||
const targetCollection = collection === "parent" | ||
? dataContext.getParentCollectionGroup(sourceCollection?.id)?.collection | ||
: getCollection(dataContext, collection ? `${collection}` : undefined) ?? sourceCollection | ||
if (!targetCollection) return collectionNotFoundResult | ||
|
||
const targetAttrs = | ||
dataContext.getGroupedCollection(targetCollection.id)?.attributes ?? dataContext.ungroupedAttributes | ||
const numPos = Number(position) | ||
|
||
// If the position isn't specified or isn't a number, make the attribute the right-most | ||
// Otherwise, round the position to an integer | ||
const pos = isNaN(numPos) ? targetAttrs.length : Math.round(numPos) | ||
|
||
// Snap the position to the left or right if it is negative or very large | ||
const _position = pos < 0 ? 0 : pos > targetAttrs.length ? targetAttrs.length : pos | ||
const afterAttrId = targetAttrs[_position - 1]?.id | ||
|
||
moveAttribute({ | ||
afterAttrId, | ||
attrId: attributeLocation.id, | ||
dataset: dataContext, | ||
sourceCollection, | ||
targetCollection | ||
}) | ||
|
||
return { success: true } | ||
} | ||
} | ||
|
||
registerDIHandler("attributeLocation", diAttributeLocationHandler) |
Oops, something went wrong.