Skip to content

Instantly share code, notes, and snippets.

@splincode
Last active May 16, 2020 15:25
Show Gist options
  • Save splincode/a2ed4b9cb6301353d824b011a20e3adc to your computer and use it in GitHub Desktop.
Save splincode/a2ed4b9cb6301353d824b011a20e3adc to your computer and use it in GitHub Desktop.
interface UserModel {
id: number | null;
username: string | null;
age: number | null;
}
export class UserSetAction {
public static type: string = '[UserSetAction]: action';
constructor(public payload: UserModel) {}
}
export class UserResetAction {
public static type: string = '[UserResetAction]: action';
}
@State<UserModel>({
name: 'userInfo',
defaults: {
id: null,
username: null,
age: null
}
})
@Injectable()
export class UserInfoState {
@Action(UserSetAction)
public set(ctx: UserAction, { payload }: UserSetAction) {
ctx.setState(payload);
}
@Acton(UserResetAction)
public reset(ctx: UserResetAction) {
ctx.reset({ id: null, username: null, age: null });
}
}
@Component()
export class UserComponent {
constructor(private store: Store, private userApi: UserService) {
}
public ngOnInit(): void {
this.userApi.load().subscribe((user: UserModel) => {
this.store.dispatch(new UserSetAction(user));
});
}
public ngOnDestroy() {
this.store.dispatch(new UserResetAction());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment