Skip to content

Commit

Permalink
Add disabled conditions
Browse files Browse the repository at this point in the history
  • Loading branch information
duydvu committed Sep 14, 2022
1 parent 33b761f commit d1b9bac
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 2 deletions.
7 changes: 7 additions & 0 deletions src/check-conditions.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { Filter } from '@directus/shared/types';
import { validatePayload } from '@directus/shared/utils';

export function checkConditions(item: Record<string, any>, conditions: Filter) {
const errors = validatePayload(conditions, item, { requireAll: true });
return errors.length === 0;
}
13 changes: 12 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ export default defineInterface({
types: ['alias'],
localTypes: ['presentation'],
group: 'presentation',
options: [
options: ({ collection }) => [
{
field: 'layout',
name: 'Layout',
Expand Down Expand Up @@ -127,6 +127,17 @@ export default defineInterface({
},
},
},
{
field: 'disabledConditions',
name: 'Disabled Conditions',
type: 'json',
meta: {
interface: 'system-filter',
options: {
collectionName: collection,
},
},
},
],
},
},
Expand Down
9 changes: 8 additions & 1 deletion src/interface.vue
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
class="action"
:class="[trigger.type]"
:loading="loadings[index].value"
:disabled="disableds[index]"
:secondary="trigger.type !== 'primary'"
v-bind="{ [trigger.size]: true }"
@click="onClick(trigger.method, parsedUrls[index], index)"
Expand All @@ -19,7 +20,9 @@
<script lang="ts">
import { computed, defineComponent, inject, PropType, ref } from 'vue';
import { useApi, useStores } from '@directus/extensions-sdk';
import { Filter } from '@directus/shared/types';
import { render } from 'micromustache';
import { checkConditions } from './check-conditions';
type Trigger = {
label: string;
Expand All @@ -28,6 +31,7 @@ type Trigger = {
icon: string;
url: string;
method: string;
disabledConditions: Filter;
};
export default defineComponent({
Expand All @@ -48,10 +52,13 @@ export default defineComponent({
const values = inject('values', ref<Record<string, any>>({}));
const parsedUrls = computed(() => props.triggers.map((trigger) => render(trigger.url ?? '', values.value)));
const disableds = computed(() => props.triggers.map((trigger) => (
trigger.disabledConditions && checkConditions(values.value, trigger.disabledConditions)),
));
const loadings = props.triggers.map(() => ref(false));
return { loadings, parsedUrls, onClick };
return { loadings, parsedUrls, disableds, onClick };
async function onClick(method: string, url: string, index: number) {
const loading = loadings[index];
Expand Down

0 comments on commit d1b9bac

Please sign in to comment.