From 71b0a40cff898b4d3cf91cd41cc1ef8c711ecdd4 Mon Sep 17 00:00:00 2001 From: Gerard Lamusse Date: Mon, 21 Aug 2023 13:52:03 +0200 Subject: [PATCH] Reuse already calculated values (#19479) * Reuse already calculated values * Add changeset --------- Co-authored-by: Pascal Jufer --- .changeset/few-chairs-cough.md | 5 +++++ app/src/composables/use-permissions.ts | 24 ++++++++++++------------ 2 files changed, 17 insertions(+), 12 deletions(-) create mode 100644 .changeset/few-chairs-cough.md diff --git a/.changeset/few-chairs-cough.md b/.changeset/few-chairs-cough.md new file mode 100644 index 0000000000..7243ba31df --- /dev/null +++ b/.changeset/few-chairs-cough.md @@ -0,0 +1,5 @@ +--- +"@directus/app": patch +--- + +Optimized code for permisson check by reusing already calculated values diff --git a/app/src/composables/use-permissions.ts b/app/src/composables/use-permissions.ts index d720170cd5..19a9633435 100644 --- a/app/src/composables/use-permissions.ts +++ b/app/src/composables/use-permissions.ts @@ -1,10 +1,10 @@ import { usePermissionsStore } from '@/stores/permissions'; import { useUserStore } from '@/stores/user'; -import { Field } from '@directus/types'; -import { computed, ComputedRef, Ref } from 'vue'; -import { cloneDeep } from 'lodash'; -import { isAllowed } from '../utils/is-allowed'; import { useCollection } from '@directus/composables'; +import { Field } from '@directus/types'; +import { cloneDeep } from 'lodash'; +import { computed, ComputedRef, Ref } from 'vue'; +import { isAllowed } from '../utils/is-allowed'; type UsablePermissions = { createAllowed: ComputedRef; @@ -27,18 +27,18 @@ export function usePermissions(collection: Ref, item: Ref, isNew: R const deleteAllowed = computed(() => isAllowed(collection.value, 'delete', item.value)); - const saveAllowed = computed(() => { - if (isNew.value) { - return true; - } - - return isAllowed(collection.value, 'update', item.value); - }); - const updateAllowed = computed(() => isAllowed(collection.value, 'update', item.value)); const shareAllowed = computed(() => isAllowed(collection.value, 'share', item.value)); + const saveAllowed = computed(() => { + if (isNew.value) { + return createAllowed.value; + } + + return updateAllowed.value; + }); + const archiveAllowed = computed(() => { if (!collectionInfo.value?.meta?.archive_field) return false;