Remove nested subscribtion, move paragraph logic to new service

This commit is contained in:
Michał Romaszkin 2020-06-25 11:45:44 +02:00
parent 6cda24fb78
commit 43ad86cf21
7 changed files with 78 additions and 36 deletions

View File

@ -1,4 +1,4 @@
export interface LabelData {
para_id: number;
id: number;
label: string;
}

View File

@ -0,0 +1,59 @@
import { Injectable } from '@angular/core';
import { HttpClient } from '@angular/common/http';
import { BehaviorSubject } from 'rxjs';
import { LabelData } from '../_interfaces/labeldata';
@Injectable()
export class ParagraphService {
// private paragraphsBS: BehaviorSubject<LabelData[]> = new BehaviorSubject(
// [] as LabelData[]
// );
private paragraphBS: BehaviorSubject<
Map<number, LabelData[]>
> = new BehaviorSubject(new Map());
constructor(private http: HttpClient) {}
public addParagraph(discussionId: number, paragraph: LabelData) {
const paragraphsMap = this.paragraphBS.getValue();
if (paragraphsMap.has(discussionId)) {
const labelsArray = paragraphsMap.get(discussionId)!;
if (labelsArray.find((p) => p.id === paragraph.id)) {
const updatedParagraphs = labelsArray.map((element) => {
if (element.id === paragraph.id) {
element.label = paragraph.label;
}
return element;
});
}
} else {
paragraphsMap.set(discussionId, new Array<LabelData>(paragraph));
}
this.paragraphBS.next(paragraphsMap);
}
public getParagraphs(discussionId: number) {
console.log(this.paragraphBS.getValue().get(discussionId));
}
// public addParagraph(paragraph: LabelData) {
// const paragraphs = this.paragraphsBS.getValue();
// if (paragraphs.find((p) => p.id === paragraph.id)) {
// const updatedParagraphs = paragraphs.map((element) => {
// if (element.id === paragraph.id) {
// element.label = paragraph.label;
// }
// return element;
// });
// this.paragraphsBS.next(updatedParagraphs);
// } else {
// paragraphs.push(paragraph);
// this.paragraphsBS.next(paragraphs);
// }
// }
// public updateParagraphs(id: number) {
// return this.http.patch(`127.0.0.1:8080/disussions/${id}`);
// }
}

View File

@ -30,20 +30,4 @@ export class SharedDataService {
public getData(): Observable<string> {
return this.dataBS.asObservable();
}
public addParagraph(paragraph: LabelData) {
const paragraphs = this.paragraphsBS.getValue();
if (paragraphs.find((p) => p.para_id === paragraph.para_id)) {
const updatedParagraphs = paragraphs.map((element) => {
if (element.para_id === paragraph.para_id) {
element.label = paragraph.label;
}
return element;
});
this.paragraphsBS.next(updatedParagraphs);
} else {
paragraphs.push(paragraph);
this.paragraphsBS.next(paragraphs);
}
}
}

View File

@ -3,6 +3,7 @@ import { ActivatedRoute, Router } from '@angular/router';
import { GetDiscussionService } from '../../_services/get-discussion.service';
import { PredictedPost } from '../../_interfaces/predictedposts';
import { Subscription } from 'rxjs';
import { concatMap } from 'rxjs/operators';
@Component({
selector: 'app-discussion-viewer',
@ -14,6 +15,7 @@ export class DiscussionViewerComponent implements OnInit {
id: string;
paramSub: Subscription;
discussionSub: Subscription;
subscription: Subscription;
constructor(
private getDiscussionService: GetDiscussionService,
@ -22,14 +24,15 @@ export class DiscussionViewerComponent implements OnInit {
) {}
ngOnInit(): void {
this.paramSub = this.activatedRouter.params.subscribe((params) => {
this.subscription = this.activatedRouter.params
.pipe(
concatMap((params) => {
this.id = params.id;
this.discussionSub = this.getDiscussionService
.getDiscussion(this.id)
.subscribe((res) => {
this.data = res.posts;
console.log(this.data);
});
return this.getDiscussionService.getDiscussion(this.id);
})
)
.subscribe((result) => {
this.data = result.posts;
});
}

View File

@ -7,6 +7,7 @@ import { MainViewRoutingModule } from './main-view-routing.module';
import { DiscussionViewerComponent } from './discussion-viewer/discussion-viewer.component';
import { GetDiscussionService } from '../_services/get-discussion.service';
import { ParagraphService } from '../_services/paragraph.service';
import { StyledParagraphComponent } from './styled-paragraph/styled-paragraph.component';
import {
@ -39,6 +40,6 @@ import {
NbButtonModule,
NbSelectModule,
],
providers: [GetDiscussionService],
providers: [GetDiscussionService, ParagraphService],
})
export class MainViewModule {}

View File

@ -1,5 +1,5 @@
import { Component, Input } from '@angular/core';
import { SharedDataService } from '../../_services/shared-data.service';
import { ParagraphService } from 'src/app/_services/paragraph.service';
@Component({
selector: 'app-styled-paragraph',
@ -19,12 +19,7 @@ export class StyledParagraphComponent {
];
selectedLabel = '';
constructor(private sharedDataService: SharedDataService) {}
constructor(private paragraphService: ParagraphService) {}
fetchLabel(label: any, id: number) {
this.sharedDataService.addParagraph({
para_id: id,
label,
});
}
fetchLabel(label: any, id: number) {}
}

View File

@ -38,7 +38,7 @@
"no-console": [true, "debug", "info", "time", "timeEnd", "trace"],
"no-empty": false,
"no-inferrable-types": [true, "ignore-params"],
"no-non-null-assertion": true,
"no-non-null-assertion": false,
"no-redundant-jsdoc": true,
"no-switch-case-fall-through": true,
"no-var-requires": false,