From 99f1a315ef3ab96040a1ff0209590400e1740f25 Mon Sep 17 00:00:00 2001 From: Trivernis Date: Mon, 20 Jan 2020 09:25:16 +0100 Subject: [PATCH 1/4] Add theme to local storage - Add theme to local storage to apply it on load instead of waiting for the user response --- .../main-navigation/main-navigation.component.ts | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/src/app/components/main-navigation/main-navigation.component.ts b/src/app/components/main-navigation/main-navigation.component.ts index 14a60de..b5db386 100644 --- a/src/app/components/main-navigation/main-navigation.component.ts +++ b/src/app/components/main-navigation/main-navigation.component.ts @@ -56,6 +56,10 @@ export class MainNavigationComponent implements OnInit { @HostBinding('class') componentCssClass; ngOnInit() { + if (this.lighttheme && this.getThemeFromLocalStorage() === 'dark-theme') { + this.toggleTheme(); + this.darkModeButtonChecked = true; + } this.data.currentUserInfo.subscribe(user => { this.user = user; this.loggedIn = user.loggedIn; @@ -70,11 +74,19 @@ export class MainNavigationComponent implements OnInit { // IF user activated darkmode and logged in after that } else if (this.user.loggedIn && !this.user.darkmode && !this.lighttheme) { this.settingsService.setDarkModeActive(true); + this.darkModeButtonChecked = true; } this.updateLinks(); }); } + /** + * Returns the saved theme from the local storage + */ + private getThemeFromLocalStorage(): string { + return localStorage.getItem('theme'); + } + toggleTheme() { if (this.overlay.classList.contains('dark-theme')) { this.overlay.classList.remove('dark-theme'); @@ -108,6 +120,7 @@ export class MainNavigationComponent implements OnInit { onSetTheme(theme) { this.overlayContainer.getContainerElement().classList.add(theme); this.componentCssClass = theme; + localStorage.setItem('theme', theme); } logout() { From 76adff40ebb6af9377404aad7a50c8f8eaa39ce9 Mon Sep 17 00:00:00 2001 From: Trivernis Date: Mon, 20 Jan 2020 10:44:09 +0100 Subject: [PATCH 2/4] Refactor request service - Add http post and postgraphql methods to base service --- src/app/components/login/login.component.ts | 1 + .../main-navigation.component.ts | 73 ++++----- src/app/services/base.service.ts | 29 +++- src/app/services/document.service.spec.ts | 12 -- src/app/services/document.service.ts | 39 ----- src/app/services/feed/feed.service.ts | 10 +- src/app/services/group/group.service.ts | 1 - src/app/services/login/login.service.ts | 26 ++- src/app/services/profile/profile.service.ts | 7 +- src/app/services/request/request.service.ts | 149 ++++++++++-------- src/app/services/search/search.service.ts | 6 +- src/app/services/selfservice/self.service.ts | 9 +- src/app/services/social/social.service.ts | 7 +- 13 files changed, 188 insertions(+), 181 deletions(-) delete mode 100644 src/app/services/document.service.spec.ts delete mode 100644 src/app/services/document.service.ts diff --git a/src/app/components/login/login.component.ts b/src/app/components/login/login.component.ts index bb6313d..16335c2 100644 --- a/src/app/components/login/login.component.ts +++ b/src/app/components/login/login.component.ts @@ -51,6 +51,7 @@ export class LoginComponent implements OnInit { }); }, (error: IErrorResponse) => { if (error.error) { + console.error(error); this.loginError(error); } }); diff --git a/src/app/components/main-navigation/main-navigation.component.ts b/src/app/components/main-navigation/main-navigation.component.ts index b5db386..609d8d5 100644 --- a/src/app/components/main-navigation/main-navigation.component.ts +++ b/src/app/components/main-navigation/main-navigation.component.ts @@ -5,10 +5,10 @@ import {RequestService} from '../../services/request/request.service'; import {SettingsService} from '../../services/settings/settings.service'; import {environment} from 'src/environments/environment'; import {Levellist} from 'src/app/models/levellist'; -import {Http} from '@angular/http'; import {Router} from '@angular/router'; import {User} from 'src/app/models/user'; import {OverlayContainer} from '@angular/cdk/overlay'; +import {LoginService} from '../../services/login/login.service'; @Component({ selector: 'app-main-navigation', @@ -21,9 +21,10 @@ export class MainNavigationComponent implements OnInit { public overlayContainer: OverlayContainer, private data: DatasharingService, private settingsService: SettingsService, - private requestservice: RequestService, + private requestService: RequestService, private breakpointObserver: BreakpointObserver, - private http: Http, private router: Router, + private loginService: LoginService, + private router: Router, ) { this.overlay = overlayContainer.getContainerElement(); } @@ -123,50 +124,46 @@ export class MainNavigationComponent implements OnInit { localStorage.setItem('theme', theme); } + /** + * Logs out + */ logout() { - const url = environment.graphQLUrl; - - const headers = new Headers(); - headers.set('Content-Type', 'application/json'); - const body = { - query: `mutation { - logout - }` - }; - this.http.post(url, body).subscribe(response => { + this.loginService.logout().subscribe(() => { + this.loggedIn = false; + const user = new User(); + user.loggedIn = false; + this.data.changeUserInfo(user); + this.router.navigate(['login']); }); - this.loggedIn = false; - const user = new User(); - user.loggedIn = false; - this.data.changeUserInfo(user); - this.router.navigate(['login']); } + /** + * Accepts a request + * @param id + */ acceptRequest(id: number) { - const headers = new Headers(); - headers.set('Content-Type', 'application/json'); - this.http.post(environment.graphQLUrl, this.requestservice.buildJsonAcceptRequest(id)) - .subscribe(response => { - for (let i = 0; i < this.user.receivedRequests.length; i++) { - if (this.user.receivedRequests[i].senderUserID === id) { - this.user.receivedRequests.splice(i, 1); - return; - } + this.requestService.acceptRequest(id).subscribe(response => { + for (let i = 0; i < this.user.receivedRequests.length; i++) { + if (this.user.receivedRequests[i].senderUserID === id) { + this.user.receivedRequests.splice(i, 1); + return; } - }); + } + }); } + /** + * Denys a request + * @param id + */ denyRequest(id: number) { - const headers = new Headers(); - headers.set('Content-Type', 'application/json'); - this.http.post(environment.graphQLUrl, this.requestservice.buildJsonDenyRequest(id)) - .subscribe(response => { - for (let i = 0; i < this.user.receivedRequests.length; i++) { - if (this.user.receivedRequests[i].senderUserID === id) { - this.user.receivedRequests.splice(i, 1); - return; - } + this.requestService.denyRequest(id).subscribe(() => { + for (let i = 0; i < this.user.receivedRequests.length; i++) { + if (this.user.receivedRequests[i].senderUserID === id) { + this.user.receivedRequests.splice(i, 1); + return; } - }); + } + }); } } diff --git a/src/app/services/base.service.ts b/src/app/services/base.service.ts index d65f950..f0b6700 100644 --- a/src/app/services/base.service.ts +++ b/src/app/services/base.service.ts @@ -1,7 +1,8 @@ import {Injectable} from '@angular/core'; -import { HttpErrorResponse, HttpHeaders} from '@angular/common/http'; -import {delay, mergeMap, retryWhen} from 'rxjs/operators'; +import {HttpClient, HttpHeaders} from '@angular/common/http'; +import {delay, mergeMap, retry, retryWhen} from 'rxjs/operators'; import {Observable, of, throwError} from 'rxjs'; +import {environment} from '../../environments/environment'; const httpTooManyCode = 429; const httpHeaderRateRetry = 'Retry-After'; @@ -11,11 +12,33 @@ const httpHeaderRateRetry = 'Retry-After'; }) export abstract class BaseService { protected headers: HttpHeaders; - protected constructor() { + protected constructor(protected http?: HttpClient) { this.headers = new HttpHeaders(); this.headers.set('Content-Type', 'application/json'); } + /** + * Does a http post request + * @param url + * @param body + * @param options + * @param retryLimit + */ + protected post(url: string, body: any, options?: any, retryLimit: number = 3): Observable { + return this.http.post(url, body, Object.assign({headers: this.headers}, options)) + .pipe(this.retryRated(retryLimit)); + } + + /** + * Does a http post to the graphql url + * @param body + * @param options + * @param retryLimit + */ + protected postGraphql(body: any, options?: any, retryLimit: number = 3): Observable { + return this.post(environment.graphQLUrl, body, options, retryLimit); + } + /** * Retries a request according to the rate limit * @param maxRetry diff --git a/src/app/services/document.service.spec.ts b/src/app/services/document.service.spec.ts deleted file mode 100644 index 3e2e3ca..0000000 --- a/src/app/services/document.service.spec.ts +++ /dev/null @@ -1,12 +0,0 @@ -import {TestBed} from '@angular/core/testing'; - -import {DocumentService} from './document.service'; - -describe('DocumentService', () => { - beforeEach(() => TestBed.configureTestingModule({})); - - it('should be created', () => { - const service: DocumentService = TestBed.get(DocumentService); - expect(service).toBeTruthy(); - }); -}); diff --git a/src/app/services/document.service.ts b/src/app/services/document.service.ts deleted file mode 100644 index 4c6c917..0000000 --- a/src/app/services/document.service.ts +++ /dev/null @@ -1,39 +0,0 @@ -import {Injectable} from '@angular/core'; - -import {Socket} from 'ngx-socket-io'; - -import {Document} from '../models/document'; - -@Injectable({ - providedIn: 'root' -}) -export class DocumentService { - currentDocument = this.socket.fromEvent('document'); - documents = this.socket.fromEvent('documents'); - - constructor(private socket: Socket) { - } - - getDocument(id: string) { - this.socket.emit('getDoc', id); - } - - newDocument() { - this.socket.emit('addDoc', {id: this.docId(), doc: ''}); - } - - editDocument(document: Document) { - this.socket.emit('editDoc', document); - } - - private docId() { - let text = ''; - const possible = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'; - - for (let i = 0; i < 5; i++) { - text += possible.charAt(Math.floor(Math.random() * possible.length)); - } - - return text; - } -} diff --git a/src/app/services/feed/feed.service.ts b/src/app/services/feed/feed.service.ts index 89659bb..0a83b24 100644 --- a/src/app/services/feed/feed.service.ts +++ b/src/app/services/feed/feed.service.ts @@ -99,8 +99,8 @@ export enum Sort { }) export class FeedService extends BaseService { - constructor(private http: HttpClient) { - super(); + constructor(http: HttpClient) { + super(http); } public postsAvailable = new BehaviorSubject(true); @@ -159,7 +159,7 @@ export class FeedService extends BaseService { * @param body */ private createPostRequest(body: { variables: any; query: string }) { - return this.http.post(environment.graphQLUrl, body, {headers: this.headers}) + return this.postGraphql(body, null, 0) .pipe(tap(response => { if (this.activePostList === Sort.NEW) { const updatedPosts = this.posts.getValue(); @@ -180,7 +180,7 @@ export class FeedService extends BaseService { } }; - return this.http.post(environment.graphQLUrl, body, {headers: this.headers}).pipe(this.retryRated()); + return this.postGraphql(body); } /** @@ -194,7 +194,7 @@ export class FeedService extends BaseService { } }; - return this.http.post(environment.graphQLUrl, body, {headers: this.headers}).pipe(this.retryRated()); + return this.postGraphql(body); } /** diff --git a/src/app/services/group/group.service.ts b/src/app/services/group/group.service.ts index 433a16f..bf04a09 100644 --- a/src/app/services/group/group.service.ts +++ b/src/app/services/group/group.service.ts @@ -137,5 +137,4 @@ export class GroupService { }; return this.http.post(environment.graphQLUrl, body); } - } diff --git a/src/app/services/login/login.service.ts b/src/app/services/login/login.service.ts index b88e013..3927540 100644 --- a/src/app/services/login/login.service.ts +++ b/src/app/services/login/login.service.ts @@ -42,13 +42,17 @@ const graphqlQuery = `mutation($email: String!, $pwHash: String!) { } }`; +const logoutGqlQuery = `mutation { + logout + }`; + @Injectable({ providedIn: 'root' }) export class LoginService extends BaseService { - constructor(private http: HttpClient, private datasharingService: DatasharingService) { - super(); + constructor(http: HttpClient, private datasharingService: DatasharingService) { + super(http); } /** @@ -65,17 +69,33 @@ export class LoginService extends BaseService { }; } + /** + * Builds a logout request + */ + public static buildLogoutBody(): any { + return { + query: logoutGqlQuery + }; + } + /** * Performs a login request and returns the data of the logged in user. * @param login */ public login(login: Login) { const body = LoginService.buildRequestBody(login); - return this.http.post(environment.graphQLUrl, body, {headers: this.headers}) + return this.postGraphql(body, null, 0) .pipe(tap(response => { const user = new User(); user.assignFromResponse(response.data.login); this.datasharingService.changeUserInfo(user); })); } + + /** + * Loggs out + */ + public logout() { + return this.postGraphql(LoginService.buildLogoutBody()); + } } diff --git a/src/app/services/profile/profile.service.ts b/src/app/services/profile/profile.service.ts index 3a7b4ca..809cb71 100644 --- a/src/app/services/profile/profile.service.ts +++ b/src/app/services/profile/profile.service.ts @@ -51,8 +51,8 @@ const graphqlGetProfileQuery = `query($userId: ID) { }) export class ProfileService extends BaseService { - constructor(private http: HttpClient) { - super(); + constructor(http: HttpClient) { + super(http); } public proflile: Subject = new Subject(); @@ -77,8 +77,7 @@ export class ProfileService extends BaseService { public getUserData(userId: string) { const headers = new Headers(); headers.set('Content-Type', 'application/json'); - this.http.post(environment.graphQLUrl, ProfileService.buildGetProfileBody(userId)) - .pipe(this.retryRated()) + this.postGraphql(ProfileService.buildGetProfileBody(userId)) .subscribe(result => { this.proflile.next(this.getProfileData(result)); return this.proflile; diff --git a/src/app/services/request/request.service.ts b/src/app/services/request/request.service.ts index d3904c9..94ecdc3 100644 --- a/src/app/services/request/request.service.ts +++ b/src/app/services/request/request.service.ts @@ -1,18 +1,74 @@ 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 {User} from 'src/app/models/user'; import {GroupInfo} from 'src/app/models/groupinfo'; +import {HttpClient} from '@angular/common/http'; +import {BaseService} from '../base.service'; +const denyRequestGqlQuery = `mutation($id: ID!) { + denyRequest(sender: $id, type: FRIENDREQUEST) +}`; + +const acceptRequestGqlQuery = `mutation($id: ID!) { + acceptRequest(sender: $id, type: FRIENDREQUEST) +}`; + +const sendRequestGqlQuery = `mutation($id: ID!, $type: RequestType) { + sendRequest(receiver: $id, type: $type) { + id + } +}`; + @Injectable({ providedIn: 'root' }) -export class RequestService { +export class RequestService extends BaseService { + + constructor(http: HttpClient, private data: DatasharingService, private router: Router) { + super(http); + } + + private static buildDenyRequestBody(id: number): any { + return { + query: denyRequestGqlQuery + , variables: { + id + } + }; + } + + private static buildAcceptRequestBody(id: number): any { + return { + query: acceptRequestGqlQuery + , variables: { + id + } + }; + } - constructor(private http: Http, private data: DatasharingService, private router: Router) { + private static buildJoinGroupBody(id: number): any { + return { + query: `mutation($id: ID!) { + joinGroup(id: $id) { + id + } + }` + , variables: { + id + } + }; + } + + private static buildSendRequestBody(id: number, type: String): any { + return { + query: sendRequestGqlQuery + , variables: { + id, + type + } + }; } public isAllowedToSendRequest(userID: number, self: User): boolean { @@ -55,74 +111,39 @@ export class RequestService { return true; } + /** + * Sends a send request + * @param user + */ public sendFriendRequest(user: User) { - this.data.addSentRequestUserID(user.userID); - const headers = new Headers(); - headers.set('Content-Type', 'application/json'); - this.http.post(environment.graphQLUrl, this.buildJsonRequest(user.userID, 'FRIENDREQUEST')) - .subscribe(response => { + this.postGraphql(RequestService.buildSendRequestBody(user.userID, 'FRIENDREQUEST')) + .subscribe(() => { + this.data.addSentRequestUserID(user.userID); }); } + /** + * Joins a group + * @param group + */ public joinGroup(group: GroupInfo) { - const headers = new Headers(); - headers.set('Content-Type', 'application/json'); - this.http.post(environment.graphQLUrl, this.buildJsonJoinGroup(group.id)) - .subscribe(response => { - }); + this.postGraphql(RequestService.buildJoinGroupBody(group.id)) + .subscribe(); } - public buildJsonRequest(id_: number, type_: String): any { - const body = { - query: `mutation($id: ID!, $type: RequestType) { - sendRequest(receiver: $id, type: $type) { - id - } - }` - , variables: { - id: id_, - type: type_ - } - }; - return body; + /** + * Accepts a request + * @param id + */ + public acceptRequest(id: number) { + return this.postGraphql(RequestService.buildAcceptRequestBody(id)); } - public buildJsonJoinGroup(id_: number): any { - const body = { - query: `mutation($id: ID!) { - joinGroup(id: $id) { - id - } - }` - , variables: { - id: id_ - } - }; - return body; + /** + * Denys a request + * @param id + */ + public denyRequest(id: number) { + return this.postGraphql(RequestService.buildDenyRequestBody(id)); } - - public buildJsonAcceptRequest(id_: number): any { - const body = { - query: `mutation($id: ID!) { - acceptRequest(sender: $id, type: FRIENDREQUEST) - }` - , variables: { - id: id_ - } - }; - return body; - } - - public buildJsonDenyRequest(id_: number): any { - const body = { - query: `mutation($id: ID!) { - denyRequest(sender: $id, type: FRIENDREQUEST) - }` - , variables: { - id: id_ - } - }; - return body; - } - } diff --git a/src/app/services/search/search.service.ts b/src/app/services/search/search.service.ts index c86ff37..dff3faa 100644 --- a/src/app/services/search/search.service.ts +++ b/src/app/services/search/search.service.ts @@ -41,8 +41,8 @@ const graphqlQuery = `query($query: String!, $first: Int, $offset: Int) { providedIn: 'root' }) export class SearchService extends BaseService { - constructor(private http: HttpClient, private data: DatasharingService, private router: Router) { - super(); + constructor(http: HttpClient, private data: DatasharingService, private router: Router) { + super(http); } /** @@ -101,7 +101,7 @@ export class SearchService extends BaseService { */ public search(query: string): Observable { const body = SearchService.buildRequestBody(query); - return this.http.post(environment.graphQLUrl, body, {headers: this.headers}) + return this.postGraphql(body) .pipe(this.retryRated()); } } diff --git a/src/app/services/selfservice/self.service.ts b/src/app/services/selfservice/self.service.ts index 283663b..8ad87e5 100644 --- a/src/app/services/selfservice/self.service.ts +++ b/src/app/services/selfservice/self.service.ts @@ -40,8 +40,8 @@ const getSelfGraphqlQuery = `{ }) export class SelfService extends BaseService { - constructor(private http: HttpClient, private data: DatasharingService) { - super(); + constructor(http: HttpClient, private data: DatasharingService) { + super(http); } /** @@ -61,8 +61,7 @@ export class SelfService extends BaseService { const headers = new Headers(); headers.set('Content-Type', 'application/json'); - return this.http.post(url, SelfService.buildGetSelfBody(), {headers: this.headers}) - .pipe(this.retryRated()) + return this.postGraphql(SelfService.buildGetSelfBody()) .pipe(tap(response => { this.updateUserInfo(response); })); @@ -75,7 +74,7 @@ export class SelfService extends BaseService { public changeProfilePicture(file: any) { const formData: any = new FormData(); formData.append('profilePicture', file); - return this.http.post(environment.greenvironmentUrl + '/upload', formData) + return this.post(environment.greenvironmentUrl + '/upload', formData, null, 0) .pipe(this.retryRated()); } diff --git a/src/app/services/social/social.service.ts b/src/app/services/social/social.service.ts index 872d36a..2dc1c56 100644 --- a/src/app/services/social/social.service.ts +++ b/src/app/services/social/social.service.ts @@ -14,8 +14,8 @@ const graphqlCreateGroupQuery = `mutation($name: String!) { }) export class SocialService extends BaseService { - constructor(private http: HttpClient) { - super(); + constructor(http: HttpClient) { + super(http); } /** @@ -36,7 +36,6 @@ export class SocialService extends BaseService { */ createGroup(name: string) { const body = SocialService.buildGroupCreateBody(name); - return this.http.post(environment.graphQLUrl, body, {headers: this.headers}) - .pipe(this.retryRated()); + return this.postGraphql(body); } } From 161c3f2afdfeeb19b77bfd2db8155ad3bd2bd1ce Mon Sep 17 00:00:00 2001 From: Trivernis Date: Mon, 20 Jan 2020 10:56:57 +0100 Subject: [PATCH 3/4] Fix Group creation, event join etc. --- src/app/components/group/dialog.html | 1 + src/app/components/group/group.component.ts | 36 +++++++++++++++++---- src/app/services/group/group.service.ts | 17 ++++------ 3 files changed, 37 insertions(+), 17 deletions(-) diff --git a/src/app/components/group/dialog.html b/src/app/components/group/dialog.html index 719cdd4..64e40c9 100644 --- a/src/app/components/group/dialog.html +++ b/src/app/components/group/dialog.html @@ -12,6 +12,7 @@ +{{getErrorMessage()}}
diff --git a/src/app/components/group/group.component.ts b/src/app/components/group/group.component.ts index bc29621..24c9719 100644 --- a/src/app/components/group/group.component.ts +++ b/src/app/components/group/group.component.ts @@ -1,5 +1,5 @@ import {Component, OnInit, ViewChild} from '@angular/core'; -import {NavigationEnd, Router} from '@angular/router'; +import {Data, NavigationEnd, Router} from '@angular/router'; import {User} from 'src/app/models/user'; import {MatSort} from '@angular/material/sort'; import {RequestService} from 'src/app/services/request/request.service'; @@ -17,11 +17,14 @@ import {DialogGroupFileUploadComponent} from './fileUpload/fileUpload.component' }) export class DialogCreateEventComponent { groupId: string; + private errorMessage: string; + errorOccurred: boolean; constructor( public dialogRef: MatDialogRef, private group: GroupService, - private router: Router) { + private router: Router, + private datasharingService: DatasharingService) { this.groupId = this.router.url.substr(this.router.url.lastIndexOf('/') + 1); } @@ -29,15 +32,36 @@ export class DialogCreateEventComponent { this.dialogRef.close(); } + /** + * Creates a new event + * @param name + * @param date + * @param time + */ createEvent(name: string, date: string, time: string) { name = name.trim(); + this.errorOccurred = false; if (name && date && time) { date = date + ' ' + time; - this.group.createEvent(name, (new Date(date)).getTime().toString(), this.groupId); - this.dialogRef.close(); + this.group.createEvent(name, (new Date(date)).getTime().toString(), this.groupId) + .subscribe(() => { + this.dialogRef.close(); + }, (error) => { + if (error.error) { + this.errorMessage = error.error.errors[0].message; + this.errorOccurred = true; + } + }); } } + /** + * Returns the error message + */ + getErrorMessage(): string { + return this.errorMessage; + } + } // GROUP COMPONENT @@ -132,14 +156,14 @@ export class GroupComponent implements OnInit { public joinEvent(event: Event) { this.groupService.joinEvent(event.id).subscribe(response => { - const pEvent = response.json().data.joinEvent; + const pEvent = response.data.joinEvent; event.joined = pEvent.joined; }); } public leaveEvent(event: Event) { this.groupService.leaveEvent(event.id).subscribe(response => { - const pEvent = response.json().data.leaveEvent; + const pEvent = response.data.leaveEvent; event.joined = pEvent.joined; }); } diff --git a/src/app/services/group/group.service.ts b/src/app/services/group/group.service.ts index b4a9f20..2af61ac 100644 --- a/src/app/services/group/group.service.ts +++ b/src/app/services/group/group.service.ts @@ -29,8 +29,8 @@ export class GroupService extends BaseService { public group: BehaviorSubject = new BehaviorSubject(new Group()); - constructor(private http: HttpClient) { - super(); + constructor(http: HttpClient) { + super(http); } /** @@ -70,8 +70,7 @@ export class GroupService extends BaseService { } }; - this.http.post(environment.graphQLUrl, body, {headers: this.headers}) - .pipe(this.retryRated()) + return this.postGraphql(body, null, 0) .pipe(tap(response => { const event = new Event(); event.assignFromResponse(response.data.createEvent); @@ -82,8 +81,6 @@ export class GroupService extends BaseService { } public joinEvent(eventId: string) { - const headers = new Headers(); - headers.set('Content-Type', 'application/json'); const body = { query: `mutation($eventId: ID!) { joinEvent(eventId: $eventId) { @@ -93,7 +90,7 @@ export class GroupService extends BaseService { eventId: eventId } }; - return this.http.post(environment.graphQLUrl, body, {headers: this.headers}) + return this.postGraphql(body) .pipe(this.retryRated()); } @@ -109,15 +106,13 @@ export class GroupService extends BaseService { eventId: eventId } }; - return this.http.post(environment.graphQLUrl, body, {headers: this.headers}) - .pipe(this.retryRated()); + return this.postGraphql(body); } public changeProfilePicture(file: any, id: number) { const formData: any = new FormData(); formData.append('groupPicture', file); formData.append('groupId', id); - return this.http.post(environment.greenvironmentUrl + '/upload', formData) - .pipe(this.retryRated()); + return this.post(environment.greenvironmentUrl + '/upload', formData); } } From 42f4a737b290551942d35a95ba499202a322dbe8 Mon Sep 17 00:00:00 2001 From: Trivernis Date: Mon, 20 Jan 2020 11:11:42 +0100 Subject: [PATCH 4/4] Refactor datasharing service --- src/app/app.component.ts | 2 +- src/app/components/feed/feed.component.ts | 2 +- src/app/components/group/group.component.ts | 15 +++++--- src/app/components/home/home.component.ts | 2 +- .../main-navigation.component.ts | 4 +- .../components/profile/profile.component.ts | 2 +- src/app/components/search/search.component.ts | 2 +- .../social/friends/friends.component.ts | 2 +- .../social/groups/groups.component.sass | 1 + .../social/groups/groups.component.ts | 2 +- src/app/services/chat/chat.service.ts | 2 +- src/app/services/datasharing.service.ts | 38 ++++++++++++------- src/app/services/request/request.service.ts | 3 +- 13 files changed, 45 insertions(+), 32 deletions(-) diff --git a/src/app/app.component.ts b/src/app/app.component.ts index 5e15e30..9f11034 100644 --- a/src/app/app.component.ts +++ b/src/app/app.component.ts @@ -13,7 +13,7 @@ export class AppComponent implements OnInit { } ngOnInit() { - this.data.currentUserInfo.subscribe(user => { + this.data.currentUser.subscribe(user => { if (user.loggedIn !== true) { this.selfservice.checkIfLoggedIn().subscribe(); } diff --git a/src/app/components/feed/feed.component.ts b/src/app/components/feed/feed.component.ts index 1ceb1d9..d7d85b8 100644 --- a/src/app/components/feed/feed.component.ts +++ b/src/app/components/feed/feed.component.ts @@ -39,7 +39,7 @@ export class FeedComponent implements OnInit { } ngOnInit() { - this.data.currentUserInfo.subscribe(user => { + this.data.currentUser.subscribe(user => { this.user = user; this.loggedIn = user.loggedIn; }); diff --git a/src/app/components/group/group.component.ts b/src/app/components/group/group.component.ts index 24c9719..a8cc934 100644 --- a/src/app/components/group/group.component.ts +++ b/src/app/components/group/group.component.ts @@ -23,8 +23,7 @@ export class DialogCreateEventComponent { constructor( public dialogRef: MatDialogRef, private group: GroupService, - private router: Router, - private datasharingService: DatasharingService) { + private router: Router) { this.groupId = this.router.url.substr(this.router.url.lastIndexOf('/') + 1); } @@ -44,7 +43,7 @@ export class DialogCreateEventComponent { if (name && date && time) { date = date + ' ' + time; this.group.createEvent(name, (new Date(date)).getTime().toString(), this.groupId) - .subscribe(() => { + .subscribe((response) => { this.dialogRef.close(); }, (error) => { if (error.error) { @@ -85,7 +84,8 @@ export class GroupComponent implements OnInit { public dialog: MatDialog, private requestService: RequestService, private data: DatasharingService, - private groupService: GroupService) { + private groupService: GroupService, + private datasharingService: DatasharingService) { router.events.forEach((event) => { // check if url changes if (event instanceof NavigationEnd) { @@ -103,7 +103,7 @@ export class GroupComponent implements OnInit { ngOnInit() { this.loading = true; this.id = this.router.url.substr(this.router.url.lastIndexOf('/') + 1); - this.data.currentUserInfo.subscribe(user => { + this.data.currentUser.subscribe(user => { this.self = user; }); this.groupService.getGroupData(this.id).subscribe(); @@ -151,7 +151,10 @@ export class GroupComponent implements OnInit { public joinGroup(group: Group) { group.allowedToJoinGroup = false; - this.requestService.joinGroup(group); + this.requestService.joinGroup(group) + .subscribe(() => { + this.datasharingService.addGroupToUser(group); + }); } public joinEvent(event: Event) { diff --git a/src/app/components/home/home.component.ts b/src/app/components/home/home.component.ts index 1e80db3..d4a3951 100644 --- a/src/app/components/home/home.component.ts +++ b/src/app/components/home/home.component.ts @@ -15,7 +15,7 @@ export class HomeComponent implements OnInit { } ngOnInit() { - this.data.currentUserInfo.subscribe(user => { + this.data.currentUser.subscribe(user => { this.loggedIn = user.loggedIn; }); } diff --git a/src/app/components/main-navigation/main-navigation.component.ts b/src/app/components/main-navigation/main-navigation.component.ts index 609d8d5..2990672 100644 --- a/src/app/components/main-navigation/main-navigation.component.ts +++ b/src/app/components/main-navigation/main-navigation.component.ts @@ -61,7 +61,7 @@ export class MainNavigationComponent implements OnInit { this.toggleTheme(); this.darkModeButtonChecked = true; } - this.data.currentUserInfo.subscribe(user => { + this.data.currentUser.subscribe(user => { this.user = user; this.loggedIn = user.loggedIn; this.userId = user.userID; @@ -132,7 +132,7 @@ export class MainNavigationComponent implements OnInit { this.loggedIn = false; const user = new User(); user.loggedIn = false; - this.data.changeUserInfo(user); + this.data.currentUser.next(user); this.router.navigate(['login']); }); } diff --git a/src/app/components/profile/profile.component.ts b/src/app/components/profile/profile.component.ts index 5242f09..9f92026 100644 --- a/src/app/components/profile/profile.component.ts +++ b/src/app/components/profile/profile.component.ts @@ -49,7 +49,7 @@ export class ProfileComponent implements OnInit { ngOnInit() { this.loading = true; this.id = this.router.url.substr(this.router.url.lastIndexOf('/') + 1); - this.data.currentUserInfo.subscribe(user => { + this.data.currentUser.subscribe(user => { this.self = user; }); this.profileService.getUserData(this.id); diff --git a/src/app/components/search/search.component.ts b/src/app/components/search/search.component.ts index bc379ea..9dd92f8 100644 --- a/src/app/components/search/search.component.ts +++ b/src/app/components/search/search.component.ts @@ -27,7 +27,7 @@ export class SearchComponent implements OnInit { } ngOnInit() { - this.data.currentUserInfo.subscribe(user => { + this.data.currentUser.subscribe(user => { this.user = user; }); } diff --git a/src/app/components/social/friends/friends.component.ts b/src/app/components/social/friends/friends.component.ts index 6ef08f0..4cd3840 100644 --- a/src/app/components/social/friends/friends.component.ts +++ b/src/app/components/social/friends/friends.component.ts @@ -16,7 +16,7 @@ export class FriendsComponent implements OnInit { } ngOnInit() { - this.data.currentUserInfo.subscribe(user => { + this.data.currentUser.subscribe(user => { this.user = user; }); } diff --git a/src/app/components/social/groups/groups.component.sass b/src/app/components/social/groups/groups.component.sass index d7f5f1a..3e12dce 100644 --- a/src/app/components/social/groups/groups.component.sass +++ b/src/app/components/social/groups/groups.component.sass @@ -11,6 +11,7 @@ margin-top: 0.5em outline: none user-select: none + cursor: pointer ::ng-deep .mat-card-header-text width: 1000% margin: auto 0 auto 24px diff --git a/src/app/components/social/groups/groups.component.ts b/src/app/components/social/groups/groups.component.ts index 4448a75..6097462 100644 --- a/src/app/components/social/groups/groups.component.ts +++ b/src/app/components/social/groups/groups.component.ts @@ -54,7 +54,7 @@ export class GroupsComponent implements OnInit { } ngOnInit() { - this.data.currentUserInfo.subscribe(user => { + this.data.currentUser.subscribe(user => { this.user = user; }); } diff --git a/src/app/services/chat/chat.service.ts b/src/app/services/chat/chat.service.ts index be92ac2..14a5c82 100644 --- a/src/app/services/chat/chat.service.ts +++ b/src/app/services/chat/chat.service.ts @@ -16,7 +16,7 @@ export class ChatService { chats: Array = []; constructor(private http: Http, private data: DatasharingService) { - this.data.currentUserInfo.subscribe(user => { + this.data.currentUser.subscribe(user => { this.ownID = user.userID; }); } diff --git a/src/app/services/datasharing.service.ts b/src/app/services/datasharing.service.ts index 160cc66..9f21f30 100644 --- a/src/app/services/datasharing.service.ts +++ b/src/app/services/datasharing.service.ts @@ -1,37 +1,47 @@ import {Injectable} from '@angular/core'; import {BehaviorSubject} from 'rxjs'; import {User} from '../models/user'; +import {FriendInfo} from '../models/friendinfo'; +import {Group} from '../models/group'; +import {GroupInfo} from '../models/groupinfo'; @Injectable({ providedIn: 'root' }) export class DatasharingService { - private userInfoSource = new BehaviorSubject(new User()); - private chatIDsSource = new BehaviorSubject(new Array()); - currentUserInfo = this.userInfoSource.asObservable(); - currentChatIDs = this.chatIDsSource.asObservable(); + currentUser = new BehaviorSubject(new User()); constructor() { } - changeUserInfo(pUserInfo: User) { - this.userInfoSource.next(pUserInfo); - } - addSentRequestUserID(id: number) { - const user: User = this.userInfoSource.getValue(); + const user: User = this.currentUser.getValue(); user.sentRequestUserIDs.push(id); - this.changeUserInfo(user); + this.currentUser.next(user); + } + + addGroupToUser(group: GroupInfo) { + const user = this.currentUser.getValue(); + user.groups.push(group); + user.groupCount++; + this.currentUser.next(user); } + addFriendToUser(friend: FriendInfo) { + const user = this.currentUser.getValue(); + user.friends.push(friend); + user.friendCount++; + this.currentUser.next(user); +} + setDarkMode(active: boolean) { - const user: User = this.userInfoSource.getValue(); + const user: User = this.currentUser.getValue(); user.darkmode = active; - this.changeUserInfo(user); + this.currentUser.next(user); } - changeChatIDs(pChatIDs: number[]) { - this.chatIDsSource.next(pChatIDs); + changeUserInfo(user: User) { + this.currentUser.next(user); } } diff --git a/src/app/services/request/request.service.ts b/src/app/services/request/request.service.ts index 94ecdc3..bb949da 100644 --- a/src/app/services/request/request.service.ts +++ b/src/app/services/request/request.service.ts @@ -127,8 +127,7 @@ export class RequestService extends BaseService { * @param group */ public joinGroup(group: GroupInfo) { - this.postGraphql(RequestService.buildJoinGroupBody(group.id)) - .subscribe(); + return this.postGraphql(RequestService.buildJoinGroupBody(group.id)); } /**