Last active
May 16, 2020 15:25
-
-
Save splincode/a2ed4b9cb6301353d824b011a20e3adc to your computer and use it in GitHub Desktop.
This file contains hidden or 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
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