/
albums.store.ts
46 lines (45 loc) · 1.33 KB
/
albums.store.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
import { inject } from '@angular/core';
import { exhaustMap, pipe, tap } from 'rxjs';
import { patchState, signalStore, withMethods } from '@ngrx/signals';
import { rxMethod } from '@ngrx/signals/rxjs-interop';
import {
setAllEntities,
setEntity,
withEntities,
} from '@ngrx/signals/entities';
import { tapResponse } from '@ngrx/operators';
import {
setError,
setFulfilled,
setPending,
withRequestStatus,
} from '@/shared/state/request-status.feature';
import { Album } from '@/albums/album.model';
import { AlbumsService } from '@/albums/albums.service';
export const AlbumsStore = signalStore(
{ providedIn: 'root' },
withEntities<Album>(),
withRequestStatus(),
withMethods((store, albumsService = inject(AlbumsService)) => ({
setAlbum(album: Album): void {
patchState(store, setEntity(album));
},
loadAllAlbums: rxMethod<void>(
pipe(
tap(() => patchState(store, setPending())),
exhaustMap(() => {
return albumsService.getAll().pipe(
tapResponse({
next: (albums) => {
patchState(store, setAllEntities(albums), setFulfilled());
},
error: (error: { message: string }) => {
patchState(store, setError(error.message));
},
}),
);
}),
),
),
})),
);