Skip to content

Commit e43f59b

Browse files
committed
feat(ngrx): store with otter
1 parent e42f945 commit e43f59b

File tree

5 files changed

+318
-280
lines changed

5 files changed

+318
-280
lines changed

apps/showcase/src/components/showcase/tanstack/store/petstore/petstore.actions.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,12 @@ const ACTION_UPDATE_ENTITIES = '[Petstore] update entities';
2424
const ACTION_UPSERT_ENTITIES = '[Petstore] upsert entities';
2525
const ACTION_SET_ENTITIES = '[Petstore] set entities';
2626
const ACTION_FAIL_ENTITIES = '[Petstore] fail entities';
27-
const ACTION_CREATE_ENTITIES_FROM_API = '[Petstore] create entity';
2827

2928
/** Async Actions */
3029
const ACTION_SET_ENTITIES_FROM_API = '[Petstore] set entities from api';
3130
const ACTION_UPDATE_ENTITIES_FROM_API = '[Petstore] update entities from api';
3231
const ACTION_UPSERT_ENTITIES_FROM_API = '[Petstore] upsert entities from api';
32+
const ACTION_CREATE_ENTITIES_FROM_API = '[Petstore] create entity';
3333

3434

3535
/** Action to clear the StateDetails of the store and replace it */

apps/showcase/src/components/showcase/tanstack/tanstack-pres.component.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import { OtterPickerPresComponent } from '../../utilities';
66
import { TanstackService } from './tanstack.service';
77
import { AngularQueryDevtools } from '@tanstack/angular-query-devtools-experimental';
88
import { AsyncPipe, JsonPipe } from '@angular/common';
9+
import { PetstoreStoreModule } from './store/petstore/index';
910

1011

1112
@O3rComponent({ componentType: 'Component' })
@@ -20,7 +21,8 @@ import { AsyncPipe, JsonPipe } from '@angular/common';
2021
NgbPaginationPages,
2122
AngularQueryDevtools,
2223
JsonPipe,
23-
AsyncPipe
24+
AsyncPipe,
25+
PetstoreStoreModule
2426
],
2527
providers: [TanstackService],
2628
templateUrl: './tanstack-pres.template.html',

apps/showcase/src/components/showcase/tanstack/tanstack-pres.template.html

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
<div class="card mb-3">
22
<div class="card-body">
3+
<span>isloading here:</span>
4+
{{ service.isLoading | async}}
5+
<span>was it here?</span>
36
<form>
47
<div class="row gap-3">
58
<label for="pet-name" class="col-xs-3 col-sm-auto col-form-label">Name</label>
@@ -177,6 +180,4 @@
177180
</div>
178181
</div>
179182

180-
{{ service.isLoading | async}}
181-
182183
<angular-query-devtools initialIsOpen />

apps/showcase/src/components/showcase/tanstack/tanstack.service.ts

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ const FILTER_PAG_REGEX = /[^0-9]/g;
1010
// Tanstack
1111
import {injectMutation, injectQuery, injectQueryClient, type QueryClient } from '@tanstack/angular-query-experimental';
1212
import { BackEndService } from './backend.service';
13-
import { setPetstoreEntityFromApi } from './store/petstore/petstore.actions';
13+
import { setPetstoreEntitiesFromApi, setPetstoreEntityFromApi } from './store/petstore/petstore.actions';
1414
import { selectPetstoreStorePendingStatus } from './store/petstore/petstore.selectors';
1515

1616
@Injectable()
@@ -50,7 +50,6 @@ export class TanstackService {
5050

5151
public isLoading = this.store.select(selectPetstoreStorePendingStatus);
5252

53-
5453
/**
5554
* Complete list of pets retrieved from the API
5655
*/
@@ -142,15 +141,26 @@ export class TanstackService {
142141
/** Base URL where the images can be fetched */
143142
public baseUrl = location.href.split('/#', 1)[0];
144143

144+
constructor() {
145+
this.loadPets();
146+
}
147+
145148
private getNextId() {
146149
return this.pets.data().reduce((maxId: number, pet: Pet) => pet.id && pet.id < Number.MAX_SAFE_INTEGER ? Math.max(maxId, pet.id) : maxId, 0) + 1;
147150
}
148151

152+
public loadPets = (/* abortSignal: AbortSignal*/) => {
153+
this.store.dispatch(setPetstoreEntitiesFromApi({call: this.petStoreApi.findPetsByStatus({status: 'available'}/* , {signal: abortSignal}*/).then((pets: Pet[]) =>
154+
pets.filter((p: Pet) => p.category?.name === 'otter').sort((a: Pet, b: Pet) => a.id && b.id && a.id - b.id || 0))}));
155+
};
156+
149157
/**
150158
* Trigger a full reload of the list of pets by calling the API
151159
*/
152-
public async reload() {
153-
await this.queryClient.invalidateQueries({ queryKey: ['findPetsByStatus'] });
160+
public reload() {
161+
// public async reload() {
162+
// await this.queryClient.invalidateQueries({ queryKey: ['findPetsByStatus'] });
163+
this.loadPets();
154164
}
155165

156166

@@ -170,6 +180,7 @@ export class TanstackService {
170180
// this.store.dispatch()
171181
// eslint-disable-next-line @typescript-eslint/naming-convention
172182
this.store.dispatch(setPetstoreEntityFromApi({call: this.petStoreApi.addPet({Pet: pet})}));
183+
// await this.reload();
173184
}
174185

175186
public delete(petToDelete: Pet) {

0 commit comments

Comments
 (0)