ngOnInit()

in web/frontend/src/app/pages/streams/modules/schema-editor/components/se-layout/se-layout.component.ts [131:266]


  ngOnInit() {
    this.streamDetails = this.streamDetailsStore.pipe(select(streamsDetailsStateSelector));
    this.activeTab = this.appStore.pipe(select(getActiveOrFirstTab));
    this.getSchemaDiff$ = this.appStore.pipe(select(getSchemaDiff));
    this.defaultDataTypes$ = this.appStore.pipe(select(getDefaultsTypes));
    this.showClassListGrid$ = this.seDataService.showClassListGrid();
    this.isWriter$ = this.permissionsService.isWriter();

    const schemaItemsChanged$ = this.appStore.pipe(
      select(getAllSchemaItems),
      map((data) => this.schemaToString(data)),
      distinctUntilChanged(),
      skipWhile((schema) => !schema),
    );

    this.appStore
      .pipe(
        select(getActiveTab),
        filter((tab: TabModel) => !this.route.snapshot.data.streamCreate && !!tab?.stream),
        switchMap((tab: TabModel) => this.schemaService.getSchema(tab?.stream)))
      .subscribe();

    this.isSchemaEdited$ = this.onSchemaResetState$.pipe(
      switchMap((isCreate) => {
        if (isCreate) {
          return schemaItemsChanged$.pipe(mapTo(true), startWith(false));
        }
        // Wait while first schema come from BE, skip it - and monitor changes removing and updating
        return schemaItemsChanged$.pipe(skip(1), mapTo(true), startWith(false));
      }),
      shareReplay(1),
    );

    const types$ = this.appStore.pipe(select(getAllSchemaItems));

    this.hasSchemaError$ = types$.pipe(
      switchMap((types) => {
        return this.seFieldFormsService.hasAnyError().pipe(
          map((hasError) => {
            const objectTypeFields = types.reduce((acc, type) => [...acc, ...type.fields], []).filter(field => field.type.elementType);
            return hasError || !types.filter((type) => type._props._isUsed).length || 
              objectTypeFields.some(field => !field.type.elementType.types.length);
          }),
        )
      })
    )

    types$.pipe(takeUntil(this.destroy$)).subscribe((types) => {
      this.seFieldFormsService.typesChanged(types);
    });

    this.createStreamBtnClass$ = this.hasSchemaError$.pipe(
      map((hasError) => (hasError ? 'btn-danger' : 'btn-success')),
    );

    this.isSchemaEdited$
      .pipe(
        withLatestFrom(this.appStore.pipe(select(getActiveTabSettings))),
        takeUntil(this.destroy$),
      )
      .subscribe(([isSchemaEdited, activeTabSettings]: [boolean, TabSettingsModel]) => {
        const tabSettings = {...activeTabSettings};
        if (isSchemaEdited) {
          tabSettings._showOnCloseAlerts = isSchemaEdited;
        } else {
          delete tabSettings._showOnCloseAlerts;
        }
        this.appStore.dispatch(new SetTabSettings({tabSettings}));
      });

    this.appStore
      .pipe(select(getOpenNewTabState))
      .subscribe((_openNewTab) => (this.isOpenInNewTab = _openNewTab));
    this.appStore.dispatch(GetDefaultTypes());

    this.appStore
      .pipe(
        select(getActiveTabFilters),
        filter((filter) => !!filter),
        takeUntil(this.destroy$),
      )
      .subscribe((filter: FilterModel) => {
        this.tabFilter = {...filter};
      });

    this.selectedSchemaItem$ = this.appStore.pipe(select(getSelectedSchemaItem));

    this.route.params
      .pipe(
        filter((params: {stream: string; id: string; symbol?: string}) => !!params?.stream),
        tap(() => this.appStore.dispatch(EditSchemaResetState())),
        withLatestFrom(this.route.data),
        takeUntil(this.destroy$),
        switchMap(([params, data]: [{stream: string; id: string; symbol?: string}, Data]) => {
          return this.appStore.pipe(
            select(getActiveTab),
            filter((tabModel: TabModel) => !!tabModel),
            take(1),
            map((tabModel: TabModel) => {
              this.onSchemaResetState$.next(tabModel.streamCreate);
              this.seFieldFormsService.streamChanged();
              return [tabModel, data];
            }),
          );
        }),
        takeUntil(this.destroy$),
      )
      .subscribe(([tabModel, data]: [TabModel, Data]) => {
        if (!tabModel.streamCreate)
          this.streamDetailsStore.dispatch(
            new StreamDetailsActions.GetSymbols({streamId: tabModel.stream}),
          );
        this.streamName = tabModel.stream;
        if (!tabModel.stream) return;
        const tab: TabModel = new TabModel({
          ...tabModel,
          ...data,
          active: true,
        });
        this.currentTab = Object.assign({}, tab);

        this.tabName = tabModel.stream;
        if (tabModel.symbol) {
          this.tabName += tabModel.symbol;
        }

        this.appStore.dispatch(SetStreamId({streamId: tab.stream}));
        if (!tabModel.streamCreate) {
          this.appStore.dispatch(GetSchema());
        }
      });

    this.modalService.onHide
      .pipe(takeUntil(this.destroy$))
      .subscribe(() => this.lastFocusedElement?.focus())
  }