mirror of
https://github.com/Raghu-Ch/angular-challenges.git
synced 2026-02-13 14:23:02 -05:00
feat(doc): move pipe bug
This commit is contained in:
58
apps/rxjs/pipe-bug/src/app/localDB.service.ts
Normal file
58
apps/rxjs/pipe-bug/src/app/localDB.service.ts
Normal file
@@ -0,0 +1,58 @@
|
||||
/* eslint-disable @typescript-eslint/member-ordering */
|
||||
import { randomError } from '@angular-challenges/shared/utils';
|
||||
import { Injectable } from '@angular/core';
|
||||
import { ComponentStore } from '@ngrx/component-store';
|
||||
import { of } from 'rxjs';
|
||||
|
||||
export type TopicType = 'food' | 'book' | 'sport';
|
||||
|
||||
interface Info {
|
||||
id: number;
|
||||
topic: TopicType;
|
||||
}
|
||||
|
||||
interface DBState {
|
||||
infos: Info[];
|
||||
}
|
||||
|
||||
const initialState: DBState = {
|
||||
infos: [
|
||||
{ id: 1, topic: 'book' },
|
||||
{ id: 2, topic: 'book' },
|
||||
{ id: 3, topic: 'book' },
|
||||
{ id: 4, topic: 'book' },
|
||||
{ id: 5, topic: 'food' },
|
||||
{ id: 6, topic: 'food' },
|
||||
{ id: 7, topic: 'book' },
|
||||
{ id: 8, topic: 'book' },
|
||||
{ id: 9, topic: 'book' },
|
||||
{ id: 10, topic: 'sport' },
|
||||
],
|
||||
};
|
||||
|
||||
@Injectable({ providedIn: 'root' })
|
||||
export class LocalDBService extends ComponentStore<DBState> {
|
||||
constructor() {
|
||||
super(initialState);
|
||||
}
|
||||
|
||||
infos$ = this.select((state) => state.infos);
|
||||
|
||||
searchByType = (type: TopicType) =>
|
||||
this.select((state) => state.infos.filter((i) => i.topic === type));
|
||||
|
||||
deleteOne = this.updater(
|
||||
(state, id: number): DBState => ({
|
||||
infos: state.infos.filter((i) => i.id !== id),
|
||||
})
|
||||
);
|
||||
|
||||
deleteOneTopic = (id: number) =>
|
||||
randomError({
|
||||
success: () => {
|
||||
this.deleteOne(id);
|
||||
return of(true);
|
||||
},
|
||||
error: () => of(false),
|
||||
});
|
||||
}
|
||||
Reference in New Issue
Block a user