Skip to content

Commit e350c98

Browse files
committed
add profile-data service for profile
1 parent f86febd commit e350c98

4 files changed

Lines changed: 76 additions & 41 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
@@ -1,6 +1,6 @@
11
<!-- @format -->
22

3-
@if (user$ | async; as user) {
3+
@if (user) {
44
<div class="profile">
55
<section class="profile__bar">
66
@if (loggedUserId$ | async; as loggedUserId) {

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

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

3-
import { Component, OnInit, signal } from "@angular/core";
3+
import { Component, inject, OnInit, signal } from "@angular/core";
44
import { ActivatedRoute, RouterLink, RouterOutlet } from "@angular/router";
5-
import { map, Observable } from "rxjs";
5+
import { filter, map, Observable, take } from "rxjs";
66
import { User } from "@auth/models/user.model";
77
import { NavService } from "@services/nav.service";
88
import { AuthService } from "@auth/services";
@@ -16,6 +16,7 @@ import { ModalComponent } from "@ui/components/modal/modal.component";
1616
import { calculateProfileProgress } from "@utils/calculateProgress";
1717
import { ProfileService as SkillsProfileService } from "projects/skills/src/app/profile/services/profile.service";
1818
import { TooltipComponent } from "@ui/components/tooltip/tooltip.component";
19+
import { ProfileDataService } from "./services/profile-date.service";
1920

2021
/**
2122
* Компонент детального просмотра профиля пользователя
@@ -52,23 +53,46 @@ import { TooltipComponent } from "@ui/components/tooltip/tooltip.component";
5253
YearsFromBirthdayPipe,
5354
BarComponent,
5455
ModalComponent,
55-
TooltipComponent,
5656
],
5757
})
5858
export class ProfileDetailComponent implements OnInit {
59-
constructor(
60-
private readonly route: ActivatedRoute,
61-
private readonly navService: NavService,
62-
public readonly authService: AuthService,
63-
public readonly chatService: ChatService,
64-
public readonly skillsProfileService: SkillsProfileService,
65-
public readonly breakpointObserver: BreakpointObserver
66-
) {}
59+
private readonly route = inject(ActivatedRoute);
60+
private readonly navService = inject(NavService);
61+
private readonly profileDataService = inject(ProfileDataService);
62+
public readonly authService = inject(AuthService);
63+
public readonly chatService = inject(ChatService);
64+
public readonly skillsProfileService = inject(SkillsProfileService);
65+
public readonly breakpointObserver = inject(BreakpointObserver);
6766

68-
user$: Observable<User> = this.route.data.pipe(
69-
map(r => r["data"][0]),
70-
map(user => ({ ...user, progress: calculateProfileProgress(user) }))
71-
);
67+
/**
68+
* Инициализация компонента
69+
* Настраивает заголовок навигации, проверяет статус подписки,
70+
* определяет необходимость заполнения профиля
71+
*/
72+
ngOnInit(): void {
73+
this.navService.setNavTitle("Профиль");
74+
75+
this.profileDataService
76+
.getProfile()
77+
.pipe(
78+
map(user => ({ ...user, progress: calculateProfileProgress(user!) })),
79+
filter(user => !!user),
80+
take(1)
81+
)
82+
.subscribe({
83+
next: user => {
84+
this.user = user as User;
85+
this.isProfileFill =
86+
user.progress! < 100 ? (this.isProfileFill = true) : (this.isProfileFill = false);
87+
},
88+
});
89+
90+
this.skillsProfileService.getSubscriptionData().subscribe(r => {
91+
this.isSubscriptionActive.set(r.isSubscribed);
92+
});
93+
}
94+
95+
user?: User;
7296

7397
loggedUserId$: Observable<number> = this.authService.profile.pipe(map(user => user.id));
7498

@@ -81,6 +105,11 @@ export class ProfileDetailComponent implements OnInit {
81105
tooltipText = "Заполни до конца — и открой весь функционал платформы!";
82106
isHintVisible = false;
83107

108+
errorMessageModal = signal("");
109+
desktopMode$: Observable<boolean> = this.breakpointObserver
110+
.observe("(min-width: 920px)")
111+
.pipe(map(result => result.matches));
112+
84113
/**
85114
* Показать подсказку для незаполненного профиля
86115
*/
@@ -95,29 +124,6 @@ export class ProfileDetailComponent implements OnInit {
95124
this.isHintVisible = false;
96125
}
97126

98-
errorMessageModal = signal("");
99-
desktopMode$: Observable<boolean> = this.breakpointObserver
100-
.observe("(min-width: 920px)")
101-
.pipe(map(result => result.matches));
102-
103-
/**
104-
* Инициализация компонента
105-
* Настраивает заголовок навигации, проверяет статус подписки,
106-
* определяет необходимость заполнения профиля
107-
*/
108-
ngOnInit(): void {
109-
this.navService.setNavTitle("Профиль");
110-
111-
this.skillsProfileService.getSubscriptionData().subscribe(r => {
112-
this.isSubscriptionActive.set(r.isSubscribed);
113-
});
114-
115-
this.user$.subscribe(r => {
116-
this.isProfileFill =
117-
r.progress! < 100 ? (this.isProfileFill = true) : (this.isProfileFill = false);
118-
});
119-
}
120-
121127
/**
122128
* Отправка CV пользователя на email
123129
* Проверяет ограничения по времени и отправляет CV на почту пользователя

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

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,9 @@ 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 } from "rxjs";
8+
import { forkJoin, map, tap } from "rxjs";
99
import { Project } from "@office/models/project.model";
10+
import { ProfileDataService } from "./services/profile-date.service";
1011

1112
/**
1213
* Резолвер для загрузки данных профиля пользователя
@@ -31,9 +32,13 @@ export const ProfileDetailResolver: ResolveFn<[User, Project[]]> = (
3132
) => {
3233
const authService = inject(AuthService);
3334
const subscriptionService = inject(SubscriptionService);
35+
const profileDataService = inject(ProfileDataService);
3436

3537
return forkJoin([
36-
authService.getUser(Number(route.paramMap.get("id"))),
38+
authService
39+
.getUser(Number(route.paramMap.get("id")))
40+
.pipe(tap(profile => profileDataService.setProfile(profile))),
41+
3742
subscriptionService
3843
.getSubscriptions(Number(route.paramMap.get("id")))
3944
.pipe(map(resp => resp.results)),
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/** @format */
2+
3+
import { Injectable } from "@angular/core";
4+
import { User } from "@auth/models/user.model";
5+
import { BehaviorSubject, filter, map } from "rxjs";
6+
7+
@Injectable({
8+
providedIn: "root",
9+
})
10+
export class ProfileDataService {
11+
private profilesSubject = new BehaviorSubject<User | undefined>(undefined);
12+
profile$ = this.profilesSubject.asObservable();
13+
14+
setProfile(profile: User) {
15+
this.profilesSubject.next(profile);
16+
}
17+
18+
getProfile() {
19+
return this.profile$.pipe(
20+
map(profile => profile),
21+
filter(profile => !!profile)
22+
);
23+
}
24+
}

0 commit comments

Comments
 (0)