Click here to Skip to main content
15,400,433 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I have two list to refresh for two different event (one post, one delete). The issue is that when delete is called, It calls also the post method. So I delete someting, but the post re-post it :p

The lists to refresh are the profileUser$ and the ptofileListDistinct$

What I have tried:

Initialization:

TypeScript
export class UserEditComponent implements OnInit {

  userId: string = "";
  currentUser$!: Observable<User>;

  profileUser$!: Observable<ProfileUser[]>;
  profileListDistinc$!: Observable<Profile[]>;

  constructor(
    private activatedRoute: ActivatedRoute,
    private router: Router,
    private userService: UserService,
    private profileService: ProfileService,
    private reloadProfile: SearchEmitterService,
    private confirmDialogService: ConfirmDialogService
  ) { }

    ngOnInit(): void {
    
    const id = this.activatedRoute.snapshot.paramMap.get('id');
    this.userId = id ? id : "";

    this.currentUser$ = this.userService.getUser(this.userId);

    this.profileUser$ = this.reloadProfile.reloadprofileUserBehavior$.pipe(
      switchMap(
        x => this.profileService.getProfilesForUser(this.userId)));

    this.profileListDistinc$ = this.reloadProfile.reloadProfileListDisctincBehavior$.pipe(
      switchMap(x => this.profileService.getProfilesDisctinc(this.userId)));
  }


Post Method:

TypeScript
onAddProfileUser(profileId: string, userId: string) {
    let newProfileUser: ProfileUserCreate = {
      profileId: profileId,
      userId: userId
    }

    this.profileService.addProfileForUser(newProfileUser)
      .subscribe(x => {
        this.reloadProfile.reloadprofileUserBehavior$.next(null);
        this.reloadProfile.reloadProfileListDisctincBehavior$.next(null);
      });
  }


Delete Method:

TypeScript
onDeleteProfileUser(profileUser: ProfileUser): void {
        this.profileService.deleteProfileForUser(profileUser)
          .subscribe(x => {
            this.reloadProfile.reloadProfileListDisctincBehavior$.next(null);
            this.reloadProfile.reloadprofileUserBehavior$.next(null);
          });
      }


Reload Service:

TypeScript
export class SearchEmitterService {
  reloadProfileListDisctincBehavior$: BehaviorSubject<any> = new BehaviorSubject<any>(null);
  reloadprofileUserBehavior$: BehaviorSubject<any> = new BehaviorSubject<any>(null);

  constructor() { }
Posted
Updated 21-Jun-22 21:28pm
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900