diff --git a/apps/demo/src/app/app.component.html b/apps/demo/src/app/app.component.html
index 86ad36f6..48f92ed1 100644
--- a/apps/demo/src/app/app.component.html
+++ b/apps/demo/src/app/app.component.html
@@ -30,8 +30,10 @@
withImmutableState
withFeatureFactory
withConditional
- withMutation
- rxMutation (without Store)
+ withMutation
+ mutations (without Store)
withEntityResources
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 ef59b121..94a1702d 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 7b0cd1e3..a1cd95b0 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)
-
-
+
-
+
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 ca3d4729..5a14c144 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 728132d6..6f75b920 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 bc77d7de..88218f66 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 cb4a2d26..a8b56d2f 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 899353b5..bdbe616d 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 d74bd3e5..c118f00f 100644
--- a/apps/demo/src/app/lazy-routes.ts
+++ b/apps/demo/src/app/lazy-routes.ts
@@ -64,17 +64,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,
),
},
{