Skip to content

Commit

Permalink
feat: limitedForm api
Browse files Browse the repository at this point in the history
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
  • Loading branch information
danielo515 committed Sep 15, 2023
1 parent 2610b37 commit b0acb29
Showing 1 changed file with 41 additions and 2 deletions.
43 changes: 41 additions & 2 deletions src/API.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
/**
Expand Down Expand Up @@ -33,21 +45,48 @@ 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
* @returns {Promise<FormResult>} - A promise that resolves with the form result
* @throws {ModalFormError} - Throws an error if the form definition is not found
*/
public namedForm(name: string): Promise<FormResult> {
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<FormResult> {
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
Expand Down

0 comments on commit b0acb29

Please sign in to comment.