Skip to content

Commit 4a517b4

Browse files
committed
add logic of getting subs & profileId of user
1 parent e350c98 commit 4a517b4

6 files changed

Lines changed: 123 additions & 22 deletions

File tree

projects/social_platform/src/app/office/profile/detail/profile-detail.component.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
@if (user) {
44
<div class="profile">
55
<section class="profile__bar">
6-
@if (loggedUserId$ | async; as loggedUserId) {
6+
@if (loggedUserId) {
77
<app-bar
88
class="bar"
99
[backHave]="true"

projects/social_platform/src/app/office/profile/detail/profile-detail.component.ts

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
/** @format */
22

3-
import { Component, inject, OnInit, signal } from "@angular/core";
3+
import { Component, inject, OnDestroy, OnInit, signal } from "@angular/core";
44
import { ActivatedRoute, RouterLink, RouterOutlet } from "@angular/router";
5-
import { filter, map, Observable, take } from "rxjs";
5+
import { filter, map, Observable, Subscription, take } from "rxjs";
66
import { User } from "@auth/models/user.model";
77
import { NavService } from "@services/nav.service";
88
import { AuthService } from "@auth/services";
@@ -55,7 +55,7 @@ import { ProfileDataService } from "./services/profile-date.service";
5555
ModalComponent,
5656
],
5757
})
58-
export class ProfileDetailComponent implements OnInit {
58+
export class ProfileDetailComponent implements OnInit, OnDestroy {
5959
private readonly route = inject(ActivatedRoute);
6060
private readonly navService = inject(NavService);
6161
private readonly profileDataService = inject(ProfileDataService);
@@ -72,7 +72,7 @@ export class ProfileDetailComponent implements OnInit {
7272
ngOnInit(): void {
7373
this.navService.setNavTitle("Профиль");
7474

75-
this.profileDataService
75+
const profileDataSub$ = this.profileDataService
7676
.getProfile()
7777
.pipe(
7878
map(user => ({ ...user, progress: calculateProfileProgress(user!) })),
@@ -87,14 +87,35 @@ export class ProfileDetailComponent implements OnInit {
8787
},
8888
});
8989

90+
const profileIdDataSub$ = this.profileDataService
91+
.getProfileId()
92+
.pipe(
93+
filter(userId => !!userId),
94+
take(1)
95+
)
96+
.subscribe({
97+
next: profileId => {
98+
this.loggedUserId = profileId;
99+
},
100+
});
101+
90102
this.skillsProfileService.getSubscriptionData().subscribe(r => {
91103
this.isSubscriptionActive.set(r.isSubscribed);
92104
});
105+
106+
profileDataSub$ && this.subscriptions.push(profileDataSub$);
107+
profileIdDataSub$ && this.subscriptions.push(profileIdDataSub$);
108+
}
109+
110+
ngOnDestroy(): void {
111+
this.subscriptions.forEach($ => $.unsubscribe());
93112
}
94113

95114
user?: User;
96115

97-
loggedUserId$: Observable<number> = this.authService.profile.pipe(map(user => user.id));
116+
loggedUserId?: number;
117+
118+
subscriptions: Subscription[] = [];
98119

99120
isDelayModalOpen = false;
100121
isSended = false;

projects/social_platform/src/app/office/profile/detail/profile-detail.resolver.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,10 @@ import { ActivatedRouteSnapshot, ResolveFn } from "@angular/router";
55
import { AuthService } from "@auth/services";
66
import { User } from "@auth/models/user.model";
77
import { SubscriptionService } from "@office/services/subscription.service";
8-
import { forkJoin, map, tap } from "rxjs";
8+
import { forkJoin, map, mergeMap, tap } from "rxjs";
99
import { Project } from "@office/models/project.model";
1010
import { ProfileDataService } from "./services/profile-date.service";
11+
import { profile } from "console";
1112

1213
/**
1314
* Резолвер для загрузки данных профиля пользователя
@@ -39,8 +40,9 @@ export const ProfileDetailResolver: ResolveFn<[User, Project[]]> = (
3940
.getUser(Number(route.paramMap.get("id")))
4041
.pipe(tap(profile => profileDataService.setProfile(profile))),
4142

42-
subscriptionService
43-
.getSubscriptions(Number(route.paramMap.get("id")))
44-
.pipe(map(resp => resp.results)),
43+
subscriptionService.getSubscriptions(Number(route.paramMap.get("id"))).pipe(
44+
map(subs => subs.results),
45+
tap(subs => profileDataService.setProfileSubs(subs))
46+
),
4547
]);
4648
};

projects/social_platform/src/app/office/profile/detail/projects/projects.component.html

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<!-- @format -->
2-
@if (user | async; as user) {
2+
@if (user) {
33
<section class="projects">
4-
@if (loggedUserId | async; as loggedUserId) {
4+
@if (loggedUserId) {
55
<div class="projects__content">
66
@if (user.projects.length) {
77
<div class="projects__section">
@@ -22,7 +22,7 @@ <h3 class="text-heading-3 about__title">
2222
}
2323
</ul>
2424
</div>
25-
} @if (subs | async; as subs) { @if (subs.length) {
25+
} @if (subs) { @if (subs.length) {
2626
<div class="projects__section">
2727
<h3 class="text-heading-3 about__title">
2828
{{
@@ -41,7 +41,7 @@ <h3 class="text-heading-3 about__title">
4141
}
4242
</ul>
4343
</div>
44-
} } @if (!user.projects.length) { @if (subs | async; as subs) { @if (!subs.length) {
44+
} } @if (!user.projects.length) { @if (subs) { @if (!subs.length) {
4545
<h3 class="text-heading-3 about__title">
4646
Вы пока не состоите ни в одном проекте и не подписаны ни на один.
4747
</h3>

projects/social_platform/src/app/office/profile/detail/projects/projects.component.ts

Lines changed: 57 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
/** @format */
22

3-
import { Component, OnInit } from "@angular/core";
3+
import { Component, inject, OnDestroy, OnInit } from "@angular/core";
44
import { ActivatedRoute, RouterLink } from "@angular/router";
55
import { User } from "@auth/models/user.model";
66
import { AuthService } from "@auth/services";
7-
import { map, Observable } from "rxjs";
7+
import { filter, Subscription, take } from "rxjs";
88
import { AsyncPipe } from "@angular/common";
99
import { Project } from "@office/models/project.model";
1010
import { InfoCardComponent } from "@office/shared/info-card/info-card.component";
11+
import { ProfileDataService } from "../services/profile-date.service";
1112

1213
/**
1314
* Компонент для отображения проектов пользователя
@@ -31,12 +32,60 @@ import { InfoCardComponent } from "@office/shared/info-card/info-card.component"
3132
standalone: true,
3233
imports: [RouterLink, AsyncPipe, InfoCardComponent],
3334
})
34-
export class ProfileProjectsComponent implements OnInit {
35-
constructor(private readonly route: ActivatedRoute, public readonly authService: AuthService) {}
35+
export class ProfileProjectsComponent implements OnInit, OnDestroy {
36+
private readonly route = inject(ActivatedRoute);
37+
private readonly profileDataService = inject(ProfileDataService);
38+
public readonly authService = inject(AuthService);
3639

37-
user?: Observable<User> = this.route.parent?.data.pipe(map(r => r["data"][0]));
38-
subs?: Observable<Project[]> = this.route.parent?.data.pipe(map(r => r["data"][1]));
39-
loggedUserId: Observable<number> = this.authService.profile.pipe(map(user => user.id));
40+
ngOnInit(): void {
41+
const profileDataSub$ = this.profileDataService
42+
.getProfile()
43+
.pipe(
44+
filter(user => !!user),
45+
take(1)
46+
)
47+
.subscribe({
48+
next: user => {
49+
this.user = user;
50+
},
51+
});
4052

41-
ngOnInit(): void {}
53+
const profileIdDataSub$ = this.profileDataService
54+
.getProfileId()
55+
.pipe(
56+
filter(profileId => !!profileId),
57+
take(1)
58+
)
59+
.subscribe({
60+
next: profileId => {
61+
this.loggedUserId = profileId;
62+
},
63+
});
64+
65+
const profileSubsDataSub$ = this.profileDataService
66+
.getProfileSubs()
67+
.pipe(
68+
filter(subs => !!subs),
69+
take(1)
70+
)
71+
.subscribe({
72+
next: subs => {
73+
this.subs = subs;
74+
},
75+
});
76+
77+
profileDataSub$ && this.subscriptions.push(profileDataSub$);
78+
profileIdDataSub$ && this.subscriptions.push(profileIdDataSub$);
79+
profileSubsDataSub$ && this.subscriptions.push(profileSubsDataSub$);
80+
}
81+
82+
ngOnDestroy(): void {
83+
this.subscriptions.forEach($ => $.unsubscribe());
84+
}
85+
86+
user?: User;
87+
loggedUserId?: number;
88+
subs?: Project[];
89+
90+
subscriptions: Subscription[] = [];
4291
}

projects/social_platform/src/app/office/profile/detail/services/profile-date.service.ts

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,32 @@
22

33
import { Injectable } from "@angular/core";
44
import { User } from "@auth/models/user.model";
5+
import { Project } from "@office/models/project.model";
56
import { BehaviorSubject, filter, map } from "rxjs";
67

78
@Injectable({
89
providedIn: "root",
910
})
1011
export class ProfileDataService {
1112
private profilesSubject = new BehaviorSubject<User | undefined>(undefined);
13+
private profileIdSubject = new BehaviorSubject<number | undefined>(undefined);
14+
private profileSubsSubject = new BehaviorSubject<Project[] | undefined>(undefined);
15+
1216
profile$ = this.profilesSubject.asObservable();
17+
profileId$ = this.profileIdSubject.asObservable();
18+
profileSubs$ = this.profileSubsSubject.asObservable();
1319

1420
setProfile(profile: User) {
1521
this.profilesSubject.next(profile);
22+
this.setProfileId(profile.id);
23+
}
24+
25+
setProfileId(id: number) {
26+
this.profileIdSubject.next(id);
27+
}
28+
29+
setProfileSubs(subs: Project[]) {
30+
this.profileSubsSubject.next(subs);
1631
}
1732

1833
getProfile() {
@@ -21,4 +36,18 @@ export class ProfileDataService {
2136
filter(profile => !!profile)
2237
);
2338
}
39+
40+
getProfileId() {
41+
return this.profileId$.pipe(
42+
map(profileId => profileId),
43+
filter(profileId => !!profileId)
44+
);
45+
}
46+
47+
getProfileSubs() {
48+
return this.profileSubs$.pipe(
49+
map(subs => subs),
50+
filter(subs => !!subs)
51+
);
52+
}
2453
}

0 commit comments

Comments
 (0)