Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Provide options for taxonomy elements (terms) #15

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "zapier-cli",
"version": "0.2.2",
"version": "0.2.3",
"scripts": {
"test": "jest",
"build": "tsc",
Expand Down
119 changes: 76 additions & 43 deletions src/fields/elements/getItemElementFields.ts
Original file line number Diff line number Diff line change
@@ -1,59 +1,92 @@
import { ZObject } from 'zapier-platform-core';
import { KontentBundle } from '../../types/kontentBundle';
import { getContentTypeElements } from './getContentTypeElements';
import { ContentTypeElements } from '@kontent-ai/management-sdk';
import { ContentTypeElements, TaxonomyModels } from '@kontent-ai/management-sdk';
import { Field } from '../field';
import { getTaxonomyGroup } from '../../utils/taxonomies/getTaxonomyGroup';

export const getItemElementFields = async (z: ZObject, bundle: KontentBundle<{}>, contentTypeId: string) =>
export const getItemElementFields = (z: ZObject, bundle: KontentBundle<{}>, contentTypeId: string) =>
getContentTypeElements(z, bundle, contentTypeId)
.then(elements => elements.map(getSimpleElementField));
.then(async elements => [elements, await getTermsForTaxonomyElements(z, bundle)(elements)] as const)
.then(([elements, termsByElementId]) => elements.map(getSimpleElementField(termsByElementId)));

const getTermsForTaxonomyElements = (z: ZObject, bundle: KontentBundle<{}>) =>
(elements: readonly ContentTypeElements.ContentTypeElementModel[]) =>
Promise.all(elements
.filter(el => el.type === "taxonomy")
.map(async el => [el.id, await getTaxonomyGroup(z, bundle)((el as ContentTypeElements.ITaxonomyElement).taxonomy_group.id || '')] as const))
.then(entries => entries.map(([elId, group]) => [elId, group.terms.flatMap(createTermData)] as const))
.then(entries => new Map(entries));

type TermData = Readonly<{
id: string;
name: string;
codename: string;
}>;

const createTermData = (term: TaxonomyModels.Taxonomy): ReadonlyArray<TermData> => [
{
id: term.id,
name: term.name,
codename: term.codename,
},
...term.terms.flatMap(createTermData),
];

export type ElementFields = Readonly<{
[key: `elements__${string}`]: string | string[] | number | undefined;
}>;

function getSimpleElementField(element: ContentTypeElements.ContentTypeElementModel) {
switch (element.type) {
case 'text':
case 'rich_text':
case 'custom':
return getField(element, { type: 'text' });

case 'number':
return getField(element, { type: 'float' });

case 'date_time':
return getField(element, { type: 'datetime' });

case 'modular_content':
const opts = {
search: 'find_item.id',
list: true,
dynamic: 'get_linked_items.id.name',
type: 'string',
altersDynamicFields: false,
} as const;

return getField(element, opts);

case 'multiple_choice': {
const choices = element.options.map(o => ({ label: o.name, value: o.codename || '', sample: '' }));

return getField(element, { type: 'unicode', list: element.mode === 'multiple', choices });
const getSimpleElementField = (termsByElementId: ReadonlyMap<string, readonly TermData[]>) =>
(element: ContentTypeElements.ContentTypeElementModel) => {
switch (element.type) {
case 'text':
case 'rich_text':
case 'custom':
return getField(element, { type: 'text' });

case 'number':
return getField(element, { type: 'float' });

case 'date_time':
return getField(element, { type: 'datetime' });

case 'modular_content':
const opts = {
search: 'find_item.id',
list: true,
dynamic: 'get_linked_items.id.name',
type: 'string',
altersDynamicFields: false,
} as const;

return getField(element, opts);

case 'multiple_choice': {
const choices = element.options.map(o => ({ label: o.name, value: o.codename || '', sample: '' }));

return getField(element, { type: 'unicode', list: element.mode === 'multiple', choices });
}
case 'asset':
return getField(element, { type: 'unicode', list: true });

case 'taxonomy': {
const terms = termsByElementId.get(element.id) ?? [];
const idChoices = terms.map(t => ({ value: t.id, label: t.name, sample: t.id }));
const codenameChoices = terms.map(t => ({ value: t.codename, label: `${t.name} (codename)`, sample: t.codename }));

return getField(element, { type: 'unicode', choices: [...idChoices, ...codenameChoices], list: true });
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it not possible to have two tabs one for id and one for codename?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am no sure what you mean by tabs, but it is possible to have two separate fields and then choose one of them at runtime. It is an unfortunate situation, because we need both options to be able to accept both identifiers from previous steps, but in the dropdown in the UI, we would ideally want just one of them.

}

case 'url_slug':
return getField(element, { type: 'unicode' });

case 'guidelines':
return getField(element, { type: 'copy' });
default:
return undefined;
}
case 'asset':
case 'taxonomy':
return getField(element, { type: 'unicode', list: true });

case 'url_slug':
return getField(element, { type: 'unicode' });

case 'guidelines':
return getField(element, { type: 'copy' });
default:
return undefined;
}
}

function getField(element: ElementWithoutSnippets, extra?: Partial<Field>) {
const base = {
Expand Down
1 change: 1 addition & 0 deletions src/searches/findLanguage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ export const findLanguage = {
key: 'find_language',
operation: {
perform: execute,
outputFields,
inputFields: [
...languageSearchFields,
],
Expand Down
2 changes: 1 addition & 1 deletion src/utils/range.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
export const range = (countOrStart: number, end?: number, step: number = 1) =>
[...new Array((end !== undefined ? Math.floor((end - countOrStart) / step) : countOrStart))]
[...new Array(end !== undefined ? Math.floor((end - countOrStart) / step) : countOrStart)]
.map((_, i) => i * step);
10 changes: 10 additions & 0 deletions src/utils/taxonomies/getTaxonomyGroup.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { ZObject } from "zapier-platform-core";
import { KontentBundle } from "../../types/kontentBundle";
import { createManagementClient } from "../kontentServices/managementClient";

export const getTaxonomyGroup = (z: ZObject, bundle: KontentBundle<{}>) => (groupId: string) =>
createManagementClient(z, bundle)
.getTaxonomy()
.byTaxonomyId(groupId)
.toPromise()
.then(res => res.data);