Skip to content

Commit 6f3efcc

Browse files
committed
changes subcription to async
1 parent 4a2f907 commit 6f3efcc

5 files changed

Lines changed: 35 additions & 27 deletions

File tree

src/app/components/search-page/search-page.component.ts

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -9,26 +9,15 @@ import * as UsersSelectors from '../../shared/state/selectors/users.selector';
99
templateUrl: './search-page.component.html',
1010
styleUrls: ['./search-page.component.scss']
1111
})
12-
export class SearchPageComponent implements OnInit, OnDestroy {
13-
$users: Observable<User>;
14-
$userStoreSubscrition: Subscription;
12+
export class SearchPageComponent implements OnInit {
13+
$users: Observable<User[]>;
1514
constructor(private store: Store<{ users: User[] }>) {}
1615

1716
ngOnInit(): void {
1817
this.getUsers();
1918
}
2019

2120
getUsers(): void {
22-
this.$userStoreSubscrition = this.store
23-
.select(UsersSelectors.selectUsers)
24-
.subscribe(res => {
25-
this.$users = of(res);
26-
});
27-
}
28-
29-
ngOnDestroy(): void {
30-
if (this.$userStoreSubscrition) {
31-
this.$userStoreSubscrition.unsubscribe;
32-
}
21+
this.$users = this.store.select(UsersSelectors.selectUsers);
3322
}
3423
}

src/app/components/search-page/serach-results/user-result/user-result.component.ts

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,24 @@
1-
import { Component, OnInit, Input } from '@angular/core';
1+
import { Component, OnInit, Input, OnDestroy } from '@angular/core';
22
import { UserItems } from 'src/app/shared/models/user-items.model';
3-
import { Observable } from 'rxjs';
3+
import { Observable, Subscription } from 'rxjs';
44
import { Store } from '@ngrx/store';
55
import * as UsersActions from 'src/app/shared/state/actions/users.actions';
66
import * as UsersSelectors from 'src/app/shared/state/selectors/users.selector';
77
import { map } from 'rxjs/operators';
88
import { UiService } from 'src/app/shared/services/ui.service';
99
import { UserFullProfile } from 'src/app/shared/models/user-full-profile';
10-
import { User } from 'src/app/shared/models/user.model';
10+
1111
@Component({
1212
selector: 'app-user-result',
1313
templateUrl: './user-result.component.html',
1414
styleUrls: ['./user-result.component.scss']
1515
})
16-
export class UserResultComponent implements OnInit {
16+
export class UserResultComponent implements OnInit, OnDestroy {
1717
@Input()
1818
user: UserItems;
1919
$subData: Observable<UserFullProfile>;
2020
selectedUser: UserFullProfile;
21+
$usersSubscrition: Subscription;
2122

2223
constructor(
2324
private store: Store<{ selectedUser: UserFullProfile }>,
@@ -29,7 +30,7 @@ export class UserResultComponent implements OnInit {
2930
onSelectUser(selectedUser: UserItems): void {
3031
this.uiService.setTabSelected(false);
3132
this.store.dispatch(new UsersActions.SetSelectedUser(null));
32-
this.store
33+
this.$usersSubscrition = this.store
3334
.select(UsersSelectors.selectSearchSubData)
3435
.pipe(
3536
map(usersSubdata =>
@@ -45,4 +46,10 @@ export class UserResultComponent implements OnInit {
4546
this.uiService.setTabSelected(true);
4647
});
4748
}
49+
50+
ngOnDestroy(): void {
51+
if (this.$usersSubscrition) {
52+
this.$usersSubscrition.unsubscribe();
53+
}
54+
}
4855
}
Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import { Component, OnInit, Input } from '@angular/core';
2-
import { Observable } from 'rxjs';
1+
import { Component, OnInit, Input, OnDestroy } from '@angular/core';
2+
import { Observable, Subscription } from 'rxjs';
33
import { UiService } from 'src/app/shared/services/ui.service';
44
import { User } from 'src/app/shared/models/user.model';
55

@@ -8,11 +8,12 @@ import { User } from 'src/app/shared/models/user.model';
88
templateUrl: './tabs.component.html',
99
styleUrls: ['./tabs.component.scss']
1010
})
11-
export class TabsComponent implements OnInit {
11+
export class TabsComponent implements OnInit, OnDestroy {
1212
@Input()
1313
users: User[];
1414
$tabSelected: Observable<boolean>;
1515
flag = false;
16+
$tabSelectedSubscription: Subscription;
1617

1718
constructor(private uiServivce: UiService) {}
1819

@@ -21,6 +22,14 @@ export class TabsComponent implements OnInit {
2122
}
2223

2324
isTabSelected(): void {
24-
this.uiServivce.dataTabSelected.subscribe(flag => (this.flag = flag));
25+
this.$tabSelectedSubscription = this.uiServivce.dataTabSelected.subscribe(
26+
flag => (this.flag = flag)
27+
);
28+
}
29+
30+
ngOnDestroy() {
31+
if (this.$tabSelectedSubscription) {
32+
this.$tabSelectedSubscription.unsubscribe();
33+
}
2534
}
2635
}

src/app/shared/state/selectors/users.selector.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import { State } from '../state';
77
import { User } from '../../models/user.model';
88
import { UserFullProfile } from '../../models/user-full-profile';
99

10-
const getUsers = (state: State): User => state.users;
10+
const getUsers = (state: State): User[] => state.users;
1111
const geSubData = (state: State): UserFullProfile[] => state.subdata;
1212
const geSelectedUserData = (state: State): UserFullProfile =>
1313
state.selectedUser;
@@ -22,12 +22,15 @@ export const selectSubDataState: MemoizedSelector<
2222
State
2323
> = createFeatureSelector<State>('usersStore');
2424

25-
export const selectUsers: MemoizedSelector<object, User> = createSelector(
25+
export const selectUsers: MemoizedSelector<object, User[]> = createSelector(
2626
selectUsersState,
2727
getUsers
2828
);
2929

30-
export const selectSearchUser: MemoizedSelector<object, User> = createSelector(
30+
export const selectSearchUser: MemoizedSelector<
31+
object,
32+
User[]
33+
> = createSelector(
3134
selectUsersState,
3235
getUsers
3336
);

src/app/shared/state/state.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { User } from '../models/user.model';
22
import { UserFullProfile } from '../models/user-full-profile';
33

44
export interface State {
5-
users: User;
5+
users: User[];
66
subdata: UserFullProfile[];
77
selectedUser: UserFullProfile;
88
}

0 commit comments

Comments
 (0)