Commit 206bc4b0 authored by micha's avatar micha
Browse files

progress

parent 9a3f9ce1
import { Component } from '@angular/core';
import { combineLatest, concat, Observable, Subject } from 'rxjs';
import { filter, map, shareReplay, take, withLatestFrom } from 'rxjs/operators';
import { BlogBasicService, mergeListsAndItems } from 'shared';
import { BlogBasicService, toBlogPosts } from 'shared';
@Component({
selector: 'solution-opt-in-updates-basic',
......@@ -42,7 +42,7 @@ export class SolutionOptInUpdatesV1Component {
this.listService.posts$.pipe(filter(l => !!l.length)),
this.listService.comments$.pipe(filter(l => !!l.length))
]).pipe(
map(([list, items]) => mergeListsAndItems(list, items)),
map(([list, items]) => toBlogPosts(list, items)),
shareReplay(1)
);
......
import { Component } from '@angular/core';
import { combineLatest, of, Subject } from 'rxjs';
import { filter, map, shareReplay } from 'rxjs/operators';
import { BlogBasicService, mergeListsAndItems } from 'shared';
import { BlogBasicService, toBlogPosts } from 'shared';
@Component({
selector: 'opt-in-updates',
......@@ -43,7 +43,7 @@ export class StartOptInUpdatesV1Component {
this.listService.posts$.pipe(filter(l => !!l.length)),
this.listService.comments$.pipe(filter(l => !!l.length))
]).pipe(
map(([posts, comments]) => mergeListsAndItems(posts, comments)),
map(([posts, comments]) => toBlogPosts(posts, comments)),
shareReplay(1)
);
......
import { Component } from '@angular/core';
import { combineLatest, Observable, zip } from 'rxjs';
import { filter, map, shareReplay, tap } from 'rxjs/operators';
import { BlogBasicService, BlogPost, mergeListsAndItems } from 'shared';
import { BlogBasicService, BlogPost, toBlogPosts } from 'shared';
@Component({
selector: 'solution-zip',
......@@ -61,7 +61,7 @@ export class SolutionZipComponent {
filter(l => !!l.length)
)
]).pipe(
map(([list, items]) => mergeListsAndItems(list, items)),
map(([list, items]) => toBlogPosts(list, items)),
tap(v => ++this.numProcessJoinedList),
shareReplay(1)
);
......
import { Component } from '@angular/core';
import { combineLatest, Observable, } from 'rxjs';
import { map, tap } from 'rxjs/operators';
import { BlogBasicService, BlogPost, mergeListsAndItems } from 'shared';
import { BlogBasicService, BlogPost, toBlogPosts } from 'shared';
@Component({
selector: 'zip',
......@@ -58,7 +58,7 @@ export class StartZipComponent {
this.listService.posts$,
this.listService.comments$
]).pipe(
map(([list, items]) => mergeListsAndItems(list, items)),
map(([list, items]) => toBlogPosts(list, items)),
tap(v => ++this.numProcessJoinedList)
);
commentedIds$ = this.blog$.pipe(map(list => list
......
......@@ -24,8 +24,8 @@ export class BlogBasicService {
comments: [] as Comment[]
});
posts$ = this.state$.pipe(map(s => s.posts));
comments$ = this.state$.pipe(map(s => s.comments));
readonly posts$ = this.state$.pipe(map(s => s.posts));
readonly comments$ = this.state$.pipe(map(s => s.comments));
constructor(private http: HttpClient) {
......@@ -34,8 +34,7 @@ export class BlogBasicService {
fetchPosts() {
this.httpGetPosts()
.subscribe(posts => {
console.log('upsert:', upsertEntities(this.state$.getValue().posts, posts, 'id'));
this.state$.next({
this.state$.next({
...this.state$.getValue(),
posts: upsertEntities(this.state$.getValue().posts, posts, 'id')
});
......@@ -48,12 +47,12 @@ export class BlogBasicService {
);
}
httpPostComment(item: Pick<Comment, 'text' | 'postId'>): Observable<Comment[]> {
return this.http.post<Comment[]>(this.commentUrl, item);
httpPostComment(item: Pick<Comment, 'text' | 'postId'>): Observable<Comment> {
return this.http.post<Comment>(this.commentUrl, item);
}
httpPostPost(post: Pick<Post, 'title'>): Observable<Post[]> {
return this.http.post<Post[]>(this.postUrl, post);
httpPostPost(post: Pick<Post, 'title'>): Observable<Post> {
return this.http.post<Post>(this.postUrl, post);
}
addPost(post: Pick<Post, 'title'>) {
......@@ -74,17 +73,17 @@ export class BlogBasicService {
fetchComments() {
this.httpGetComments()
.subscribe(comments => {
.subscribe(comment => {
this.state$.next({
...this.state$.getValue(),
comments: upsertEntities(this.state$.getValue().comments, comments, 'id')
comments: upsertEntities(this.state$.getValue().comments, [comment], 'id')
});
});
}
httpGetComments(): Observable<Comment[]> {
return this.http.get<Comment[]>(this.commentUrl).pipe(
catchError(() => of([] as Comment[]))
httpGetComments(): Observable<Comment> {
return this.http.get<Comment>(this.commentUrl).pipe(
catchError(() => of({} as Comment))
);
}
}
import { BlogPost, Comment, Post } from '../models';
export function mergeListsAndItems(posts: Post[], comments: Comment[]): BlogPost[] {
export function toBlogPosts(posts: Post[], comments: Comment[]): BlogPost[] {
return posts.map(({ id, title}) => {
const cmts = comments.filter(comment => comment.postId === id);
return {
......@@ -14,7 +14,7 @@ export function mergeListsAndItems(posts: Post[], comments: Comment[]): BlogPost
});
}
export function upsertEntities<T>(oldEntities: T[], newEntities: T[], id: string): T[] {
export function upsertEntities<T>(oldEntities: T[], newEntities: T[], id: keyof T): T[] {
const insertLists = newEntities.filter(nL => !oldEntities.find(oL => oL[id] === nL[id]));
const updateLists = newEntities.filter(nL => oldEntities.find(oL => oL[id] === nL[id]));
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment