Files
directus/app/src/composables/use-template-data.ts
Rijk van Zanten 8f00e37daf Add improved collection organization setup (#8623)
* Add migrations, start on service

* Dont track TODO files

* Update collection types, add collection type

* Allow drag and drop sorting of collections

* Add tooltip

* Add grouping + collapsed state

* Fix nested closed state

* Tweak active drag styling

* Remove collapsed state

* Add folder creation/editing

* Render collections as nested tree in nav

* Fix open active state

* Add dense when collection count > 5

* Add visible toggle

* Add show-hidden toggle

* Fix css specificity

* Add support for query in v-list-group

* Add missing cascade

* Remove collapsed state

* Finish three-way toggle

* Add custom lock icon

* Fix icon size in non-dense

* Redirect to first & open tree on load

* Dont make prop required

* Fix search

* Only apply archive filter when enabled in settings

* Add archive view

* Add translations

* Hide organization fields

* Show system collections
2021-10-15 21:19:00 +00:00

53 lines
1.4 KiB
TypeScript

import api from '@/api';
import { Collection } from '@/types';
import { getFieldsFromTemplate } from '@directus/shared/utils';
import { computed, Ref, ref, watch } from 'vue';
type UsableTemplateData = {
templateData: Ref<Record<string, any> | undefined>;
loading: Ref<boolean>;
error: Ref<any>;
};
export default function useTemplateData(
collection: Ref<Collection | null>,
primaryKey: Ref<string>
): UsableTemplateData {
const templateData = ref<Record<string, any>>();
const loading = ref(false);
const error = ref<any>(null);
const fields = computed(() => {
if (!collection.value?.meta?.display_template) return null;
return getFieldsFromTemplate(collection.value.meta.display_template);
});
watch([collection, primaryKey], fetchTemplateValues, { immediate: true });
return { templateData, loading, error };
async function fetchTemplateValues() {
if (!primaryKey.value || primaryKey.value === '+' || fields.value === null || !collection.value) return;
loading.value = true;
const endpoint = collection.value.collection.startsWith('directus_')
? `/${collection.value.collection.substring(9)}/${primaryKey.value}`
: `/items/${collection.value.collection}/${encodeURIComponent(primaryKey.value)}`;
try {
const result = await api.get(endpoint, {
params: {
fields: fields.value,
},
});
templateData.value = result.data.data;
} catch (err: any) {
error.value = err;
} finally {
loading.value = false;
}
}
}