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

New method for executing on save operations #77

Open
iGrubesic opened this issue Oct 31, 2021 · 0 comments
Open

New method for executing on save operations #77

iGrubesic opened this issue Oct 31, 2021 · 0 comments
Assignees
Labels
enhancement New feature or request form-builder

Comments

@iGrubesic
Copy link
Member

Current Approach

At the moment when using a field that needs to mutate the dataset on save for example the ImageComponent needing to execute a file upload and set the value to the uploaded URL, the component itself pushes its save method into a service. This method is then called when save is triggered. The save method edits the control's value to the exit value.

This approach has a number of problems:

  • The form builder component that created the field needs to be alive when save is triggered. This isn't always the case, the page builder creates multiple form builder instances.
  • We're editing the value of the control not the save object. This means value changes triggers multiple times during the save process.

current ImageComponent save method

save(moduleId: string, documentId: string) {
    if (this.value) {
      if (
        this.imageUrl.value &&
        this.imageUrl.value !== this.value.name
      ) {
        return of(this.imageUrl.value).pipe(
          tap(() => this.cData.control.setValue(this.imageUrl.value))
        );
      } else {
        const name = this.cData.preserveFileName ? this.value.name : [
          moduleId,
          documentId,
          random.string()
        ].join('-') +
          /**
           * TODO:
           * Maybe we should put a type extension based on type
           * instead of taking from the name
           */
          (this.value.name.split('.')[1]);

        return from(
          this.storage.upload(name, this.value, {
            contentType: this.value.type,
            customMetadata: {
              moduleId,
              documentId,
              ...(this.cData.generatedImages &&
                formatGeneratedImages(this.cData.generatedImages))
            }
          })
        ).pipe(
          switchMap((res: any) => res.ref.getDownloadURL()),
          tap(url => this.cData.control.setValue(url))
        );
      }
    } else {
      this.cData.control.setValue(this.imageUrl.value);
      return of({});
    }
  }

Desired Approach

  • All of the properties that need to be mutated during saving need to be accumulated when the form is created along with data pointers.
  • Each such component needs to provide a self-sufficient method (meaning it doesn't depend on anything in the angular component) separate from the component itself.
  • This method receives as arguments the initial value, current value, configuration from the definitions, moduleId, documentId and should return the final value.
  • When save is triggered all of these methods are called in parallel.
  • Once they all complete successfully the final object is mutated using the pointers collected initially.
  • These methods aren't even called if the initial and current value don't differ

example save method

export function ImageSave(
  initial: string,
  current: any,
  configuration: any,
  moduleId: string,
  documentId: string
) {
  return from(
    this.storage.upload(current.name, current.value, {
      contentType: current.type,
      customMetadata: {
        moduleId,
        documentId,
        ...(configuration.generatedImages &&
          formatGeneratedImages(configuration.generatedImages))
      }
    })
  ).pipe(
    switchMap((res: any) => res.ref.getDownloadURL())
  );
}

Note: We still need to properly define this.

@iGrubesic iGrubesic added enhancement New feature or request form-builder labels Oct 31, 2021
flauc added a commit that referenced this issue Feb 11, 2022
flauc added a commit that referenced this issue Feb 11, 2022
github-actions bot pushed a commit that referenced this issue Feb 11, 2022
# [1.6.0](https://github.com/Jaspero/schema-forms/compare/@jaspero/[email protected]...@jaspero/[email protected]) (2022-02-11)

### Bug Fixes

* better naming conventions for uploaded files [#77](#77) ([44c3a9d](44c3a9d))

### Features

* refactored all fields to use new save method approach ([6e20934](6e20934))
* **form-builder:** processing method on file component working 🎉 [#77](#77) ([af89035](af89035))
github-actions bot pushed a commit that referenced this issue Feb 11, 2022
# [5.4.0](https://github.com/Jaspero/schema-forms/compare/@jaspero/[email protected]...@jaspero/[email protected]) (2022-02-11)

### Bug Fixes

* better naming conventions for uploaded files [#77](#77) ([44c3a9d](44c3a9d))

### Features

* **page-builder:** connected save methods [#77](#77) ([b8a1730](b8a1730))
* refactored all fields to use new save method approach ([6e20934](6e20934))
* **form-builder:** forwarding formId and parentForm to segments and fields ([918454d](918454d))
* **form-builder:** processing method on file component working 🎉 [#77](#77) ([af89035](af89035))
* **form-builder:** refactored save method [#77](#77) ([fc8ca71](fc8ca71))
github-actions bot pushed a commit that referenced this issue Feb 11, 2022
# [3.21.0](https://github.com/Jaspero/schema-forms/compare/@jaspero/[email protected]...@jaspero/[email protected]) (2022-02-11)

### Bug Fixes

* better naming conventions for uploaded files [#77](#77) ([44c3a9d](44c3a9d))

### Features

* **page-builder:** added img-preview directive for rendering preview when controls value is a File [#77](#77) ([d79319a](d79319a))
* **page-builder:** connected save methods [#77](#77) ([b8a1730](b8a1730))
* refactored all fields to use new save method approach ([6e20934](6e20934))
* **form-builder:** forwarding formId and parentForm to segments and fields ([918454d](918454d))
* **form-builder:** processing method on file component working 🎉 [#77](#77) ([af89035](af89035))
* **form-builder:** refactored save method [#77](#77) ([fc8ca71](fc8ca71))
github-actions bot pushed a commit that referenced this issue Feb 11, 2022
# [2.6.0](https://github.com/Jaspero/schema-forms/compare/@jaspero/[email protected]...@jaspero/[email protected]) (2022-02-11)

### Bug Fixes

* better naming conventions for uploaded files [#77](#77) ([44c3a9d](44c3a9d))

### Features

* refactored all fields to use new save method approach ([6e20934](6e20934))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request form-builder
Projects
None yet
Development

No branches or pull requests

2 participants