Add error messages when posting failed

master
trivernis 5 years ago
parent f11e6a3699
commit 0be475e79a

@ -7,7 +7,7 @@ infinite-scroll
<mat-card > <mat-card >
<mat-card-content> <mat-card-content>
<mat-form-field id="input"> <mat-form-field id="input">
<textarea matInput #content type="text" [(ngModel)]="empty" mat-autosize="true" matAutosizeMaxRows="3" placeholder="post something"></textarea> <textarea matInput #content type="text" [(ngModel)]="textInputValue" mat-autosize="true" matAutosizeMaxRows="3" placeholder="post something"></textarea>
<button mat-button matSuffix mat-icon-button> <button mat-button matSuffix mat-icon-button>
<mat-icon>add</mat-icon> <mat-icon>add</mat-icon>
</button> </button>
@ -24,7 +24,8 @@ infinite-scroll
</mat-option> </mat-option>
</mat-select> </mat-select>
</mat-form-field> </mat-form-field>
<button mat-raised-button *ngIf="empty" color="primary" id="post-button" (click)=createPost(content,activityId)> <mat-error *ngIf="errorOccurred && textInputValue">{{getErrorMessage()}}</mat-error>
<button mat-raised-button *ngIf="textInputValue" color="primary" id="post-button" (click)=createPost(content,activityId)>
POST POST
</button> </button>
</mat-card-content> </mat-card-content>

@ -5,6 +5,7 @@ import { Activitylist } from 'src/app/models/activity';
import {DatasharingService} from '../../services/datasharing.service'; import {DatasharingService} from '../../services/datasharing.service';
import {ActivityService} from 'src/app/services/activity/activity.service'; import {ActivityService} from 'src/app/services/activity/activity.service';
import {User} from 'src/app/models/user'; import {User} from 'src/app/models/user';
import {IErrorResponse} from '../../models/interfaces/IErrorResponse';
@Component({ @Component({
selector: 'home-feed', selector: 'home-feed',
@ -17,21 +18,25 @@ export class FeedComponent implements OnInit {
checked = false; // if the "I protected the environment."-box is checked checked = false; // if the "I protected the environment."-box is checked
view = 'new'; view = 'new';
empty: any; textInputValue: string;
// id of the green activity // id of the green activity
value: any; value: any;
parentSelectedPostList: Array<Post>; parentSelectedPostList: Post[];
actionlist: Activitylist = new Activitylist(); actionlist: Activitylist = new Activitylist();
loggedIn = false; loggedIn = false;
user: User; user: User;
errorOccurred: boolean;
private errorMessage: string;
constructor( constructor(
private feedService: FeedService, private feedService: FeedService,
private data: DatasharingService, private data: DatasharingService,
private activityService: ActivityService private activityService: ActivityService
) { } ) {
}
ngOnInit() { ngOnInit() {
this.data.currentUserInfo.subscribe(user => { this.data.currentUserInfo.subscribe(user => {
@ -60,15 +65,23 @@ export class FeedComponent implements OnInit {
createPost(pElement, activityId: string) { createPost(pElement, activityId: string) {
if (pElement && activityId && this.checked) { if (pElement && activityId && this.checked) {
this.feedService.createPostActivity(pElement.value, activityId); this.feedService.createPostActivity(pElement.value, activityId).subscribe(() => {
pElement.value = ''; pElement.value = '';
this.empty = ''; this.textInputValue = '';
this.view = 'new'; this.view = 'new';
}, (error: IErrorResponse) => {
this.errorOccurred = true;
this.errorMessage = error.error.errors[0].message;
});
} else if (pElement) { } else if (pElement) {
this.feedService.createPost(pElement.value); this.feedService.createPost(pElement.value).subscribe(() => {
pElement.value = ''; pElement.value = '';
this.empty = ''; this.textInputValue = '';
this.view = 'new'; this.view = 'new';
}, (error: IErrorResponse) => {
this.errorOccurred = true;
this.errorMessage = error.error.errors[0].message;
});
} }
} }
@ -84,4 +97,11 @@ export class FeedComponent implements OnInit {
showMostLiked() { showMostLiked() {
this.feedService.getPosts('TOP'); this.feedService.getPosts('TOP');
} }
/**
* Returns the error message if one exists
*/
getErrorMessage() {
return this.errorMessage;
}
} }

@ -5,7 +5,7 @@ import { Author } from 'src/app/models/author';
import { environment } from 'src/environments/environment'; import { environment } from 'src/environments/environment';
import { Activity } from 'src/app/models/activity'; import { Activity } from 'src/app/models/activity';
import { BehaviorSubject } from 'rxjs'; import { BehaviorSubject } from 'rxjs';
import { User } from 'src/app/models/user'; import {tap} from 'rxjs/operators';
@Injectable({ @Injectable({
providedIn: 'root' providedIn: 'root'
@ -51,12 +51,12 @@ export class FeedService {
}`, variables: { }`, variables: {
content: pContent content: pContent
}}; }};
return this.http.post(environment.graphQLUrl, body).subscribe(response => { return this.http.post(environment.graphQLUrl, body).pipe(tap(response => {
const updatedposts = this.newPosts.getValue(); const updatedposts = this.newPosts.getValue();
updatedposts.unshift(this.renderPost(response)); updatedposts.unshift(this.renderPost(response));
this.newPosts.next(updatedposts); this.newPosts.next(updatedposts);
this.setPost('NEW'); this.setPost('NEW');
}); }));
} }
public createPostActivity(pContent: String, activityId: String) { public createPostActivity(pContent: String, activityId: String) {
@ -88,12 +88,12 @@ export class FeedService {
content: pContent, content: pContent,
id: activityId id: activityId
}}; }};
return this.http.post(environment.graphQLUrl, body).subscribe(response => { return this.http.post(environment.graphQLUrl, body).pipe(tap(response => {
const updatedposts = this.newPosts.getValue(); const updatedposts = this.newPosts.getValue();
updatedposts.unshift(this.renderPost(response)); updatedposts.unshift(this.renderPost(response));
this.newPosts.next(updatedposts); this.newPosts.next(updatedposts);
this.setPost('NEW'); this.setPost('NEW');
}); }));
} }
public upvote(postId: number): any { public upvote(postId: number): any {

Loading…
Cancel
Save