Refactor social service

- Add error messages for group creation
- Switch to non-deprecated http module
master
Trivernis 5 years ago
parent 21bba52f78
commit c485641afb

@ -4,6 +4,7 @@
<input matInput placeholder="Enter groupname" #name> <input matInput placeholder="Enter groupname" #name>
</mat-form-field> </mat-form-field>
</div> </div>
<mat-error *ngIf="errorOccurred">{{getErrorMessage()}}</mat-error>
<div mat-dialog-actions> <div mat-dialog-actions>
<button mat-button (click)="onNoClick()">Cancel</button> <button mat-button (click)="onNoClick()">Cancel</button>
<button mat-button cdkFocusInitial (click)="createGroup(name.value)">Create Group</button> <button mat-button cdkFocusInitial (click)="createGroup(name.value)">Create Group</button>

@ -1,10 +1,13 @@
import { Component, OnInit } from '@angular/core'; import {Component, OnInit} from '@angular/core';
import { GroupInfo } from 'src/app/models/groupinfo'; import {GroupInfo} from 'src/app/models/groupinfo';
import {MatDialog, MatDialogRef} from '@angular/material/dialog'; import {MatDialog, MatDialogRef} from '@angular/material/dialog';
import { SocialService } from 'src/app/services/social/social.service'; import {SocialService} from 'src/app/services/social/social.service';
import { User } from 'src/app/models/user'; import {User} from 'src/app/models/user';
import { DatasharingService } from 'src/app/services/datasharing.service'; import {DatasharingService} from 'src/app/services/datasharing.service';
import { Router } from '@angular/router'; import {Router} from '@angular/router';
import {GraphQLError} from 'graphql';
import {IGraphqlError} from '../../../models/interfaces/IGraphqlError';
import {IErrorResponse} from '../../../models/interfaces/IErrorResponse';
// DIALOG COMPONENT to create groups // DIALOG COMPONENT to create groups
@Component({ @Component({
@ -12,22 +15,31 @@ import { Router } from '@angular/router';
templateUrl: 'dialog.html', templateUrl: 'dialog.html',
}) })
export class DialogCreateGroupComponent { export class DialogCreateGroupComponent {
errorOccurred = false;
private errorMessage: string;
constructor( constructor(
public dialogRef: MatDialogRef<DialogCreateGroupComponent>, private social: SocialService) {} public dialogRef: MatDialogRef<DialogCreateGroupComponent>, private social: SocialService) {
}
onNoClick(): void { onNoClick(): void {
this.dialogRef.close(); this.dialogRef.close();
} }
createGroup(name: string) { createGroup(name: string) {
console.log('create groupe ' + name);
name = name.trim(); name = name.trim();
if (name) { if (name) {
this.social.createGroup(name); this.social.createGroup(name).subscribe(() => {
this.dialogRef.close(); this.dialogRef.close();
}, ((error: IErrorResponse) => {
this.errorMessage = error.error.errors[0].message;
this.errorOccurred = true;
}));
}
} }
getErrorMessage() {
return this.errorMessage;
} }
} }
@ -39,11 +51,14 @@ export class DialogCreateGroupComponent {
}) })
export class GroupsComponent implements OnInit { export class GroupsComponent implements OnInit {
user: User; user: User;
constructor(public dialog: MatDialog, private data: DatasharingService, private router: Router) { }
constructor(public dialog: MatDialog, private data: DatasharingService, private router: Router) {
}
ngOnInit() { ngOnInit() {
this.data.currentUserInfo.subscribe(user => { this.data.currentUserInfo.subscribe(user => {
this.user = user; }); this.user = user;
});
} }
public showGroupProfile(group: GroupInfo) { public showGroupProfile(group: GroupInfo) {

@ -1,7 +1,6 @@
import { Injectable } from '@angular/core'; import { Injectable } from '@angular/core';
import {Http, URLSearchParams, Headers} from '@angular/http'; import {Http, URLSearchParams, Headers} from '@angular/http';
import { Chat } from 'src/app/models/chat'; import { Chat } from 'src/app/models/chat';
import { responsePathAsArray } from 'graphql';
import { Chatmessage } from 'src/app/models/chatmessage'; import { Chatmessage } from 'src/app/models/chatmessage';
import { FriendInfo } from 'src/app/models/friendinfo'; import { FriendInfo } from 'src/app/models/friendinfo';
import { DatasharingService } from '../datasharing.service'; import { DatasharingService } from '../datasharing.service';
@ -35,7 +34,7 @@ export class ChatService {
} }
public getAllChatsRaw(): any { public getAllChatsRaw(): any {
const url = 'https://greenvironment.net/graphql'; const url = environment.graphQLUrl;
const headers = new Headers(); const headers = new Headers();
headers.set('Content-Type', 'application/json'); headers.set('Content-Type', 'application/json');
@ -63,7 +62,7 @@ export class ChatService {
public getChatsByIDRaw(pChatIDs: number[]): any { public getChatsByIDRaw(pChatIDs: number[]): any {
for (const chatId of pChatIDs) { for (const chatId of pChatIDs) {
const url = 'https://greenvironment.net/graphql'; const url = environment.graphQLUrl;
const headers = new Headers(); const headers = new Headers();
headers.set('Content-Type', 'application/json'); headers.set('Content-Type', 'application/json');
@ -142,7 +141,7 @@ export class ChatService {
} }
public getMessagesRaw(pChatID): any { public getMessagesRaw(pChatID): any {
const url = 'https://greenvironment.net/graphql'; const url = environment.graphQLUrl;
const headers = new Headers(); const headers = new Headers();
headers.set('Content-Type', 'application/json'); headers.set('Content-Type', 'application/json');
@ -242,9 +241,9 @@ export class ChatService {
getBodyForGetAllChats() { getBodyForGetAllChats() {
const body = {query: `query { const body = {query: `query {
getSelf { getSelf {
chats(first: 1000, offset: 0) { chats(first: 10, offset: 0) {
id, members{name, id, level}, id, members{name, id, level},
messages(first: 1000, offset: 0) { messages(first: 10, offset: 0) {
author {id}, createdAt, content author {id}, createdAt, content
} }
} }

@ -69,7 +69,7 @@ export class LoginService extends BaseService {
*/ */
public login(login: Login) { public login(login: Login) {
const body = LoginService.buildRequestBody(login); const body = LoginService.buildRequestBody(login);
return this.http.post<ILoginRequestResult>(environment.graphQLUrl, body) return this.http.post<ILoginRequestResult>(environment.graphQLUrl, body, {headers: this.headers})
.pipe(tap(response => { .pipe(tap(response => {
const user = new User(); const user = new User();
user.assignFromResponse(response.data.login); user.assignFromResponse(response.data.login);

@ -46,8 +46,6 @@ export class SearchService extends BaseService {
super(); super();
} }
users: User[];
/** /**
* Builds the body for the request * Builds the body for the request
* @param query - the search query * @param query - the search query

@ -1,9 +1,7 @@
import { Injectable, EventEmitter, Output } from '@angular/core'; import { Injectable, EventEmitter, Output } from '@angular/core';
import {Http, URLSearchParams, Headers} from '@angular/http'; import {Http, URLSearchParams, Headers} from '@angular/http';
import { Login } from '../../models/login';
import { User } from 'src/app/models/user'; import { User } from 'src/app/models/user';
import { DatasharingService } from '../datasharing.service'; import { DatasharingService } from '../datasharing.service';
import { userInfo } from 'os';
import {Router} from '@angular/router'; import {Router} from '@angular/router';
import { environment } from 'src/environments/environment'; import { environment } from 'src/environments/environment';
import { FriendRequest } from 'src/app/models/friendRequest'; import { FriendRequest } from 'src/app/models/friendRequest';

@ -1,37 +1,41 @@
import {Injectable} from '@angular/core'; import {Injectable} from '@angular/core';
import {Headers, Http} from '@angular/http';
import {DatasharingService} from '../datasharing.service';
import {Router} from '@angular/router';
import {environment} from 'src/environments/environment'; import {environment} from 'src/environments/environment';
import { User } from 'src/app/models/user'; import {HttpClient} from '@angular/common/http';
import {BaseService} from '../base.service';
const graphqlCreateGroupQuery = `mutation($name: String!) {
createGroup(name: $name) {
id
}
}`;
@Injectable({ @Injectable({
providedIn: 'root' providedIn: 'root'
}) })
export class SocialService { export class SocialService extends BaseService {
users: Array<User>;
constructor(private http: Http, private data: DatasharingService, private router: Router) {
}
createGroup(name: string) { constructor(private http: HttpClient) {
const headers = new Headers(); super();
headers.set('Content-Type', 'application/json');
this.http.post(environment.graphQLUrl, this.buildJsonGroup(name)).subscribe(response => {
console.log(response.text()); });
} }
public buildJsonGroup(name_: String): any { /**
const body = { * Builds the body for a group creation request
query: `mutation($name: String!) { * @param name
createGroup(name: $name) { */
id private static buildGroupCreateBody(name: String): any {
} return {
}` query: graphqlCreateGroupQuery, variables: {
, variables: { name
name: name_
} }
}; };
return body; }
/**
* Creates a group
* @param name
*/
createGroup(name: string) {
const body = SocialService.buildGroupCreateBody(name);
return this.http.post(environment.graphQLUrl, body, {headers: this.headers});
} }
} }

@ -7,12 +7,3 @@ export const environment = {
graphQLUrl: 'https://greenvironment.net/graphql', graphQLUrl: 'https://greenvironment.net/graphql',
greenvironmentUrl: 'https://greenvironment.net/', greenvironmentUrl: 'https://greenvironment.net/',
}; };
/*
* For easier debugging in development mode, you can import the following file
* to ignore zone related error stack frames such as `zone.run`, `zoneDelegate.invokeTask`.
*
* This import should be commented out in production mode because it will have a negative impact
* on performance if an error is thrown.
*/
// import 'zone.js/dist/zone-error'; // Included with Angular CLI.

Loading…
Cancel
Save