Skip to content

Commit

Permalink
update dependency
Browse files Browse the repository at this point in the history
  • Loading branch information
alexcibotari committed Mar 22, 2024
1 parent 5dc98bf commit 8f1253a
Show file tree
Hide file tree
Showing 4 changed files with 92 additions and 1 deletion.
18 changes: 18 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
"@ngrx/effects": "^17.1.0",
"@ngrx/entity": "^17.1.0",
"@ngrx/router-store": "^17.1.0",
"@ngrx/signals": "^17.1.1",
"@ngrx/store": "^17.1.0",
"@ngrx/store-devtools": "^17.1.0",
"@stoplight/elements": "^8.1.0",
Expand Down
72 changes: 72 additions & 0 deletions src/app/core/state/space.store.ts
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 });
},
};
})
);
2 changes: 1 addition & 1 deletion src/app/shared/services/space.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import { Locale } from '../models/locale.model';
import { ObjectUtils } from '@core/utils/object-utils.service';
import { Functions, httpsCallableData } from '@angular/fire/functions';

@Injectable()
@Injectable({ providedIn: 'root' })
export class SpaceService {
constructor(
private firestore: Firestore,
Expand Down

0 comments on commit 8f1253a

Please sign in to comment.