K
K
Konstantin2020-06-10 21:15:07
Angular
Konstantin, 2020-06-10 21:15:07

How to make the code more understandable - by separating the logic?

In the parent component in ngOninit (), a request is made to receive data, if the data does not arrive, another request is executed next.

Then I pass the response through rxjs operators to form a specific object.

this.route.params
            .pipe(
                pluck('num'),
                tap((num) => {
                    this.peonService1.repositoryModel.findallversionbynum.model.num = num;
                    this.peonService2.repositoryModel.findallversionbynum.model.num = num;
                    this.peonService3.repositoryModel.findallversionbynum.model.num = num;
                }),
                switchMap(() =>
                    parcel.versions.load().pipe(
                        tap((versions) => parcel.versions.set(versions)),
                        map(() => parcel),
                        flatMap((result) => {
                            return result.versions.exist()
                                ? of(result)
                                : premisse.versions.load().pipe(
                                      tap((versions) => premisse.versions.set(versions)),
                                      map(() => premisse),
                                  );
                        }),
                    ),
                ),
            )
            .subscribe((parcelOrPremise: RustParcel | RustPremise) => {
                console.log(parcelOrPremise);
                parcelOrPremise.getByItGeoId();
                this.listChangesCad = parcelOrPremise.versions.get();
            });


As a result, at the output I get an object of one of the types `RustParcel | RustPremise`, each of the objects has its own set of fields and each needs to display its own view.

The question is how to simplify this and make the code more readable, because then you have to do something like this:

public RustParcel: RustParcel;
public RustPremise: RustPremise;

.subscribe((parcelOrPremise: RustParcel | RustPremise) => {
                parcelOrPremise.getByItGeoId();
                this.listChangesCad = parcelOrPremise.versions.get();
               
                if (parcelOrPremise instanceof RustParcel) {
                         this.RustParce = parcelOrPremise;
                }

                  if (parcelOrPremise instanceof RustPremise) {
                         this.RustPremise = parcelOrPremise;
                }
 
            });


And then substitute the components like this:

<app-RustPremise *ngif="RustPremise"></app-RustPremise>
<app-RustParcel *ngif="RustParcel"></app-RustParcel>


How can all this be simplified?

It might be worth creating two beans app-RustPremis and app-RustParce. And in them to call a specific method to receive data? But in this case, more per request will be executed - if the data in another component comes. And also the component will always be shown - even if empty

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question