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
for now, only `getDragData` for `DCEDataset`s
  • Loading branch information
ahmedhamidawan committed Aug 17, 2024
1 parent 1365a1d commit f99d3d1
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 20 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
54 changes: 37 additions & 17 deletions client/src/components/History/CurrentHistory/HistoryPanel.vue
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { storeToRefs } from "pinia";
import { computed, onMounted, type Ref, ref, set as VueSet, unref, watch } from "vue";
import {
type HDAObject,
type DCEDataset,
type HistoryItemSummary,
type HistorySummaryExtended,
isDatasetElement,
Expand Down Expand Up @@ -61,7 +61,7 @@ interface Props {
isMultiViewItem?: boolean;
}
type DraggableHistoryItem = HistoryItemSummary | HDAObject;
type DraggableHistoryItem = HistoryItemSummary | DCEDataset; // TODO: DCESummary instead of DCEDataset
type ContentItemRef = Record<string, Ref<InstanceType<typeof ContentItem> | null>>;
Expand Down Expand Up @@ -236,14 +236,17 @@ function getDragData() {
const eventStore = useEventStore();
const dragItems = eventStore.getDragItems();
// Filter out any non-history items
const historyItems = dragItems?.map((item: any) => {
if (isHistoryItem(item)) {
return item;
} else if (isDatasetElement(item)) {
return item.object;
}
}) as DraggableHistoryItem[];
const historyId = historyItems?.[0]?.history_id;
// TODO: `isDCE` instead of `isDatasetElement`
const historyItems = dragItems?.filter(
(item: any) => isHistoryItem(item) || isDatasetElement(item)
) as DraggableHistoryItem[];
// TODO: handle historyId === null || historyItems.length === 0
const historyId = historyItems[0]
? isHistoryItem(historyItems[0])
? historyItems[0].history_id
: historyItems[0].object?.history_id
: null;
return { data: historyItems, sameHistory: historyId === props.history.id, multiple: historyItems?.length > 1 };
}
Expand Down Expand Up @@ -397,15 +400,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 f99d3d1

Please sign in to comment.