Skip to content

Commit

Permalink
Convert axios queries to fetcher + type fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
davelopez committed Aug 7, 2024
1 parent e2b6922 commit 7c95ac2
Show file tree
Hide file tree
Showing 5 changed files with 126 additions and 135 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -68,12 +68,14 @@ const rootCollection = computed(() => {
const isRoot = computed(() => dsc.value == rootCollection.value);
const canEdit = computed(() => isRoot.value && canMutateHistory(props.history));

function updateDsc(collection: any, fields: Object | undefined) {
updateContentFields(collection, fields).then((response) => {
Object.keys(response).forEach((key) => {
collection[key] = response[key];
});
});
async function updateDsc(collection: CollectionEntry, fields: Object | undefined) {
if (!isHDCA(collection)) {
return;
}
const updatedCollection = await updateContentFields(collection, fields);
// Update only editable fields
collection.name = updatedCollection.name || collection.name;
collection.tags = updatedCollection.tags || collection.tags;
}

function getItemKey(item: DCESummary) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { FontAwesomeIcon } from "@fortawesome/vue-fontawesome";
import { BDropdown, BDropdownItem, BDropdownText, BModal } from "bootstrap-vue";
import { toRef } from "vue";
import type { HistorySummary, HistorySummaryExtended } from "@/api";
import { type HistorySummaryExtended } from "@/api";
import {
deleteAllHiddenContent,
purgeAllDeletedContent,
Expand Down Expand Up @@ -42,7 +42,7 @@ function purgeAllDeleted() {
runOperation(() => purgeAllDeletedContent(props.history));
}
async function runOperation(operation: () => Promise<HistorySummary>) {
async function runOperation(operation: () => Promise<unknown>) {
emit("update:operation-running", props.history.update_time);
await operation();
emit("update:operation-running", props.history.update_time);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -158,12 +158,14 @@ import {
undeleteSelectedContent,
unhideSelectedContent,
} from "components/History/model/crud";
import { createDatasetCollection, getHistoryContent } from "components/History/model/queries";
import { DatatypesProvider, DbKeyProvider } from "components/providers";
import SingleItemSelector from "components/SingleItemSelector";
import { StatelessTags } from "components/Tags";
import { GalaxyApi } from "@/api";
import { createDatasetCollection, filtersToQueryValues } from "@/components/History/model/queries";
import { useConfig } from "@/composables/config";
import { rethrowSimple } from "@/utils/simple-error";
export default {
components: {
Expand Down Expand Up @@ -368,8 +370,18 @@ export default {
async buildDatasetListAll() {
let allContents = [];
const filters = HistoryFilters.getQueryDict(this.filterText);
const filterQuery = filtersToQueryValues(filters);
const { data, error } = await GalaxyApi().GET("/api/histories/{history_id}/contents/{type}s", {
params: {
path: { history_id: this.history.id, type: "dataset" },
query: { ...filterQuery, v: "dev" },
},
});
if (error) {
rethrowSimple(error);
}
allContents = await getHistoryContent(this.history.id, filters, "dataset");
allContents = data;
this.buildNewCollection("list", allContents);
},
Expand Down
125 changes: 0 additions & 125 deletions client/src/components/History/model/queries.js

This file was deleted.

102 changes: 102 additions & 0 deletions client/src/components/History/model/queries.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
import { type components, GalaxyApi, type HistoryItemSummary, type HistorySummary } from "@/api";
import { rethrowSimple } from "@/utils/simple-error";

type BulkOperation = components["schemas"]["HistoryContentItemOperation"];
type QueryFilters = Record<string, unknown>;

export function filtersToQueryValues(filters: QueryFilters) {
const filterKeys = Object.keys(filters);
const filterValues = filterKeys.map((key) => `${filters[key]}`);
return { q: filterKeys, qv: filterValues };
}

/**
* Deletes item from history
*/
export async function deleteContent(
content: HistoryItemSummary,
deleteParams: Partial<{ purge: boolean; recursive: boolean }> = {}
) {
const defaults = { purge: false, recursive: false, stop_job: true };
const params = Object.assign({}, defaults, deleteParams);
const { data, error } = await GalaxyApi().DELETE("/api/histories/{history_id}/contents/{type}s/{id}", {
params: {
path: { history_id: content.history_id, type: content.history_content_type, id: content.id },
query: params,
},
});

if (error) {
rethrowSimple(error);
}
return data;
}

/**
* Update specific fields on datasets or collections.
*/
export async function updateContentFields(content: HistoryItemSummary, newFields = {}) {
const { data, error } = await GalaxyApi().PUT("/api/histories/{history_id}/contents/{type}s/{id}", {
params: {
path: { history_id: content.history_id, type: content.history_content_type, id: content.id },
},
body: newFields,
});

if (error) {
rethrowSimple(error);
}
return data;
}

/**
* Performs an operation on a specific set of items or all the items
* matching the filters.
* If a specific set of items is provided, the filters are ignored, otherwise
* the filters will determine which items are processed.
*/
export async function bulkUpdate(
history: HistorySummary,
operation: BulkOperation,
filters: QueryFilters,
items = [],
params = null
) {
const { data, error } = await GalaxyApi().PUT("/api/histories/{history_id}/contents/bulk", {
params: {
path: { history_id: history.id },
query: filtersToQueryValues(filters),
},
body: {
operation,
items,
params,
},
});

if (error) {
rethrowSimple(error);
}
return data;
}

export async function createDatasetCollection(history: HistorySummary, inputs = {}) {
const defaults = {
collection_type: "list",
copy_elements: true,
name: "list",
element_identifiers: [],
hide_source_items: true,
};
const payload = Object.assign({}, defaults, inputs);

const { data, error } = await GalaxyApi().POST("/api/histories/{history_id}/contents", {
params: { path: { history_id: history.id } },
body: { ...payload, instance_type: "history", type: "dataset_collection" },
});

if (error) {
rethrowSimple(error);
}
return data;
}

0 comments on commit 7c95ac2

Please sign in to comment.