Website Summary Files Commits Branches @commit @diff @stats
path: root / src / DevHive.Angular / src / app
diff options
context:
space:
mode:
diff --git a/src/DevHive.Angular/src/app/components/post-page/post-page.component.css b/src/DevHive.Angular/src/app/components/post-page/post-page.component.css
index 50adee5..b1814fb 100644
--- a/src/DevHive.Angular/src/app/components/post-page/post-page.component.css
+++ b/src/DevHive.Angular/src/app/components/post-page/post-page.component.css
@@ -6,13 +6,21 @@
width: 100%;
}
-#back-btns {
+.many-buttons {
width: 100%;
display: flex;
}
-.submit-btn {
+.many-buttons > * {
flex: 1;
+ margin-right: .3em;
+}
+
+.many-buttons:last-of-type {
+ margin-right: 0;
+}
+
+.submit-btn {
max-width: 98%;
margin: 0 auto;
margin-bottom: .5em;
diff --git a/src/DevHive.Angular/src/app/components/post-page/post-page.component.html b/src/DevHive.Angular/src/app/components/post-page/post-page.component.html
index 4f6f33d..40a2aba 100644
--- a/src/DevHive.Angular/src/app/components/post-page/post-page.component.html
+++ b/src/DevHive.Angular/src/app/components/post-page/post-page.component.html
@@ -1,6 +1,14 @@
<div id="content">
- <div id="back-btns">
+ <div class="many-buttons">
<button class="submit-btn" type="submit" (click)="backToFeed()">ᐊ Back to feed</button>
</div>
<app-post [paramId]="postId.toString()"></app-post>
+ <div class="many-buttons" *ngIf="editable">
+ <button class="submit-btn" (click)="editPost()">Edit post</button>
+ <button class="submit-btn delete-btn" (click)="deletePost()">Delete post</button>
+ </div>
+ <form *ngIf="editingPost" [formGroup]="editPostFormGroup" (ngSubmit)="editPost()">
+ <input type="text" placeholder="New post message" class="input-field" formControlName="newPostMessage">
+ <input type="submit" style="display: none" />
+ </form>
<div>
diff --git a/src/DevHive.Angular/src/app/components/post-page/post-page.component.ts b/src/DevHive.Angular/src/app/components/post-page/post-page.component.ts
index 56e146d..3705ae6 100644
--- a/src/DevHive.Angular/src/app/components/post-page/post-page.component.ts
+++ b/src/DevHive.Angular/src/app/components/post-page/post-page.component.ts
@@ -1,6 +1,11 @@
+import {HttpErrorResponse} from '@angular/common/http';
import { Component, OnInit } from '@angular/core';
+import {FormBuilder, FormControl, FormGroup} from '@angular/forms';
import { Router } from '@angular/router';
import { Guid } from 'guid-typescript';
+import {PostService} from 'src/app/services/post.service';
+import {TokenService} from 'src/app/services/token.service';
+import {Post} from 'src/models/post';
@Component({
selector: 'app-post-page',
@@ -8,16 +13,55 @@ import { Guid } from 'guid-typescript';
styleUrls: ['./post-page.component.css']
})
export class PostPageComponent implements OnInit {
+ public editable = false;
+ public editingPost = false;
public postId: Guid;
+ public editPostFormGroup: FormGroup;
- constructor(private _router: Router)
+ constructor(private _router: Router, private _fb: FormBuilder, private _tokenService: TokenService, private _postService: PostService)
{ }
ngOnInit(): void {
this.postId = Guid.parse(this._router.url.substring(6));
+
+ // Gets the post and the logged in user and compares them,
+ // to determine if the current post is made by the user
+ this._postService.getPostRequest(this.postId).subscribe(
+ (result: object) => {
+ const post = result as Post;
+ this.editable = post.creatorUsername === this._tokenService.getUsernameFromSessionStorageToken();
+ },
+ (err: HttpErrorResponse) => {
+ this._router.navigate(['/not-found']);
+ }
+ );
+
+ this.editPostFormGroup = this._fb.group({
+ newPostMessage: new FormControl('')
+ });
}
backToFeed(): void {
this._router.navigate(['/']);
}
+
+ editPost(): void {
+ if (this.editingPost) {
+ const newMessage = this.editPostFormGroup.get('newPostMessage')?.value;
+ if (newMessage !== '') {
+ this._postService.putPostFromSessionStorageRequest(this.postId, newMessage).subscribe(
+ (result: object) => {
+ // Reload the page
+ this._router.routeReuseStrategy.shouldReuseRoute = () => false;
+ this._router.onSameUrlNavigation = 'reload';
+ this._router.navigate([this._router.url]);
+ }
+ );
+ }
+ }
+ this.editingPost = !this.editingPost;
+ }
+
+ deletePost(): void {
+ }
}
diff --git a/src/DevHive.Angular/src/app/components/profile-settings/profile-settings.component.css b/src/DevHive.Angular/src/app/components/profile-settings/profile-settings.component.css
index 83f66f1..716ffc9 100644
--- a/src/DevHive.Angular/src/app/components/profile-settings/profile-settings.component.css
+++ b/src/DevHive.Angular/src/app/components/profile-settings/profile-settings.component.css
@@ -78,11 +78,6 @@ hr {
margin-top: 1em;
}
-#delete-account:hover {
- color: indianred;
- border-color: indianred !important;
-}
-
#confirm-delete {
box-sizing: border-box;
width: 100%;
diff --git a/src/DevHive.Angular/src/app/components/profile-settings/profile-settings.component.html b/src/DevHive.Angular/src/app/components/profile-settings/profile-settings.component.html
index c38a6d7..ef5e8fe 100644
--- a/src/DevHive.Angular/src/app/components/profile-settings/profile-settings.component.html
+++ b/src/DevHive.Angular/src/app/components/profile-settings/profile-settings.component.html
@@ -101,6 +101,6 @@
<div id="confirm-delete" *ngIf="deleteAccountConfirm">
Are you sure you want to delete your account?<br>This is permanent!
</div>
- <button id="delete-account" class="submit-btn" (click)="deleteAccount()">Delete account</button>
+ <button class="submit-btn delete-btn" (click)="deleteAccount()">Delete account</button>
</div>
</div>
diff --git a/src/DevHive.Angular/src/app/services/post.service.ts b/src/DevHive.Angular/src/app/services/post.service.ts
index a35a323..c8f9a61 100644
--- a/src/DevHive.Angular/src/app/services/post.service.ts
+++ b/src/DevHive.Angular/src/app/services/post.service.ts
@@ -1,5 +1,6 @@
import { HttpClient, HttpHeaders, HttpParams } from '@angular/common/http';
import { Injectable } from '@angular/core';
+import * as FormData from 'form-data';
import { Guid } from 'guid-typescript';
import { Observable } from 'rxjs';
import { Post } from 'src/models/post';
@@ -26,18 +27,23 @@ export class PostService {
return this.createPostRequest(userId, token, postMessage);
}
+ putPostFromSessionStorageRequest(postId: Guid, newMessage: string): Observable<object> {
+ const userId = this._tokenService.getUserIdFromSessionStorageToken();
+ const token = this._tokenService.getTokenFromSessionStorage();
+
+ return this.putPostRequest(userId, token, postId, newMessage);
+ }
+
/* Post requests */
createPostRequest(userId: Guid, authToken: string, postMessage: string): Observable<object> {
- const body = {
- message: postMessage,
- files: []
- };
+ const form = new FormData();
+ form.append('message', postMessage);
const options = {
params: new HttpParams().set('UserId', userId.toString()),
headers: new HttpHeaders().set('Authorization', 'Bearer ' + authToken)
};
- return this._http.post(AppConstants.API_POST_URL, body, options);
+ return this._http.post(AppConstants.API_POST_URL, form, options);
}
getPostRequest(id: Guid): Observable<object> {
@@ -46,4 +52,16 @@ export class PostService {
};
return this._http.get(AppConstants.API_POST_URL, options);
}
+
+ putPostRequest(userId: Guid, authToken: string, postId: Guid, newMessage: string): Observable<object> {
+ const body = {
+ postId: postId.toString(),
+ newMessage: newMessage
+ };
+ const options = {
+ params: new HttpParams().set('UserId', userId.toString()),
+ headers: new HttpHeaders().set('Authorization', 'Bearer ' + authToken)
+ };
+ return this._http.put(AppConstants.API_POST_URL, body, options);
+ }
}
diff --git a/src/DevHive.Angular/src/app/services/token.service.ts b/src/DevHive.Angular/src/app/services/token.service.ts
index 8131228..62bc07e 100644
--- a/src/DevHive.Angular/src/app/services/token.service.ts
+++ b/src/DevHive.Angular/src/app/services/token.service.ts
@@ -32,6 +32,15 @@ export class TokenService {
return userCred.ID;
}
+ getUsernameFromSessionStorageToken(): string {
+ const jwt: IJWTPayload = {
+ token: this.getTokenFromSessionStorage()
+ };
+ const userCred = jwt_decode<IUserCredentials>(jwt.token);
+
+ return userCred.Username;
+ }
+
logoutUserFromSessionStorage(): void {
sessionStorage.removeItem(AppConstants.SESSION_TOKEN_KEY);
}