Skip to content

Commit

Permalink
CB-3464 refactor: add formate stage for form (#2266)
Browse files Browse the repository at this point in the history
  • Loading branch information
Wroud authored Jan 3, 2024
1 parent 7db7049 commit 027b986
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 1 deletion.
2 changes: 2 additions & 0 deletions webapp/packages/core-ui/src/Form/FormBaseService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ export class FormBaseService<TState, TProps extends IFormProps<TState> = IFormPr
readonly onConfigure: IExecutorHandlersCollection<IFormState<TState>>;
readonly onFillDefaultConfig: IExecutorHandlersCollection<IFormState<TState>>;
readonly onPrepareConfig: IExecutorHandlersCollection<TState>;
readonly onFormat: IExecutorHandlersCollection<IFormState<TState>>;
readonly onValidate: IExecutorHandlersCollection<IFormState<TState>>;
readonly onSubmit: IExecutorHandlersCollection<IFormState<TState>>;
readonly onState: IExecutorHandlersCollection<TState>;
Expand All @@ -33,6 +34,7 @@ export class FormBaseService<TState, TProps extends IFormProps<TState> = IFormPr
this.onConfigure = new ExecutorHandlersCollection();
this.onFillDefaultConfig = new ExecutorHandlersCollection();
this.onPrepareConfig = new ExecutorHandlersCollection();
this.onFormat = new ExecutorHandlersCollection();
this.onValidate = new ExecutorHandlersCollection();
this.onSubmit = new ExecutorHandlersCollection();
this.onState = new ExecutorHandlersCollection();
Expand Down
2 changes: 2 additions & 0 deletions webapp/packages/core-ui/src/Form/FormPart.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ export abstract class FormPart<TPartState, TFormState = any> implements IFormPar

this.formState.submitTask.addHandler(executorHandlerFilter(() => this.isLoaded(), this.save.bind(this)));
this.formState.configureTask.addHandler(executorHandlerFilter(() => this.isLoaded(), this.configure.bind(this)));
this.formState.formatTask.addHandler(executorHandlerFilter(() => this.isLoaded(), this.format.bind(this)));
this.formState.validationTask.addHandler(executorHandlerFilter(() => this.isLoaded(), this.validate.bind(this)));

makeObservable<this, 'loaded' | 'loading' | 'setInitialState'>(this, {
Expand Down Expand Up @@ -141,6 +142,7 @@ export abstract class FormPart<TPartState, TFormState = any> implements IFormPar
}

protected configure(data: IFormState<TFormState>, contexts: IExecutionContextProvider<IFormState<TFormState>>): void | Promise<void> {}
protected format(data: IFormState<TFormState>, contexts: IExecutionContextProvider<IFormState<TFormState>>): void | Promise<void> {}
protected validate(data: IFormState<TFormState>, contexts: IExecutionContextProvider<IFormState<TFormState>>): void | Promise<void> {}

protected abstract loader(): Promise<void>;
Expand Down
6 changes: 5 additions & 1 deletion webapp/packages/core-ui/src/Form/FormState.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ export class FormState<TState> implements IFormState<TState> {
readonly formStateTask: IExecutor<TState>;
readonly fillDefaultConfigTask: IExecutor<IFormState<TState>>;
readonly submitTask: IExecutor<IFormState<TState>>;
readonly formatTask: IExecutor<IFormState<TState>>;
readonly validationTask: IExecutor<IFormState<TState>>;

constructor(app: App, service: FormBaseService<TState, any>, state: TState) {
Expand Down Expand Up @@ -67,8 +68,11 @@ export class FormState<TState> implements IFormState<TState> {
this.fillDefaultConfigTask = new Executor(this as IFormState<TState>, () => true);
this.fillDefaultConfigTask.addCollection(service.onFillDefaultConfig).next(this.formStateTask, form => form.state);

this.formatTask = new Executor(this as IFormState<TState>, () => true);
this.formatTask.addCollection(service.onFormat);

this.validationTask = new Executor(this as IFormState<TState>, () => true);
this.validationTask.addCollection(service.onValidate);
this.validationTask.addCollection(service.onValidate).before(this.formatTask);

this.submitTask = new Executor(this as IFormState<TState>, () => true);
this.submitTask.addCollection(service.onSubmit).before(this.validationTask);
Expand Down
1 change: 1 addition & 0 deletions webapp/packages/core-ui/src/Form/IFormState.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ export interface IFormState<TState> extends ILoadableState {
readonly formStateTask: IExecutor<TState>;
readonly fillDefaultConfigTask: IExecutor<IFormState<TState>>;
readonly submitTask: IExecutor<IFormState<TState>>;
readonly formatTask: IExecutor<IFormState<TState>>;
readonly validationTask: IExecutor<IFormState<TState>>;

setMode(mode: FormMode): this;
Expand Down

0 comments on commit 027b986

Please sign in to comment.