ngOnInit()

in web/frontend/src/app/pages/streams/modules/schema-editor/components/field-properties/field-properties.component.ts [63:229]


  ngOnInit() {
    this.isWriter$ = this.permissionsService.isWriter();

    const uniqueNameValidator = uniqueName(
      combineLatest([
        this.appStore.pipe(select(getSelectedSchemaItemAllFields)),
        this.field(),
      ]).pipe(
        take(1),
        map(([items, field]) =>
          items.filter((item) => item._props._uuid !== field._props._uuid).map((item) => item.name),
        ),
      ),
    );

    this.formGroup = this.fb.group({
      name: [
        null,
        [Validators.required, Validators.pattern(FIELD_NAME_PATTER_REGEXP)],
        uniqueNameValidator,
      ],
      static: null,
      description: null,
      relativeTo: null,
      value: [null, this.staticFieldValidator()],
      title: null,
      type: this.fb.group({
        name: null,
        encoding: null,
        nullable: null,
        types: [null, this.typesFieldValidator()],
        elementType: this.fb.group({
          name: null,
          encoding: null,
          nullable: null,
          types: null,
        }),
      }),
    });

    this.isWriter$.pipe(takeUntil(this.destroy$)).subscribe((isWriter) => {
      if (isWriter) {
        this.formGroup.enable({emitEvent: false});
      } else {
        this.formGroup.disable({emitEvent: false});
      }
    });

    this.fieldPropertiesFormFieldsService
      .onStaticValueChange()
      .pipe(takeUntil(this.destroy$))
      .subscribe((staticValueField) => {
        const getValue = (entry) => (typeof entry === 'object' ? entry.value : entry);
        if (
          staticValueField?.values &&
          !staticValueField.values.map(getValue).includes(this.formGroup.get('value').value)
        ) {
          this.formGroup
            .get('value')
            .patchValue(getValue(staticValueField.values[0]), {emitEvent: false});
          this.updateStoreValueFromForm();
        }
      });

    this.formGroup.valueChanges
      .pipe(
        takeUntil(this.destroy$),
        distinctUntilChanged((c, p) => JSON.stringify(c) === JSON.stringify(p)),
        debounceTime(150),
        switchMap(formaData => {
          this.schemaEditorService.editedFieldNames.add(formaData.name);
          return this.checkRelativeTo();
        }),
      )
      .subscribe(() => this.updateStoreValueFromForm());

    merge(this.formGroup.get('type').valueChanges, this.formGroup.get('static').valueChanges)
      .pipe(takeUntil(this.destroy$))
      .subscribe(() => this.formGroup.get('value').markAsDirty());

    ['type', 'type.elementType'].forEach((formPath) => {
      combineLatest([
        this.formGroup.get(`${formPath}.name`).valueChanges,
        this.appStore.pipe(
          select(getDefaultsTypes),
          filter((t) => !!t),
        ),
      ])
        .pipe(
          distinctUntilChanged((p, c) => JSON.stringify(p) === JSON.stringify(c)),
          takeUntil(this.destroy$),
        )
        .subscribe(([typeName, defaultTypes]) => {
          const encodings = defaultTypes
            .filter((t) => t.name === typeName && t.encoding)
            .map((_type) => _type.encoding);
          this.formGroup
            .get(`${formPath}.encoding`)
            .patchValue(encodings[0] || null, {emitEvent: false});
          this.updateStoreValueFromForm();
        });
    });

    this.formGroup
      .get('type')
      .valueChanges.pipe(takeUntil(this.destroy$))
      .subscribe((data) => {
        this.fillEmptyElementType();
      });

    this.formGroup
      .get('static')
      .valueChanges.pipe(takeUntil(this.destroy$))
      .subscribe((staticValue) => {
        if (!staticValue) {
          this.formGroup.get('value').patchValue(null, {emitEvent: false});
          this.updateStoreValueFromForm();
        }

        if (staticValue && ['ARRAY', 'OBJECT'].includes(this.formGroup.get('type.name').value)) {
          this.formGroup.get('type').patchValue(this.preferTypeWithoutArray(), {emitEvent: false});
          this.fillEmptyElementType();
          this.updateStoreValueFromForm();
        }
      });

    this.formGroup
      .get('type')
      .valueChanges.pipe(takeUntil(this.destroy$))
      .subscribe((type) => {
        SeFormPreferencesService.preferType = {
          ...type,
          elementType: type.name === 'ARRAY' ? type.elementType : null,
        };
      });

    const selectedFieldChanged$: Observable<SchemaClassFieldModel> = this.appStore.pipe(
      select(getSelectedFieldProps),
      filter((f) => !!f),
    );

    selectedFieldChanged$
      .pipe(takeUntil(this.destroy$))
      .subscribe((field) => this.resetFormFormField(field));

    selectedFieldChanged$
      .pipe(
        takeUntil(this.destroy$),
        distinctUntilChanged((p, c) => p._props._uuid === c._props._uuid),
        startWith(null),
      )
      .subscribe(() => {
        this.formGroup.markAsPristine();
        this.seFieldFormsService.formGroupChange(this.formGroup);
      });

    this.seFieldFormsService
      .onRevertForm()
      .pipe(takeUntil(this.destroy$))
      .subscribe((data) => {
        this.resetFormFormField(data);
        this.formGroup.markAsPristine();
        this.updateStoreValueFromForm();
      });

    this.fields$ = this.fieldPropertiesFormFieldsService.fields();
  }