-
-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
5dc98bf
commit 8f1253a
Showing
4 changed files
with
92 additions
and
1 deletion.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
import { inject } from '@angular/core'; | ||
import { patchState, signalStore, withMethods, withState } from '@ngrx/signals'; | ||
import { rxMethod } from '@ngrx/signals/rxjs-interop'; | ||
import { tapResponse } from '@ngrx/operators'; | ||
import { Space } from '@shared/models/space.model'; | ||
import { SpaceService } from '@shared/services/space.service'; | ||
import { pipe, switchMap } from 'rxjs'; | ||
|
||
export type SpaceState = { | ||
spaces: Space[]; | ||
selectedSpace: Space | undefined; | ||
contentPath: PathItem[] | undefined; | ||
assetPath: PathItem[] | undefined; | ||
}; | ||
|
||
export type PathItem = { | ||
fullSlug: string; | ||
name: string; | ||
}; | ||
|
||
const initialState: SpaceState = { | ||
spaces: [], | ||
selectedSpace: undefined, | ||
contentPath: undefined, | ||
assetPath: undefined, | ||
}; | ||
|
||
export const SpaceStore = signalStore( | ||
{ providedIn: 'root' }, | ||
withState(initialState), | ||
withMethods(state => { | ||
const spaceService = inject(SpaceService); | ||
return { | ||
load: rxMethod( | ||
pipe( | ||
switchMap(() => spaceService.findAll()), | ||
tapResponse({ | ||
next: response => { | ||
if (response.length === 0) { | ||
patchState(state, { spaces: [], selectedSpace: undefined, assetPath: undefined, contentPath: undefined }); | ||
} else { | ||
const selectedSpace = state.selectedSpace(); | ||
if (selectedSpace) { | ||
const found = response.find(space => space.id === selectedSpace.id); | ||
if (found) { | ||
patchState(state, { spaces: response, selectedSpace: found }); | ||
} else { | ||
patchState(state, { spaces: response, selectedSpace: response[0], assetPath: undefined, contentPath: undefined }); | ||
} | ||
} else { | ||
patchState(state, { spaces: response, selectedSpace: response[0], assetPath: undefined, contentPath: undefined }); | ||
} | ||
} | ||
}, | ||
error: error => { | ||
console.error('Error loading spaces', error); | ||
}, | ||
}) | ||
) | ||
), | ||
select: (space: Space) => { | ||
patchState(state, { selectedSpace: space, assetPath: undefined, contentPath: undefined }); | ||
}, | ||
changeContentPath: (contentPath: PathItem[]) => { | ||
patchState(state, { contentPath }); | ||
}, | ||
changeAssetPath: (assetPath: PathItem[]) => { | ||
patchState(state, { assetPath }); | ||
}, | ||
}; | ||
}) | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters