Skip to content

Commit

Permalink
use a computed to track changed history for drag drop
Browse files Browse the repository at this point in the history
  • Loading branch information
ahmedhamidawan committed Aug 21, 2024
1 parent d7560df commit 40379b8
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,8 @@ const historyStore = useHistoryStore();
const historyItemsStore = useHistoryItemsStore();
const { currentUser } = storeToRefs(useUserStore());
const { showDropZone, onDragEnter, onDragLeave, onDragOver, onDrop } = useHistoryDragDrop(props.history.id);
const historyIdComputed = computed(() => props.history.id);
const { showDropZone, onDragEnter, onDragLeave, onDragOver, onDrop } = useHistoryDragDrop(historyIdComputed);
const currentUserOwnsHistory = computed(() => {
return userOwnsHistory(currentUser.value, props.history);
Expand Down
18 changes: 13 additions & 5 deletions client/src/composables/historyDragDrop.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,14 @@ import { useHistoryStore } from "@/stores/historyStore";
type DraggableHistoryItem = HistoryItemSummary | DCEDataset; // TODO: DCESummary instead of DCEDataset

export function useHistoryDragDrop(targetHistoryId?: Ref<string> | string, createNew = false, pinHistories = false) {
const destinationHistoryId = unref(targetHistoryId);
// convert destinationHistoryId to a ref if it's not already
const destinationHistoryId = computed(() => {
if (typeof targetHistoryId === "string") {
return targetHistoryId;
}
return unref(targetHistoryId);
});

const eventStore = useEventStore();
const historyStore = useHistoryStore();

Expand All @@ -29,8 +36,8 @@ export function useHistoryDragDrop(targetHistoryId?: Ref<string> | string, creat
const operationDisabled = computed(
() =>
!fromHistoryId.value ||
(destinationHistoryId && fromHistoryId.value === destinationHistoryId) ||
(!createNew && !destinationHistoryId) ||
(destinationHistoryId.value && fromHistoryId.value === destinationHistoryId.value) ||
(!createNew && !destinationHistoryId.value) ||
!getDragItems().length ||
processingDrop.value
);
Expand All @@ -44,6 +51,7 @@ export function useHistoryDragDrop(targetHistoryId?: Ref<string> | string, creat
}

function onDragEnter(e: DragEvent) {
console.log("onDragEnter", fromHistoryId.value, destinationHistoryId.value);
if (operationDisabled.value) {
return;
}
Expand Down Expand Up @@ -83,8 +91,8 @@ export function useHistoryDragDrop(targetHistoryId?: Ref<string> | string, creat
const originalHistoryId = fromHistoryId.value as string;

let historyId;
if (destinationHistoryId) {
historyId = destinationHistoryId;
if (destinationHistoryId.value) {
historyId = destinationHistoryId.value;
} else if (createNew) {
await historyStore.createNewHistory();
historyId = historyStore.currentHistoryId;
Expand Down

0 comments on commit 40379b8

Please sign in to comment.