Skip to content

Commit

Permalink
feat: begin defer chapter
Browse files Browse the repository at this point in the history
  • Loading branch information
Nicoss54 committed Jun 18, 2024
1 parent 35072e4 commit 2e5a0b1
Showing 1 changed file with 37 additions and 0 deletions.
37 changes: 37 additions & 0 deletions apps/34-signal/src/app/core/store/store.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import { computed, inject, InjectionToken, makeEnvironmentProviders, signal } from '@angular/core';
import { PeopleService } from '../providers/people.service';
import { People, PeopleForm } from '../../shared/models/people.model';
import { switchMap, tap } from 'rxjs';

const filterPeople = (search: string) => (people: People) =>
people.lastname.toLowerCase().includes(search.toLowerCase()) || people.firstname.toLowerCase().includes(search.toLowerCase());

function appStore(peopleService = inject(PeopleService)) {
const people = signal<People[]>([]);
const search = signal<string>('');

return {
people: computed(() => people().filter(filterPeople(search()))),
search: search.asReadonly(),
setSearch(value: string) {
search.set(value);
},
getPeople() {
return peopleService.getPeople().pipe(tap(data => people.set(data)));
},
deletePeople(id: string) {
return peopleService.deletePeople(id).pipe(tap(data => people.set(data)));
},
addNewPerson(person: PeopleForm) {
return peopleService.addNewPerson(person).pipe(switchMap(() => this.getPeople()));
},
};
}

export const PEOPLE_STORE = new InjectionToken<Store>('StoreToken');

export type Store = ReturnType<typeof appStore>;

export const provideAppStore = () => {
return makeEnvironmentProviders([{ provide: PEOPLE_STORE, useFactory: appStore }]);
};

0 comments on commit 2e5a0b1

Please sign in to comment.