From b0acb29e85dc806bbf3ff04391c42c220bfcb178 Mon Sep 17 00:00:00 2001 From: Danielo Rodriguez Date: Fri, 15 Sep 2023 17:37:55 +0200 Subject: [PATCH] feat: limitedForm api it allows to choose which fields to be used either by picking or omiting fields ```js //everything except api.limitedForm('example-form',{ omit:['name','age']}); // only name and age api.limitedForm('example-form',{ pick:['name','age']}); ``` fixes #39 --- src/API.ts | 43 +++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 41 insertions(+), 2 deletions(-) diff --git a/src/API.ts b/src/API.ts index 8b20f28a..b5e1ff45 100644 --- a/src/API.ts +++ b/src/API.ts @@ -6,6 +6,18 @@ import { exampleModalDefinition } from "./exampleModalDefinition"; import ModalFormPlugin from "main"; import { ModalFormError } from "./utils/Error"; import { FormModal } from "./FormModal"; +import { log_error } from "./utils/Log"; + +type pickOption = { pick: string[] } +type omitOption = { omit: string[] } +type limitOptions = { pick?: string[], omit?: string[] } + +function isPickOption(opts: limitOptions): opts is pickOption { + return 'pick' in opts && Array.isArray(opts.pick) +} +function isOmitOption(opts: limitOptions): opts is omitOption { + return 'omit' in opts && Array.isArray(opts.omit) +} export class API { /** @@ -33,6 +45,10 @@ export class API { return this.openModalForm(exampleModalDefinition) } + getFormByName(name: string): FormDefinition | undefined { + return this.plugin.settings?.formDefinitions.find(form => form.name === name); + } + /** * Opens a named form * @param {string} name - The name of the form to open @@ -40,14 +56,37 @@ export class API { * @throws {ModalFormError} - Throws an error if the form definition is not found */ public namedForm(name: string): Promise { - const formDefinition = this.plugin.settings?.formDefinitions.find(form => form.name === name); + const formDefinition = this.getFormByName(name) if (formDefinition) { return this.openModalForm(formDefinition); } else { - throw new ModalFormError(`Form definition ${name} not found`); + const error = new ModalFormError(`Form definition ${name} not found`) + log_error(error); + return Promise.reject(error) } } + public limitedForm(name: string, opts: limitOptions): Promise { + const formDefinition = this.getFormByName(name) + let newFormDefinition: FormDefinition; + if (formDefinition) { + if (isOmitOption(opts)) { + const omit = opts.omit + newFormDefinition = { ...formDefinition, fields: formDefinition.fields.filter(field => !omit.includes(field.name)) } + } else if (isPickOption(opts)) { + newFormDefinition = { ...formDefinition, fields: formDefinition.fields.filter(field => opts.pick.includes(field.name)) } + } else { + throw new ModalFormError('Invalid options provided to limitedForm', `GOT: ${JSON.stringify(opts)}`) + } + return this.openModalForm(newFormDefinition); + } else { + const error = new ModalFormError(`Form definition ${name} not found`) + log_error(error); + return Promise.reject(error) + } + + } + /** * Opens a form with the provided form reference * @param {string | FormDefinition} formReference - The form reference, either a form name of an existing form or an inline form definition