mirror of
https://github.com/Raghu-Ch/angular-challenges.git
synced 2026-02-10 12:53:03 -05:00
feat(challenge11): creation
This commit is contained in:
36
apps/rxjs-pipe-bug/.eslintrc.json
Normal file
36
apps/rxjs-pipe-bug/.eslintrc.json
Normal file
@@ -0,0 +1,36 @@
|
|||||||
|
{
|
||||||
|
"extends": ["../../.eslintrc.json"],
|
||||||
|
"ignorePatterns": ["!**/*"],
|
||||||
|
"overrides": [
|
||||||
|
{
|
||||||
|
"files": ["*.ts"],
|
||||||
|
"extends": [
|
||||||
|
"plugin:@nrwl/nx/angular",
|
||||||
|
"plugin:@angular-eslint/template/process-inline-templates"
|
||||||
|
],
|
||||||
|
"rules": {
|
||||||
|
"@angular-eslint/directive-selector": [
|
||||||
|
"error",
|
||||||
|
{
|
||||||
|
"type": "attribute",
|
||||||
|
"prefix": "app",
|
||||||
|
"style": "camelCase"
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"@angular-eslint/component-selector": [
|
||||||
|
"error",
|
||||||
|
{
|
||||||
|
"type": "element",
|
||||||
|
"prefix": "app",
|
||||||
|
"style": "kebab-case"
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"files": ["*.html"],
|
||||||
|
"extends": ["plugin:@nrwl/nx/angular-template"],
|
||||||
|
"rules": {}
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
35
apps/rxjs-pipe-bug/README.md
Normal file
35
apps/rxjs-pipe-bug/README.md
Normal file
@@ -0,0 +1,35 @@
|
|||||||
|
<h1>Rxjs pipe bug</h1>
|
||||||
|
|
||||||
|
> Author: Thomas Laforge
|
||||||
|
|
||||||
|
<!-- TODO: add Information/Statement/Rules/Constraint/Steps -->
|
||||||
|
|
||||||
|
### Information
|
||||||
|
|
||||||
|
### Statement
|
||||||
|
|
||||||
|
### Step 1
|
||||||
|
|
||||||
|
### Step 2
|
||||||
|
|
||||||
|
### Constraints:
|
||||||
|
|
||||||
|
### Submitting your work
|
||||||
|
|
||||||
|
1. Fork the project
|
||||||
|
2. clone it
|
||||||
|
3. npm install
|
||||||
|
4. **nx serve rxjs-pipe-bug**
|
||||||
|
5. _...work on it_
|
||||||
|
6. Commit your work
|
||||||
|
7. Submit a PR with a title beginning with **Answer:11** that I will review and other dev can review.
|
||||||
|
|
||||||
|
<!-- TODO: add challenge number and project Name -->
|
||||||
|
|
||||||
|
<a href="https://github.com/tomalaforge/angular-challenges/pulls?q=label%3A11+label%3Aanswer"><img src="https://img.shields.io/badge/-Solutions-green" alt="Rxjs pipe bug"/></a>
|
||||||
|
|
||||||
|
<!-- TODO: uncomment when done late -->
|
||||||
|
<!-- <a href='https://github.com/tomalaforge/angular-challenges/pulls?q=label%3A11+label%3A"answer+author"'><img src="https://img.shields.io/badge/-Author solution-important" alt="{Project name} solution author"/></a>
|
||||||
|
<a href="{Blog post url}" target="_blank" rel="noopener noreferrer"><img src="https://img.shields.io/badge/-Blog post explanation-blue" alt="{Project name} blog article"/></a> -->
|
||||||
|
|
||||||
|
_You can ask any question on_ <a href="https://twitter.com/laforge_toma" target="_blank" rel="noopener noreferrer"><img src="./../../logo/twitter.svg" height=20px alt="twitter"/></a>
|
||||||
81
apps/rxjs-pipe-bug/project.json
Normal file
81
apps/rxjs-pipe-bug/project.json
Normal file
@@ -0,0 +1,81 @@
|
|||||||
|
{
|
||||||
|
"name": "rxjs-pipe-bug",
|
||||||
|
"$schema": "../../node_modules/nx/schemas/project-schema.json",
|
||||||
|
"projectType": "application",
|
||||||
|
"sourceRoot": "apps/rxjs-pipe-bug/src",
|
||||||
|
"prefix": "app",
|
||||||
|
"targets": {
|
||||||
|
"build": {
|
||||||
|
"executor": "@angular-devkit/build-angular:browser",
|
||||||
|
"outputs": ["{options.outputPath}"],
|
||||||
|
"options": {
|
||||||
|
"outputPath": "dist/apps/rxjs-pipe-bug",
|
||||||
|
"index": "apps/rxjs-pipe-bug/src/index.html",
|
||||||
|
"main": "apps/rxjs-pipe-bug/src/main.ts",
|
||||||
|
"polyfills": ["zone.js"],
|
||||||
|
"tsConfig": "apps/rxjs-pipe-bug/tsconfig.app.json",
|
||||||
|
"inlineStyleLanguage": "scss",
|
||||||
|
"assets": [
|
||||||
|
"apps/rxjs-pipe-bug/src/favicon.ico",
|
||||||
|
"apps/rxjs-pipe-bug/src/assets"
|
||||||
|
],
|
||||||
|
"styles": ["apps/rxjs-pipe-bug/src/styles.scss"],
|
||||||
|
"scripts": []
|
||||||
|
},
|
||||||
|
"configurations": {
|
||||||
|
"production": {
|
||||||
|
"budgets": [
|
||||||
|
{
|
||||||
|
"type": "initial",
|
||||||
|
"maximumWarning": "500kb",
|
||||||
|
"maximumError": "1mb"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"type": "anyComponentStyle",
|
||||||
|
"maximumWarning": "2kb",
|
||||||
|
"maximumError": "4kb"
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"outputHashing": "all"
|
||||||
|
},
|
||||||
|
"development": {
|
||||||
|
"buildOptimizer": false,
|
||||||
|
"optimization": false,
|
||||||
|
"vendorChunk": true,
|
||||||
|
"extractLicenses": false,
|
||||||
|
"sourceMap": true,
|
||||||
|
"namedChunks": true
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"defaultConfiguration": "production"
|
||||||
|
},
|
||||||
|
"serve": {
|
||||||
|
"executor": "@angular-devkit/build-angular:dev-server",
|
||||||
|
"configurations": {
|
||||||
|
"production": {
|
||||||
|
"browserTarget": "rxjs-pipe-bug:build:production"
|
||||||
|
},
|
||||||
|
"development": {
|
||||||
|
"browserTarget": "rxjs-pipe-bug:build:development"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"defaultConfiguration": "development"
|
||||||
|
},
|
||||||
|
"extract-i18n": {
|
||||||
|
"executor": "@angular-devkit/build-angular:extract-i18n",
|
||||||
|
"options": {
|
||||||
|
"browserTarget": "rxjs-pipe-bug:build"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"lint": {
|
||||||
|
"executor": "@nrwl/linter:eslint",
|
||||||
|
"options": {
|
||||||
|
"lintFilePatterns": [
|
||||||
|
"apps/rxjs-pipe-bug/**/*.ts",
|
||||||
|
"apps/rxjs-pipe-bug/**/*.html"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"tags": []
|
||||||
|
}
|
||||||
56
apps/rxjs-pipe-bug/src/app/app.component.ts
Normal file
56
apps/rxjs-pipe-bug/src/app/app.component.ts
Normal file
@@ -0,0 +1,56 @@
|
|||||||
|
/* eslint-disable @angular-eslint/component-selector */
|
||||||
|
import { AsyncPipe, NgFor } from '@angular/common';
|
||||||
|
import { Component, inject, Input } from '@angular/core';
|
||||||
|
import { BehaviorSubject, take } from 'rxjs';
|
||||||
|
import { AppService } from './app.service';
|
||||||
|
import { TopicType } from './localDB.service';
|
||||||
|
|
||||||
|
@Component({
|
||||||
|
standalone: true,
|
||||||
|
selector: 'button-delete-topic',
|
||||||
|
imports: [AsyncPipe],
|
||||||
|
template: `
|
||||||
|
<button (click)="deleteTopic()"><ng-content></ng-content></button>
|
||||||
|
<div>{{ message$$ | async }}</div>
|
||||||
|
`,
|
||||||
|
})
|
||||||
|
export class ButtonDeleteComponent {
|
||||||
|
@Input() topic!: TopicType;
|
||||||
|
|
||||||
|
message$$ = new BehaviorSubject<string>('');
|
||||||
|
|
||||||
|
private service = inject(AppService);
|
||||||
|
|
||||||
|
deleteTopic() {
|
||||||
|
this.service
|
||||||
|
.deleteOldTopics(this.topic)
|
||||||
|
.pipe(take(1))
|
||||||
|
.subscribe((result) =>
|
||||||
|
this.message$$.next(
|
||||||
|
result
|
||||||
|
? `All ${this.topic} have been deleted`
|
||||||
|
: `Error: deletion of some ${this.topic} failed`
|
||||||
|
)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Component({
|
||||||
|
standalone: true,
|
||||||
|
imports: [AsyncPipe, NgFor, ButtonDeleteComponent],
|
||||||
|
selector: 'app-root',
|
||||||
|
template: `
|
||||||
|
<div *ngFor="let item of all$ | async">
|
||||||
|
{{ item.id }} - {{ item.topic }}
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<button-delete-topic topic="food">Delete Food</button-delete-topic>
|
||||||
|
<button-delete-topic topic="sport">Delete Sport</button-delete-topic>
|
||||||
|
<button-delete-topic topic="book">Delete Book</button-delete-topic>
|
||||||
|
`,
|
||||||
|
})
|
||||||
|
export class AppComponent {
|
||||||
|
private service = inject(AppService);
|
||||||
|
|
||||||
|
all$ = this.service.getAll$;
|
||||||
|
}
|
||||||
23
apps/rxjs-pipe-bug/src/app/app.service.ts
Normal file
23
apps/rxjs-pipe-bug/src/app/app.service.ts
Normal file
@@ -0,0 +1,23 @@
|
|||||||
|
import { inject, Injectable } from '@angular/core';
|
||||||
|
import { merge, mergeMap, Observable, of, take } from 'rxjs';
|
||||||
|
import { LocalDBService, TopicType } from './localDB.service';
|
||||||
|
|
||||||
|
@Injectable({ providedIn: 'root' })
|
||||||
|
export class AppService {
|
||||||
|
private dbService = inject(LocalDBService);
|
||||||
|
|
||||||
|
getAll$ = this.dbService.infos$;
|
||||||
|
|
||||||
|
deleteOldTopics(type: TopicType): Observable<boolean> {
|
||||||
|
return this.dbService.searchByType(type).pipe(
|
||||||
|
take(1),
|
||||||
|
mergeMap((topicToDelete) =>
|
||||||
|
topicToDelete.length > 0
|
||||||
|
? topicToDelete
|
||||||
|
.map((t) => this.dbService.deleteOneTopic(t.id))
|
||||||
|
.reduce((acc, curr) => merge(acc, curr), of(true))
|
||||||
|
: of(true)
|
||||||
|
)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
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 { Injectable } from '@angular/core';
|
||||||
|
import { randNumber } from '@ngneat/falso';
|
||||||
|
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) => {
|
||||||
|
const randomNumber = randNumber({ min: 0.1, max: 1, fraction: 2 });
|
||||||
|
if (randomNumber > 0.5) {
|
||||||
|
this.deleteOne(id);
|
||||||
|
return of(true);
|
||||||
|
}
|
||||||
|
return of(false);
|
||||||
|
};
|
||||||
|
}
|
||||||
0
apps/rxjs-pipe-bug/src/assets/.gitkeep
Normal file
0
apps/rxjs-pipe-bug/src/assets/.gitkeep
Normal file
BIN
apps/rxjs-pipe-bug/src/favicon.ico
Normal file
BIN
apps/rxjs-pipe-bug/src/favicon.ico
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 15 KiB |
13
apps/rxjs-pipe-bug/src/index.html
Normal file
13
apps/rxjs-pipe-bug/src/index.html
Normal file
@@ -0,0 +1,13 @@
|
|||||||
|
<!DOCTYPE html>
|
||||||
|
<html lang="en">
|
||||||
|
<head>
|
||||||
|
<meta charset="utf-8" />
|
||||||
|
<title>RxjsPipeBug</title>
|
||||||
|
<base href="/" />
|
||||||
|
<meta name="viewport" content="width=device-width, initial-scale=1" />
|
||||||
|
<link rel="icon" type="image/x-icon" href="favicon.ico" />
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
<app-root></app-root>
|
||||||
|
</body>
|
||||||
|
</html>
|
||||||
4
apps/rxjs-pipe-bug/src/main.ts
Normal file
4
apps/rxjs-pipe-bug/src/main.ts
Normal file
@@ -0,0 +1,4 @@
|
|||||||
|
import { bootstrapApplication } from '@angular/platform-browser';
|
||||||
|
import { AppComponent } from './app/app.component';
|
||||||
|
|
||||||
|
bootstrapApplication(AppComponent).catch((err) => console.error(err));
|
||||||
1
apps/rxjs-pipe-bug/src/styles.scss
Normal file
1
apps/rxjs-pipe-bug/src/styles.scss
Normal file
@@ -0,0 +1 @@
|
|||||||
|
/* You can add global styles to this file, and also import other style files */
|
||||||
10
apps/rxjs-pipe-bug/tsconfig.app.json
Normal file
10
apps/rxjs-pipe-bug/tsconfig.app.json
Normal file
@@ -0,0 +1,10 @@
|
|||||||
|
{
|
||||||
|
"extends": "./tsconfig.json",
|
||||||
|
"compilerOptions": {
|
||||||
|
"outDir": "../../dist/out-tsc",
|
||||||
|
"types": []
|
||||||
|
},
|
||||||
|
"files": ["src/main.ts"],
|
||||||
|
"include": ["src/**/*.d.ts"],
|
||||||
|
"exclude": ["jest.config.ts", "**/*.test.ts", "**/*.spec.ts"]
|
||||||
|
}
|
||||||
7
apps/rxjs-pipe-bug/tsconfig.editor.json
Normal file
7
apps/rxjs-pipe-bug/tsconfig.editor.json
Normal file
@@ -0,0 +1,7 @@
|
|||||||
|
{
|
||||||
|
"extends": "./tsconfig.json",
|
||||||
|
"include": ["**/*.ts"],
|
||||||
|
"compilerOptions": {
|
||||||
|
"types": []
|
||||||
|
}
|
||||||
|
}
|
||||||
29
apps/rxjs-pipe-bug/tsconfig.json
Normal file
29
apps/rxjs-pipe-bug/tsconfig.json
Normal file
@@ -0,0 +1,29 @@
|
|||||||
|
{
|
||||||
|
"extends": "../../tsconfig.base.json",
|
||||||
|
"files": [],
|
||||||
|
"include": [],
|
||||||
|
"references": [
|
||||||
|
{
|
||||||
|
"path": "./tsconfig.app.json"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"path": "./tsconfig.editor.json"
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"compilerOptions": {
|
||||||
|
"target": "es2022",
|
||||||
|
"useDefineForClassFields": false,
|
||||||
|
"forceConsistentCasingInFileNames": true,
|
||||||
|
"strict": true,
|
||||||
|
"noImplicitOverride": true,
|
||||||
|
"noPropertyAccessFromIndexSignature": true,
|
||||||
|
"noImplicitReturns": true,
|
||||||
|
"noFallthroughCasesInSwitch": true
|
||||||
|
},
|
||||||
|
"angularCompilerOptions": {
|
||||||
|
"enableI18nLegacyMessageIdFormat": false,
|
||||||
|
"strictInjectionParameters": true,
|
||||||
|
"strictInputAccessModifiers": true,
|
||||||
|
"strictTemplates": true
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user