Files
directus/app/src/interfaces/list-m2m/use-selection.ts
Nicola Krumschmidt 051df415df Fix extensions (#6377)
* Add support for npm extensions

* Allow extensions to import vue from the main app

* Bundle app extensions on server startup

* Fix return type of useLayoutState

* Add shared package

* Add extension-sdk package

* Add type declaration files to allow deep import of shared package

* Add extension loading to shared

* Refactor extension loading to use shared package

* Remove app bundle newline replacement

* Fix extension loading in development

* Rename extension entrypoints

* Update extension build instructions

* Remove vite auto-replacement workaround

* Update package-lock.json

* Remove newline from generated extension entrypoint

* Update package-lock.json

* Build shared package as cjs and esm

* Move useLayoutState composable to shared

* Reverse vite base env check

* Share useLayoutState composable through extension-sdk

* Update layout docs

* Update package versions

* Small cleanup

* Fix layout docs

* Fix imports

* Add nickrum to codeowners

* Fix typo

* Add 'em to vite config too

* Fix email

Co-authored-by: rijkvanzanten <rijkvanzanten@me.com>
2021-06-23 12:43:06 -04:00

66 lines
1.9 KiB
TypeScript

import { Filter } from '@directus/shared/types';
import { get } from 'lodash';
import { computed, ComputedRef, Ref, ref } from 'vue';
import { RelationInfo } from './use-relation';
type UsableSelection = {
stageSelection: (newSelection: (number | string)[]) => void;
selectModalActive: Ref<boolean>;
selectedPrimaryKeys: ComputedRef<(string | number)[]>;
selectionFilters: ComputedRef<Filter[]>;
};
export default function useSelection(
value: Ref<(string | number | Record<string, any>)[] | null>,
items: Ref<Record<string, any>[]>,
relation: Ref<RelationInfo>,
emit: (newVal: any[] | null) => void
): UsableSelection {
const selectModalActive = ref(false);
const selectedPrimaryKeys = computed(() => {
if (items.value === null) return [];
const { relationPkField, junctionField } = relation.value;
const selectedKeys = items.value.reduce((acc, current) => {
const key = get(current, [junctionField, relationPkField]);
if (key !== undefined) acc.push(key);
return acc;
}, []) as (number | string)[];
return selectedKeys;
});
const selectionFilters = computed<Filter[]>(() => {
const { relationPkField } = relation.value;
if (selectedPrimaryKeys.value.length === 0) return [];
const filter: Filter = {
key: 'selection',
field: relationPkField,
operator: 'nin',
value: selectedPrimaryKeys.value.join(','),
locked: true,
};
return [filter];
});
function stageSelection(newSelection: (number | string)[]) {
const { junctionField } = relation.value;
const selection = newSelection.reduce((acc, item) => {
if (selectedPrimaryKeys.value.includes(item) === false) acc.push({ [junctionField]: item });
return acc;
}, [] as Record<string, any>[]);
const newVal = [...selection, ...(value.value || [])];
if (newVal.length === 0) emit(null);
else emit(newVal);
}
return { stageSelection, selectModalActive, selectedPrimaryKeys, selectionFilters };
}