aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorKamen Mladenov <kamen.d.mladenov@protonmail.com>2021-04-08 09:32:55 +0300
committerGitHub <noreply@github.com>2021-04-08 09:32:55 +0300
commit601b39df7da18bfa80a0443be258ccee74403b0a (patch)
tree8f29ea0acf3716fd13b24d3a3282f66a9bf78e68
parentd12c7c4dbf117ecec60eae6f0dc8d9bb5ee339b5 (diff)
parentbf75b260b2c8b8c11ae3c672ae6378bd24f9f196 (diff)
downloadDevHive-Angular-601b39df7da18bfa80a0443be258ccee74403b0a.tar
DevHive-Angular-601b39df7da18bfa80a0443be258ccee74403b0a.tar.gz
DevHive-Angular-601b39df7da18bfa80a0443be258ccee74403b0a.zip
Merge pull request #6 from Team-Kaleidoscope/friends-update
Friends request update
-rw-r--r--src/app/app-constants.module.ts1
-rw-r--r--src/app/components/profile/profile.component.html9
-rw-r--r--src/app/components/profile/profile.component.ts52
-rw-r--r--src/app/services/friend.service.ts44
-rw-r--r--src/app/services/user.service.ts17
5 files changed, 69 insertions, 54 deletions
diff --git a/src/app/app-constants.module.ts b/src/app/app-constants.module.ts
index d1bd9f5..67091d9 100644
--- a/src/app/app-constants.module.ts
+++ b/src/app/app-constants.module.ts
@@ -3,6 +3,7 @@ export class AppConstants {
public static API_USER_URL = AppConstants.BASE_API_URL + '/User';
public static API_PROFILE_PICTURE_URL = AppConstants.BASE_API_URL + '/ProfilePicture';
+ public static API_FRIENDS_URL = AppConstants.BASE_API_URL + '/Friends';
public static API_USER_LOGIN_URL = AppConstants.API_USER_URL + '/login';
public static API_USER_REGISTER_URL = AppConstants.API_USER_URL + '/register';
diff --git a/src/app/components/profile/profile.component.html b/src/app/components/profile/profile.component.html
index 1a360fc..c6fdee6 100644
--- a/src/app/components/profile/profile.component.html
+++ b/src/app/components/profile/profile.component.html
@@ -14,12 +14,9 @@
<div>
@{{ user.userName }}
</div>
- <form class="flex-row flexible-children width-full font-size-dot7 margin-top-dot4" [formGroup]="updateFrienship" (ngSubmit)="modifyFriend()" *ngIf="!isTheLoggedInUser && isUserLoggedIn">
- <input class="border-faded-slim border-bottom-only margin-right-dot2" type="password" formControlName="password" *ngIf="updatingFriendship" placeholder="Type in password to confirm">
- <button id="add-friend" type="submit" class="border-faded-slim padding-dot1 lighter-hover click-effect border-radius-dot3 width-full">
- {{ updatingFriendship ? 'Confirm' : (friendOfUser ? 'Unfriend' : 'Add friend') }}
- </button>
- </form>
+ <button id="add-friend" class="border-faded-slim padding-dot3 lighter-hover click-effect border-radius-dot3 font-size-dot8 margin-top-dot4" (click)="modifyFriend()" *ngIf="!isTheLoggedInUser && isUserLoggedIn">
+ {{ friendOfUser ? 'Unfriend' : 'Add friend' }}
+ </button>
</div>
</section>
<section class="card sec-info-card flex-col width-full">
diff --git a/src/app/components/profile/profile.component.ts b/src/app/components/profile/profile.component.ts
index 83f6517..f9f64b5 100644
--- a/src/app/components/profile/profile.component.ts
+++ b/src/app/components/profile/profile.component.ts
@@ -10,8 +10,7 @@ import { Post } from 'src/models/post.model';
import { FeedService } from 'src/app/services/feed.service';
import { TokenService } from 'src/app/services/token.service';
import { Title } from '@angular/platform-browser';
-import { Friend } from 'src/models/identity/friend.model';
-import { FormBuilder, FormControl, FormGroup } from '@angular/forms';
+import { FriendService } from 'src/app/services/friend.service';
@Component({
selector: 'app-profile',
@@ -28,12 +27,10 @@ export class ProfileComponent implements OnInit {
public isAdminUser = false;
public dataArrived = false;
public friendOfUser = false;
- public updatingFriendship = false;
public user: User;
public userPosts: Post[];
- public updateFrienship: FormGroup;
- constructor(private _titleService: Title, private _fb: FormBuilder, private _router: Router, private _userService: UserService, private _languageService: LanguageService, private _technologyService: TechnologyService, private _feedService: FeedService, private _location: Location, private _tokenService: TokenService) {
+ constructor(private _titleService: Title, private _router: Router, private _userService: UserService, private _friendService: FriendService, private _languageService: LanguageService, private _technologyService: TechnologyService, private _feedService: FeedService, private _location: Location, private _tokenService: TokenService) {
this._titleService.setTitle(this._title);
}
@@ -48,10 +45,6 @@ export class ProfileComponent implements OnInit {
this.user = this._userService.getDefaultUser();
this.userPosts = [];
- this.updateFrienship = this._fb.group({
- password: new FormControl('')
- });
-
this._userService.getUserByUsernameRequest(this._urlUsername).subscribe({
next: (res: object) => {
Object.assign(this.user, res);
@@ -141,31 +134,28 @@ export class ProfileComponent implements OnInit {
}
modifyFriend(): void {
- if (this.updatingFriendship) {
- this.dataArrived = false;
-
- this._userService.getUserFromSessionStorageRequest().subscribe({
- next: (result: object) => {
- const loggedInUser: User = result as User;
-
- if (this.friendOfUser) {
- loggedInUser.friends = loggedInUser.friends.filter(x => x.userName !== this.user.userName);
- }
- else {
- const newFriend = new Friend();
- newFriend.userName = this.user.userName;
- loggedInUser.friends.push(newFriend);
- }
-
- this.updateUserWithNewFriends(loggedInUser);
- }
- });
+ this.dataArrived = false;
+ if (this.friendOfUser) {
+ this.removeFriendFromLoggedInUser();
}
- this.updatingFriendship = !this.updatingFriendship;
+ else {
+ this.addFriendToLoggedInUser();
+ }
+ }
+
+ private addFriendToLoggedInUser(): void {
+ this._friendService.postFriendWithSessionStorageRequest(this.user.userName).subscribe({
+ next: () => {
+ this.reloadPage();
+ },
+ error: () => {
+ this._router.navigate(['/']);
+ }
+ });
}
- private updateUserWithNewFriends(loggedInUser: User): void {
- this._userService.putBareUserFromSessionStorageRequest(loggedInUser, this.updateFrienship.get('password')?.value).subscribe({
+ private removeFriendFromLoggedInUser(): void {
+ this._friendService.deleteFriendWithSessionStorageRequest(this.user.userName).subscribe({
next: () => {
this.reloadPage();
},
diff --git a/src/app/services/friend.service.ts b/src/app/services/friend.service.ts
new file mode 100644
index 0000000..6821606
--- /dev/null
+++ b/src/app/services/friend.service.ts
@@ -0,0 +1,44 @@
+import { HttpClient, HttpHeaders, HttpParams } from '@angular/common/http';
+import { Injectable } from '@angular/core';
+import { Guid } from 'guid-typescript';
+import { Observable } from 'rxjs';
+import { AppConstants } from '../app-constants.module';
+import { TokenService } from './token.service';
+
+@Injectable({
+ providedIn: 'root'
+})
+export class FriendService {
+ constructor(private _http: HttpClient, private _tokenService: TokenService)
+ { }
+
+ postFriendWithSessionStorageRequest(friendUsername: string): Observable<object> {
+ const userId = this._tokenService.getUserIdFromSessionStorageToken();
+ const token = this._tokenService.getTokenFromSessionStorage();
+
+ return this.postFriendRequest(userId, token, friendUsername);
+ }
+
+ deleteFriendWithSessionStorageRequest(friendUsername: string): Observable<object> {
+ const userId = this._tokenService.getUserIdFromSessionStorageToken();
+ const token = this._tokenService.getTokenFromSessionStorage();
+
+ return this.deleteFriendRequest(userId, token, friendUsername);
+ }
+
+ postFriendRequest(userId: Guid, authToken: string, friendUsername: string): Observable<object> {
+ const options = {
+ params: new HttpParams().set('UserId', userId.toString()).set('FriendUsername', friendUsername),
+ headers: new HttpHeaders().set('Authorization', 'Bearer ' + authToken)
+ };
+ return this._http.post(AppConstants.API_FRIENDS_URL, {}, options);
+ }
+
+ deleteFriendRequest(userId: Guid, authToken: string, friendUsername: string): Observable<object> {
+ const options = {
+ params: new HttpParams().set('UserId', userId.toString()).set('FriendUsername', friendUsername),
+ headers: new HttpHeaders().set('Authorization', 'Bearer ' + authToken)
+ };
+ return this._http.delete(AppConstants.API_FRIENDS_URL, options);
+ }
+}
diff --git a/src/app/services/user.service.ts b/src/app/services/user.service.ts
index e910ada..690fff5 100644
--- a/src/app/services/user.service.ts
+++ b/src/app/services/user.service.ts
@@ -45,13 +45,6 @@ export class UserService {
return this.putUserRequest(userId, token, updateUserFormGroup, languages, technologies, userRoles, userFriends);
}
- putBareUserFromSessionStorageRequest(user: User, password: string): Observable<object> {
- const userId = this._tokenService.getUserIdFromSessionStorageToken();
- const token = this._tokenService.getTokenFromSessionStorage();
-
- return this.putBareUserRequest(userId, token, user, password);
- }
-
deleteUserFromSessionStorageRequest(): Observable<object> {
const userId = this._tokenService.getUserIdFromSessionStorageToken();
const token = this._tokenService.getTokenFromSessionStorage();
@@ -133,16 +126,6 @@ export class UserService {
return this._http.put(AppConstants.API_USER_URL, body, options);
}
- putBareUserRequest(userId: Guid, authToken: string, user: User, password: string): Observable<object> {
- const body: object = user;
- Object.assign(body, { password: password });
- const options = {
- params: new HttpParams().set('Id', userId.toString()),
- headers: new HttpHeaders().set('Authorization', 'Bearer ' + authToken)
- };
- return this._http.put(AppConstants.API_USER_URL, body, options);
- }
-
deleteUserRequest(userId: Guid, authToken: string): Observable<object> {
const options = {
params: new HttpParams().set('Id', userId.toString()),