diff --git a/apps/demo/src/app/app.component.html b/apps/demo/src/app/app.component.html index a18faa3..97f8054 100644 --- a/apps/demo/src/app/app.component.html +++ b/apps/demo/src/app/app.component.html @@ -26,8 +26,10 @@ withImmutableState withFeatureFactory withConditional - withMutation - rxMutation (without Store) + withMutation + mutations (without Store) diff --git a/apps/demo/src/app/counter-rx-mutation/counter-rx-mutation.css b/apps/demo/src/app/counter-mutation-functions/counter-mutation-functions.css similarity index 98% rename from apps/demo/src/app/counter-rx-mutation/counter-rx-mutation.css rename to apps/demo/src/app/counter-mutation-functions/counter-mutation-functions.css index ef59b12..94a1702 100644 --- a/apps/demo/src/app/counter-rx-mutation/counter-rx-mutation.css +++ b/apps/demo/src/app/counter-mutation-functions/counter-mutation-functions.css @@ -20,7 +20,7 @@ font-size: 0.9rem; } -.section { +section { margin: 2rem 0; padding: 1.5rem; border: 1px solid #e0e0e0; @@ -28,7 +28,7 @@ background-color: #fafafa; } -.section h2 { +section h2 { margin-top: 0; color: #333; border-bottom: 2px solid #007acc; diff --git a/apps/demo/src/app/counter-rx-mutation/counter-rx-mutation.html b/apps/demo/src/app/counter-mutation-functions/counter-mutation-functions.html similarity index 74% rename from apps/demo/src/app/counter-rx-mutation/counter-rx-mutation.html rename to apps/demo/src/app/counter-mutation-functions/counter-mutation-functions.html index 7b0cd1e..a1cd95b 100644 --- a/apps/demo/src/app/counter-rx-mutation/counter-rx-mutation.html +++ b/apps/demo/src/app/counter-mutation-functions/counter-mutation-functions.html @@ -2,7 +2,7 @@ rxMutation + httpMutation (without Store) {{ counter() }} - + Local Increment (rxMutation) isPending: {{ isPending() }} @@ -13,16 +13,16 @@ Local Increment (rxMutation) - + @if (isPending()) { Incrementing... } @else { Increment by 1 } - + @if (isPending()) { Incrementing... } @else { Increment by 13 } - + - + Send to Server (httpMutation) @@ -33,8 +33,12 @@ Send to Server (httpMutation) - + @if (saveIsPending()) { Sending to Server... } @else { Send to Server } - + diff --git a/apps/demo/src/app/counter-rx-mutation/counter-rx-mutation.ts b/apps/demo/src/app/counter-mutation-functions/counter-mutation-functions.ts similarity index 82% rename from apps/demo/src/app/counter-rx-mutation/counter-rx-mutation.ts rename to apps/demo/src/app/counter-mutation-functions/counter-mutation-functions.ts index ca3d472..5a14c14 100644 --- a/apps/demo/src/app/counter-rx-mutation/counter-rx-mutation.ts +++ b/apps/demo/src/app/counter-mutation-functions/counter-mutation-functions.ts @@ -3,7 +3,7 @@ import { httpMutation, rxMutation, } from '@angular-architects/ngrx-toolkit'; -import { CommonModule } from '@angular/common'; +import { JsonPipe } from '@angular/common'; import { Component, computed, signal } from '@angular/core'; import { delay, Observable, of, throwError } from 'rxjs'; @@ -16,16 +16,13 @@ export type CounterResponse = { json: { counter: number }; }; -// TODO - rename this file to just be `mutations-functions-standalone` + class/selector etc?? -// And then the other folder to "store" -// Or maybe put these all in one folder too while we are at it? @Component({ - selector: 'demo-counter-rx-mutation', - imports: [CommonModule], - templateUrl: './counter-rx-mutation.html', - styleUrl: './counter-rx-mutation.css', + selector: 'demo-counter-mutation-functions', + imports: [JsonPipe], + templateUrl: './counter-mutation-functions.html', + styleUrl: './counter-mutation-functions.css', }) -export class CounterRxMutation { +export class CounterRxMutationFunctions { private counterSignal = signal(0); private increment = rxMutation({ @@ -41,13 +38,13 @@ export class CounterRxMutation { }, }); - private saveToServer = httpMutation({ - request: (p) => ({ + private saveToServer = httpMutation({ + request: (p: Params) => ({ url: `https://httpbin.org/post`, method: 'POST', body: { counter: p.value }, - headers: { 'Content-Type': 'application/json' }, }), + parse: (response) => response as CounterResponse, onSuccess: (response) => { console.log('Counter sent to server:', response); }, diff --git a/apps/demo/src/app/counter-mutation/counter-mutation.css b/apps/demo/src/app/counter-mutation-store/counter-mutation-store.css similarity index 98% rename from apps/demo/src/app/counter-mutation/counter-mutation.css rename to apps/demo/src/app/counter-mutation-store/counter-mutation-store.css index 728132d..6f75b92 100644 --- a/apps/demo/src/app/counter-mutation/counter-mutation.css +++ b/apps/demo/src/app/counter-mutation-store/counter-mutation-store.css @@ -20,7 +20,7 @@ font-size: 0.9rem; } -.section { +section { margin: 2rem 0; padding: 1.5rem; border: 1px solid #e0e0e0; @@ -28,7 +28,7 @@ background-color: #fafafa; } -.section h2 { +section h2 { margin-top: 0; color: #333; border-bottom: 2px solid #007acc; diff --git a/apps/demo/src/app/counter-mutation/counter-mutation.html b/apps/demo/src/app/counter-mutation-store/counter-mutation-store.html similarity index 77% rename from apps/demo/src/app/counter-mutation/counter-mutation.html rename to apps/demo/src/app/counter-mutation-store/counter-mutation-store.html index bc77d7d..88218f6 100644 --- a/apps/demo/src/app/counter-mutation/counter-mutation.html +++ b/apps/demo/src/app/counter-mutation-store/counter-mutation-store.html @@ -2,7 +2,7 @@ withMutations + HTTP {{ counter() }} - + Local Increment isPending: {{ isPending() }} @@ -11,13 +11,13 @@ Local Increment - + @if (isPending()) { Incrementing... } @else { Increment } - + - + Sending to Server @@ -28,8 +28,8 @@ Sending to Server - + @if (saveIsPending()) { Sending to Server... } @else { Sending to Server } - + diff --git a/apps/demo/src/app/counter-mutation/counter-mutation.ts b/apps/demo/src/app/counter-mutation-store/counter-mutation-store.ts similarity index 74% rename from apps/demo/src/app/counter-mutation/counter-mutation.ts rename to apps/demo/src/app/counter-mutation-store/counter-mutation-store.ts index cb4a2d2..a8b56d2 100644 --- a/apps/demo/src/app/counter-mutation/counter-mutation.ts +++ b/apps/demo/src/app/counter-mutation-store/counter-mutation-store.ts @@ -1,14 +1,14 @@ -import { CommonModule } from '@angular/common'; +import { JsonPipe } from '@angular/common'; import { Component, inject } from '@angular/core'; import { CounterStore } from './counter.store'; @Component({ - selector: 'demo-counter-mutation', - imports: [CommonModule], - templateUrl: './counter-mutation.html', - styleUrl: './counter-mutation.css', + selector: 'demo-counter-mutation-store', + imports: [JsonPipe], + templateUrl: './counter-mutation-store.html', + styleUrl: './counter-mutation-store.css', }) -export class CounterMutation { +export class CounterMutationStore { private store = inject(CounterStore); protected counter = this.store.counter; diff --git a/apps/demo/src/app/counter-mutation/counter.store.ts b/apps/demo/src/app/counter-mutation-store/counter.store.ts similarity index 68% rename from apps/demo/src/app/counter-mutation/counter.store.ts rename to apps/demo/src/app/counter-mutation-store/counter.store.ts index 899353b..bdbe616 100644 --- a/apps/demo/src/app/counter-mutation/counter.store.ts +++ b/apps/demo/src/app/counter-mutation-store/counter.store.ts @@ -36,14 +36,13 @@ export const CounterStore = signalStore( console.error('Error occurred:', error); }, }), - saveToServer: httpMutation({ - request: () => ({ + saveToServer: httpMutation({ + request: (_: void) => ({ url: `https://httpbin.org/post`, method: 'POST', body: { counter: store.counter() }, - headers: { 'Content-Type': 'application/json' }, }), - onSuccess: (response) => { + onSuccess: (response: CounterResponse) => { console.log('Counter sent to server:', response); patchState(store, { lastResponse: response.json }); }, @@ -54,24 +53,26 @@ export const CounterStore = signalStore( })), ); +// For demo purposes, helps ensures we fail on the first time we hit 7 or 13 let error = false; -function createSumObservable(a: number, b: number): Observable { - return new Observable((subscriber) => { - const result = a + b; - - if ((result === 7 || result === 13) && !error) { - subscriber.error({ message: 'error due to bad luck!', result }); - error = true; - } else { - subscriber.next(result); - error = false; - } - subscriber.complete(); - }); -} - +/** + * @description return of(a + b) + */ function calcSum(a: number, b: number): Observable { - // return of(a + b); + function createSumObservable(a: number, b: number): Observable { + return new Observable((subscriber) => { + const result = a + b; + + if ((result === 7 || result === 13) && !error) { + subscriber.error({ message: 'error due to bad luck!', result }); + error = true; + } else { + subscriber.next(result); + error = false; + } + subscriber.complete(); + }); + } return createSumObservable(a, b).pipe(delay(500)); } diff --git a/apps/demo/src/app/lazy-routes.ts b/apps/demo/src/app/lazy-routes.ts index ba4c66e..25a2625 100644 --- a/apps/demo/src/app/lazy-routes.ts +++ b/apps/demo/src/app/lazy-routes.ts @@ -62,17 +62,17 @@ export const lazyRoutes: Route[] = [ ), }, { - path: 'mutation', + path: 'mutation-store', loadComponent: () => - import('./counter-mutation/counter-mutation').then( - (m) => m.CounterMutation, + import('./counter-mutation-store/counter-mutation-store').then( + (m) => m.CounterMutationStore, ), }, { - path: 'rx-mutation', + path: 'mutation-functions', loadComponent: () => - import('./counter-rx-mutation/counter-rx-mutation').then( - (m) => m.CounterRxMutation, + import('./counter-mutation-functions/counter-mutation-functions').then( + (m) => m.CounterRxMutationFunctions, ), }, ];