Remove nested subscribtion, move paragraph logic to new service
This commit is contained in:
parent
6cda24fb78
commit
43ad86cf21
@ -1,4 +1,4 @@
|
||||
export interface LabelData {
|
||||
para_id: number;
|
||||
id: number;
|
||||
label: string;
|
||||
}
|
||||
|
59
frontend/src/app/_services/paragraph.service.ts
Normal file
59
frontend/src/app/_services/paragraph.service.ts
Normal 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}`);
|
||||
// }
|
||||
}
|
@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -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,15 +24,16 @@ export class DiscussionViewerComponent implements OnInit {
|
||||
) {}
|
||||
|
||||
ngOnInit(): void {
|
||||
this.paramSub = this.activatedRouter.params.subscribe((params) => {
|
||||
this.id = params.id;
|
||||
this.discussionSub = this.getDiscussionService
|
||||
.getDiscussion(this.id)
|
||||
.subscribe((res) => {
|
||||
this.data = res.posts;
|
||||
console.log(this.data);
|
||||
});
|
||||
});
|
||||
this.subscription = this.activatedRouter.params
|
||||
.pipe(
|
||||
concatMap((params) => {
|
||||
this.id = params.id;
|
||||
return this.getDiscussionService.getDiscussion(this.id);
|
||||
})
|
||||
)
|
||||
.subscribe((result) => {
|
||||
this.data = result.posts;
|
||||
});
|
||||
}
|
||||
|
||||
onBackButtonClick() {
|
||||
|
@ -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 {}
|
||||
|
@ -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) {}
|
||||
}
|
||||
|
@ -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,
|
||||
|
Loading…
Reference in New Issue
Block a user