Skip to content

Commit

Permalink
store DCEs as dragData, and check for element_type within onDrop()
Browse files Browse the repository at this point in the history
  • Loading branch information
ahmedhamidawan committed Aug 16, 2024
1 parent 1365a1d commit 793fe46
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 15 deletions.
2 changes: 1 addition & 1 deletion client/src/api/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,7 @@ export function isHistoryItem(item: object): item is HistoryItemSummary {
return item && "history_content_type" in item;
}

export function isCollectionItem(item: object): item is DCESummary {
export function isDCE(item: object): item is DCESummary {
return item && "element_type" in item;
}

Expand Down
40 changes: 28 additions & 12 deletions client/src/components/History/CurrentHistory/HistoryPanel.vue
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@ import { storeToRefs } from "pinia";
import { computed, onMounted, type Ref, ref, set as VueSet, unref, watch } from "vue";
import {
type HDAObject,
type DCESummary,
type HistoryItemSummary,
type HistorySummaryExtended,
isDatasetElement,
isDCE,
isHistoryItem,
userOwnsHistory,
} from "@/api";
Expand Down Expand Up @@ -61,7 +62,7 @@ interface Props {
isMultiViewItem?: boolean;
}
type DraggableHistoryItem = HistoryItemSummary | HDAObject;
type DraggableHistoryItem = HistoryItemSummary | (DCESummary & { history_id: string });
type ContentItemRef = Record<string, Ref<InstanceType<typeof ContentItem> | null>>;
Expand Down Expand Up @@ -237,10 +238,8 @@ function getDragData() {
const dragItems = eventStore.getDragItems();
// Filter out any non-history items
const historyItems = dragItems?.map((item: any) => {
if (isHistoryItem(item)) {
if (isHistoryItem(item) || isDCE(item)) {
return item;
} else if (isDatasetElement(item)) {
return item.object;
}
}) as DraggableHistoryItem[];
const historyId = historyItems?.[0]?.history_id;
Expand Down Expand Up @@ -397,15 +396,32 @@ async function onDrop() {
// iterate over the data array and copy each item to the current history
for (const item of data) {
let dataSource: HistoryContentsArgs["source"];
const type = item.history_content_type as "dataset" | "dataset_collection" | undefined;
if (type) {
// it's a `HistoryItemSummary`
dataSource = type === "dataset" ? "hda" : "hdca";
} else {
// it's a `HDAObject` from a collection
let type: HistoryContentsArgs["type"];
let id: string;
if (isHistoryItem(item)) {
dataSource = item.history_content_type === "dataset" ? "hda" : "hdca";
type = item.history_content_type;
id = item.id;
}
// TEMPORARY: fix this when DCEs are handled correctly, unify like commented out code below
else if (isDatasetElement(item) && item.object) {
dataSource = "hda";
type = "dataset";
id = item.object.id;
}
/** TODO: Handle DCE, `DCEDataset`s work fine as they are HDAs,
* `DCECollection`s are `dataset_collection`s and need to be HDCAs...
*/
// else if (isDCE(item) && (item as DCESummary).object) {
// const collectionElement = item as DCESummary;
// dataSource = collectionElement.element_type === "dataset_collection" ? "hdca" : "hda"; // incorrect...
// type = collectionElement.element_type === "dataset_collection" ? "dataset_collection" : "dataset";
// id = collectionElement.object.id as string;
// }
else {
throw new Error(`Invalid item type${item.element_type ? `: ${item.element_type}` : ""}`);
}
await copyDataset(item.id, props.history.id, type, dataSource);
await copyDataset(id, props.history.id, type, dataSource);
if (dataSource === "hda") {
datasetCount++;
Expand Down
4 changes: 2 additions & 2 deletions client/src/utils/setDrag.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**
* Helper to configure datatransfer for drag & drop operations
*/
import { type DCESummary, isCollectionItem } from "@/api";
import { type DCESummary, isDCE } from "@/api";
import { type EventData, useEventStore } from "@/stores/eventStore";

type NamedDCESummary = DCESummary & { name: string };
Expand Down Expand Up @@ -50,7 +50,7 @@ export function setItemDragstart<T>(
}

function setCollectionElementName<T extends NamedDCESummary>(obj: T) {
if (isCollectionItem(obj as object)) {
if (isDCE(obj as object)) {
obj["name"] = obj.element_identifier;
}
}

0 comments on commit 793fe46

Please sign in to comment.