Skip to content

Commit

Permalink
Clean-up useItem composable
Browse files Browse the repository at this point in the history
  • Loading branch information
paescuj committed Apr 7, 2024
1 parent 20dd0e7 commit 9f66c2e
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 105 deletions.
5 changes: 0 additions & 5 deletions .changeset/olive-humans-cross.md

This file was deleted.

175 changes: 75 additions & 100 deletions app/src/composables/use-item.ts
Expand Up @@ -12,15 +12,15 @@ import { unexpectedError } from '@/utils/unexpected-error';
import { validateItem } from '@/utils/validate-item';
import { useCollection } from '@directus/composables';
import { isSystemCollection } from '@directus/system-data';
import { Field, Query, Relation } from '@directus/types';
import { Alterations, Field, Item, PrimaryKey, Query, Relation } from '@directus/types';
import { getEndpoint } from '@directus/utils';
import { AxiosResponse } from 'axios';
import { mergeWith } from 'lodash';
import { ComputedRef, MaybeRef, Ref, computed, isRef, ref, unref, watch } from 'vue';
import { UsablePermissions, usePermissions } from './use-permissions';

type UsableItem<T extends Record<string, any>> = {
edits: Ref<Record<string, any>>;
type UsableItem<T extends Item> = {
edits: Ref<Item>;
hasEdits: ComputedRef<boolean>;
item: Ref<T | null>;
permissions: UsablePermissions;
Expand All @@ -40,9 +40,9 @@ type UsableItem<T extends Record<string, any>> = {
validationErrors: Ref<any[]>;
};

export function useItem<T extends Record<string, any>>(
export function useItem<T extends Item>(
collection: Ref<string>,
primaryKey: Ref<string | number | null>,
primaryKey: Ref<PrimaryKey | null>,
query: MaybeRef<Query> = {},
): UsableItem<T> {
const { info: collectionInfo, primaryKeyField } = useCollection(collection);
Expand All @@ -53,7 +53,7 @@ export function useItem<T extends Record<string, any>>(
const saving = ref(false);
const deleting = ref(false);
const archiving = ref(false);
const edits = ref<Record<string, any>>({});
const edits = ref<Item>({});
const hasEdits = computed(() => Object.keys(edits.value).length > 0);
const isNew = computed(() => primaryKey.value === '+');
const isSingle = computed(() => !!collectionInfo.value?.meta?.singleton);
Expand Down Expand Up @@ -115,7 +115,7 @@ export function useItem<T extends Record<string, any>>(
try {
const response = await api.get(itemEndpoint.value, { params: unref(query) });
setItemValueToResponse(response);
} catch (err: any) {
} catch (err) {
error.value = err;
} finally {
loadingItem.value = false;
Expand Down Expand Up @@ -183,85 +183,76 @@ export function useItem<T extends Record<string, any>>(

const itemData = await api.get(itemEndpoint.value, { params: { fields } });

const newItem: { [field: string]: any } = {
const newItem: Item = {
...(itemData.data.data || {}),
...edits.value,
};

// Make sure to delete the primary key if it's has auto increment enabled
if (primaryKeyField.value && primaryKeyField.value.field in newItem) {
if (primaryKeyField.value.schema?.has_auto_increment || primaryKeyField.value.meta?.special?.includes('uuid')) {
delete newItem[primaryKeyField.value.field];
}
}

// Make sure to delete nested relational primary keys
const fieldsStore = useFieldsStore();
const relationsStore = useRelationsStore();
const relations = relationsStore.getRelationsForCollection(collection.value);

for (const relation of relations) {
const relatedPrimaryKeyField = fieldsStore.getPrimaryKeyFieldForCollection(relation.collection);
if (!relatedPrimaryKeyField) continue;

const existsJunctionRelated = relationsStore.relations.find((r) => {
return r.collection === relation.collection && r.meta?.many_field === relation.meta?.junction_field;
});
const existsJunctionRelated = relationsStore.relations.find(
(r) => r.collection === relation.collection && r.meta?.many_field === relation.meta?.junction_field,
);

const oneField = relation.meta?.one_field;
if (!oneField || !(oneField in newItem)) continue;

if (oneField && oneField in newItem) {
const fieldsToFetch = fields
.filter((field) => field.split('.')[0] === oneField || field === '*')
.map((field) => (field.includes('.') ? field.split('.').slice(1).join('.') : '*'));

if (Array.isArray(newItem[oneField])) {
const existingItems = await findExistingRelatedItems(
newItem,
relation,
relatedPrimaryKeyField,
fieldsToFetch,
);

newItem[oneField] = newItem[oneField].map((relatedItem: any) => {
if (typeof relatedItem !== 'object' && existingItems.length > 0) {
relatedItem = existingItems.find((existingItem: any) => existingItem.id === relatedItem);
}

delete relatedItem[relatedPrimaryKeyField!.field];

updateJunctionRelatedKey(relation, existsJunctionRelated, fieldsStore, relatedItem);
return relatedItem;
});
} else {
const createdRelatedItems = newItem[oneField]?.create;
const updatedRelatedItems = newItem[oneField]?.update;
const deletedRelatedItems = newItem[oneField]?.delete;

let existingItems: any[] = await findExistingRelatedItems(
item.value,
relation,
relatedPrimaryKeyField,
fieldsToFetch,
);

existingItems = existingItems.filter((i) => {
return deletedRelatedItems.indexOf(i[relatedPrimaryKeyField!.field]) === -1;
});

for (const item of updatedRelatedItems) {
updateJunctionRelatedKey(relation, existsJunctionRelated, fieldsStore, item);
}
const fieldsToFetch = fields
.filter((field) => field.split('.')[0] === oneField || field === '*')
.map((field) => (field.includes('.') ? field.split('.').slice(1).join('.') : '*'));

for (const item of existingItems) {
updateExistingRelatedItems(updatedRelatedItems, item, relatedPrimaryKeyField, relation);
if (Array.isArray(newItem[oneField])) {
const existingItems = await findExistingRelatedItems(newItem, relation, relatedPrimaryKeyField, fieldsToFetch);

newItem[oneField] = newItem[oneField].map((relatedItem: any) => {
if (typeof relatedItem !== 'object' && existingItems.length > 0) {
relatedItem = existingItems.find((existingItem) => existingItem.id === relatedItem);
}

updatedRelatedItems.length = 0;
delete relatedItem[relatedPrimaryKeyField.field];

for (const item of existingItems) {
delete item[relatedPrimaryKeyField!.field];
createdRelatedItems.push(item);
}
updateJunctionRelatedKey(relation, existsJunctionRelated, relatedItem);
return relatedItem;
});
} else {
const newRelatedItem: Alterations = newItem[oneField];

let existingItems = await findExistingRelatedItems(
item.value as Item,
relation,
relatedPrimaryKeyField,
fieldsToFetch,
);

existingItems = existingItems.filter((i) => {
return newRelatedItem.delete.indexOf(i[relatedPrimaryKeyField.field]) === -1;
});

for (const item of newRelatedItem.update) {
updateJunctionRelatedKey(relation, existsJunctionRelated, item);
}

for (const item of existingItems) {
updateExistingRelatedItems(newRelatedItem.update, item, relatedPrimaryKeyField, relation);
}

newRelatedItem.update.length = 0;

for (const item of existingItems) {
delete item[relatedPrimaryKeyField!.field];
newRelatedItem.create.push(item);
}
}
}
Expand All @@ -285,25 +276,25 @@ export function useItem<T extends Record<string, any>>(
edits.value = {};

return primaryKeyField.value ? response.data.data[primaryKeyField.value.field] : null;
} catch (err: any) {
saveErrorHandler(err);
} catch (error) {
saveErrorHandler(error);
} finally {
saving.value = false;
}

async function findExistingRelatedItems(
item: any,
item: Item,
relation: Relation,
relatedPrimaryKeyField: Field | null,
relatedPrimaryKeyField: Field,
fieldsToFetch: string[],
) {
const existingIds = item?.[relation.meta!.one_field!].filter((item: any) => typeof item !== 'object');
let existingItems: any[] = [];
let existingItems: Item[] = [];

if (existingIds.length > 0) {
const response = await api.get(getEndpoint(relation.collection), {
params: {
fields: [relatedPrimaryKeyField!.field, ...fieldsToFetch],
fields: [relatedPrimaryKeyField.field, ...fieldsToFetch],
[`filter[${relation.field}][_eq]`]: primaryKey.value,
},
});
Expand All @@ -315,49 +306,33 @@ export function useItem<T extends Record<string, any>>(
}

function updateExistingRelatedItems(
updatedRelatedItems: any,
item: any,
relatedPrimaryKeyField: Field | null,
updatedRelatedItems: Item[],
item: Item,
relatedPrimaryKeyField: Field,
relation: Relation,
) {
for (const updatedItem of updatedRelatedItems) {
copyUserEditValuesToExistingItem(item, relatedPrimaryKeyField, updatedItem, relation);
}
}
if (item[relatedPrimaryKeyField.field] !== updatedItem[relatedPrimaryKeyField.field]) continue;

function copyUserEditValuesToExistingItem(
item: any,
relatedPrimaryKeyField: Field | null,
updatedItem: any,
relation: Relation,
) {
if (item[relatedPrimaryKeyField!.field] === updatedItem[relatedPrimaryKeyField!.field]) {
const columns = fields.filter((s) => s.startsWith(relation.meta!.one_field!));
for (const field of fields) {
const [relationField, fieldName] = field.split('.');

for (const col of columns) {
const colName = col.split('.')[1];
if (relationField !== relation.meta!.one_field!) continue;

if (colName !== undefined) {
item[colName] = updatedItem[colName];
}
if (fieldName && fieldName in updatedItem) item[fieldName] = updatedItem[fieldName];
}
}
}
}

function updateJunctionRelatedKey(
relation: Relation,
existsJunctionRelated: Relation | undefined,
fieldsStore: any,
item: any,
) {
if (relation.meta?.junction_field && existsJunctionRelated?.related_collection) {
const junctionRelatedPrimaryKeyField = fieldsStore.getPrimaryKeyFieldForCollection(
existsJunctionRelated.related_collection,
);
function updateJunctionRelatedKey(relation: Relation, existsJunctionRelated: Relation | undefined, item: Item) {
if (relation.meta?.junction_field && existsJunctionRelated?.related_collection) {
const junctionRelatedPrimaryKeyField = fieldsStore.getPrimaryKeyFieldForCollection(
existsJunctionRelated.related_collection,
);

if (relation.meta.junction_field in item && junctionRelatedPrimaryKeyField.schema!.is_generated) {
delete item[relation.meta.junction_field][junctionRelatedPrimaryKeyField!.field];
if (relation.meta.junction_field in item && junctionRelatedPrimaryKeyField?.schema!.is_generated) {
delete item[relation.meta.junction_field][junctionRelatedPrimaryKeyField!.field];
}
}
}
}
Expand Down

0 comments on commit 9f66c2e

Please sign in to comment.