Reuse already calculated values (#19479)

* Reuse already calculated values

* Add changeset

---------

Co-authored-by: Pascal Jufer <pascal-jufer@bluewin.ch>
This commit is contained in:
Gerard Lamusse
2023-08-21 13:52:03 +02:00
committed by GitHub
parent 1625953af6
commit 71b0a40cff
2 changed files with 17 additions and 12 deletions

View File

@@ -0,0 +1,5 @@
---
"@directus/app": patch
---
Optimized code for permisson check by reusing already calculated values

View File

@@ -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<boolean>;
@@ -27,18 +27,18 @@ export function usePermissions(collection: Ref<string>, item: Ref<any>, 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;