-
Notifications
You must be signed in to change notification settings - Fork 3.3k
/
profile.state.ts
45 lines (40 loc) · 1.18 KB
/
profile.state.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
import { State, Action, StateContext, Selector } from '@ngxs/store';
import { GetProfile, ChangePassword, UpdateProfile } from '../actions/profile.actions';
import { Profile } from '../models/profile';
import { ProfileService } from '../services/profile.service';
import { tap } from 'rxjs/operators';
@State<Profile.State>({
name: 'ProfileState',
defaults: {} as Profile.State,
})
export class ProfileState {
@Selector()
static getProfile({ profile }: Profile.State): Profile.Response {
return profile;
}
constructor(private profileService: ProfileService) {}
@Action(GetProfile)
profileGet({ patchState }: StateContext<Profile.State>) {
return this.profileService.get().pipe(
tap(profile =>
patchState({
profile,
}),
),
);
}
@Action(UpdateProfile)
profileUpdate({ patchState }: StateContext<Profile.State>, { payload }: UpdateProfile) {
return this.profileService.update(payload).pipe(
tap(profile =>
patchState({
profile,
}),
),
);
}
@Action(ChangePassword)
changePassword(_, { payload }: ChangePassword) {
return this.profileService.changePassword(payload, true);
}
}