I had 3 components inquiry-form
, inquiry-response
and inquiry-summary
and for some conditions, this components can be hide and show, Im using rxjs
subject
to send data between component, this is the scenario:
inquiry-form
- has 2 div
- user enter id number (redirect to inquiry response 1) and it will hide 2 div on inquiry form
inquiry-response
- has 2-div
- change ID button redirect to Inquiry Form 1
- checked checkbox and submit button redirect to Inquiry Summary 2
here I created stackblitz and this is what I had tried:
inquiry-store.service.ts
private hideContainer = new Subject<boolean>();
public hideContainer$ = this.hideContainer.asObservable
();
private summary = new Subject<boolean>();
public summary$ = this.summary.asObservable
();
private checkbox = new Subject<boolean>();
public checkbox$ = this.checkbox.asObservable
();
setHideContainer(data: boolean) {
this.hideContainer.next(data);
}
setSummary(data: boolean) {
this.summary.next(data);
}
setCheckbox(data: boolean) {
this.checkbox.next(data);
}
}
and in each component will subscribe this based on conditon,
inquiry-form.component.ts
ngOnInit() {
this.inquiryStore.hideContainer$.subscribe(istrue => {
this.isHasSummon = istrue;
});
}
onSubmit(): void {
this.inquiryService.getData(data).subscribe((res: any)=> {
this.inquiryStore.setSummon(res);
this.isHasSummon = true;
})
}
inquiry-response.component.ts
ngOnInit() {
this.inquiryStore.hideContainer$.subscribe(isTrue => {
this.showChangeId = isTrue;
});
this.inquiryStore.checkbox$.subscribe(isTrue => {
this.hideCheckbox = isTrue;
})
}
submitSelectedCheckbox() {
this.inquiryStore.setSummary(false)
this.hideCheckbox = true;
}
goToInquiryForm() {
this.inquiryStore.setHideContainer(false);
this.isShowResponse = false;
}
}
I got few problems, where in inquiry response 1, after I click change ID it will redirect to inquiry Form 1, after I click submit button, it will not go to inquiry response 1 again,and if there any better way to do this ?