From ad62a29affff10bc24b78fa263b3486bf14bf8b2 Mon Sep 17 00:00:00 2001 From: Max Date: Tue, 7 Jan 2020 20:09:31 +0100 Subject: [PATCH] added group creation --- src/app/app.module.ts | 7 ++- .../social/friends/friends.component.html | 4 +- .../social/friends/friends.component.ts | 3 +- src/app/components/social/groups/dialog.html | 10 ++++ .../social/groups/groups.component.html | 15 ++---- .../social/groups/groups.component.ts | 51 ++++++++++++++----- .../components/social/social.component.html | 4 +- .../components/social/social.component.sass | 8 --- src/app/models/groupinfo.ts | 4 +- src/app/models/user.ts | 3 +- src/app/services/login/login.service.ts | 9 +++- src/app/services/register/register.service.ts | 2 +- src/app/services/selfservice/self.service.ts | 10 +++- .../services/social/social.service.spec.ts | 12 +++++ src/app/services/social/social.service.ts | 37 ++++++++++++++ 15 files changed, 130 insertions(+), 49 deletions(-) create mode 100644 src/app/components/social/groups/dialog.html create mode 100644 src/app/services/social/social.service.spec.ts create mode 100644 src/app/services/social/social.service.ts diff --git a/src/app/app.module.ts b/src/app/app.module.ts index 9ddc11e..d1334b0 100644 --- a/src/app/app.module.ts +++ b/src/app/app.module.ts @@ -17,6 +17,7 @@ import { FeedComponent } from './components/feed/feed.component'; import { HomeComponent } from './components/home/home.component'; import { SocialComponent } from './components/social/social.component'; import { GroupsComponent } from './components/social/groups/groups.component'; +import { DialogCreateGroupComponent } from './components/social/groups/groups.component'; import { ChatmanagerComponent } from './components/chatmanager/chatmanager.component'; import { ChatlistComponent } from './components/chatlist/chatlist.component'; import { PostlistComponent } from './components/feed/postlist/postlist.component'; @@ -96,7 +97,8 @@ const appRoutes: Routes = [ AboutComponent, ProfileComponent, MainNavigationComponent, - SearchComponent + SearchComponent, + DialogCreateGroupComponent ], imports: [ BrowserModule, @@ -133,8 +135,9 @@ const appRoutes: Routes = [ MatSortModule, MatBadgeModule, MatProgressSpinnerModule, - MatDialogModule + MatDialogModule, ], + entryComponents: [ DialogCreateGroupComponent ], providers: [], bootstrap: [AppComponent] }) diff --git a/src/app/components/social/friends/friends.component.html b/src/app/components/social/friends/friends.component.html index abd53a4..6fe2159 100644 --- a/src/app/components/social/friends/friends.component.html +++ b/src/app/components/social/friends/friends.component.html @@ -1,9 +1,7 @@ Friends - +
{ diff --git a/src/app/components/social/groups/dialog.html b/src/app/components/social/groups/dialog.html new file mode 100644 index 0000000..f3f8237 --- /dev/null +++ b/src/app/components/social/groups/dialog.html @@ -0,0 +1,10 @@ +

Create a new group!

+
+ + + +
+
+ + +
diff --git a/src/app/components/social/groups/groups.component.html b/src/app/components/social/groups/groups.component.html index ea64cf3..6076fd9 100644 --- a/src/app/components/social/groups/groups.component.html +++ b/src/app/components/social/groups/groups.component.html @@ -1,22 +1,13 @@ - Groups
+
- - +
{{group.name}} diff --git a/src/app/components/social/groups/groups.component.ts b/src/app/components/social/groups/groups.component.ts index 83f1d99..2b2d31f 100644 --- a/src/app/components/social/groups/groups.component.ts +++ b/src/app/components/social/groups/groups.component.ts @@ -1,27 +1,54 @@ -import { Component, OnInit, Inject } from '@angular/core'; +import { Component, Inject, OnInit } from '@angular/core'; import { GroupInfo } from 'src/app/models/groupinfo'; -import {MatDialog, MatDialogRef, MAT_DIALOG_DATA} from '@angular/material/dialog'; +import {MatDialog, MatDialogRef} from '@angular/material/dialog'; +import { SocialService } from 'src/app/services/social/social.service'; +import { User } from 'src/app/models/user'; +import { DatasharingService } from 'src/app/services/datasharing.service'; -export interface DialogData { - animal: string; - name: string; +// DIALOG COMPONENT to create groups +@Component({ + selector: 'dialog-overview-example-dialog', + templateUrl: 'dialog.html', +}) +export class DialogCreateGroupComponent { + + constructor( + public dialogRef: MatDialogRef, private social: SocialService) {} + + onNoClick(): void { + this.dialogRef.close(); + } + + createGroup(name: string) { + console.log('create groupe ' + name); + name = name.trim(); + if (name) { + this.social.createGroup(name); + this.dialogRef.close(); + } + + } } +// GROUP COMPONENT @Component({ selector: 'social-groups', templateUrl: './groups.component.html', styleUrls: ['./groups.component.sass'] }) export class GroupsComponent implements OnInit { - // TODO: replace with actual logic that loads the groups from the backend - groups: Array = [ - new GroupInfo(1, 'Group 1', []), - new GroupInfo(1, 'Group 2', []), - new GroupInfo(1, 'Group 3', []), - new GroupInfo(1, 'Group 4', [])]; - constructor() { } + user: User; + constructor(public dialog: MatDialog, private data: DatasharingService) { } ngOnInit() { + this.data.currentUserInfo.subscribe(user => { + this.user = user; }); } + openDialog(): void { + const dialogRef = this.dialog.open(DialogCreateGroupComponent, { + width: '250px' + }); + } } + diff --git a/src/app/components/social/social.component.html b/src/app/components/social/social.component.html index 1ae417f..493cc55 100644 --- a/src/app/components/social/social.component.html +++ b/src/app/components/social/social.component.html @@ -4,7 +4,9 @@ people - +
+ +
diff --git a/src/app/components/social/social.component.sass b/src/app/components/social/social.component.sass index baf32af..b05eb04 100644 --- a/src/app/components/social/social.component.sass +++ b/src/app/components/social/social.component.sass @@ -2,16 +2,8 @@ @import '../../../styles/vars.sass' #friendscontainer - box-sizing: content-box - height: 50% - width: 100% - overflow: auto #groupscontainer - box-sizing: content-box - height: 50% - width: 100% - overflow: auto #tabs /deep/ .mat-tab-label diff --git a/src/app/models/groupinfo.ts b/src/app/models/groupinfo.ts index 9220e21..a074422 100644 --- a/src/app/models/groupinfo.ts +++ b/src/app/models/groupinfo.ts @@ -1,11 +1,9 @@ export class GroupInfo { id: number; name: string; - members: number[]; - constructor(pId: number, pName: string, pMembers: number[]) { + constructor(pId: number, pName: string) { this.id = pId; this.name = pName; - this.members = pMembers; } } diff --git a/src/app/models/user.ts b/src/app/models/user.ts index a641ad7..b42a8b5 100644 --- a/src/app/models/user.ts +++ b/src/app/models/user.ts @@ -1,5 +1,6 @@ import { FriendRequest } from 'src/app/models/friendRequest'; import { FriendInfo } from 'src/app/models/friendinfo'; +import { GroupInfo } from 'src/app/models/groupinfo'; export class User { loggedIn = false; @@ -14,7 +15,7 @@ export class User { darkmode = false; friends: FriendInfo[] = new Array(); - groupIDs: number[]; + groups: GroupInfo[] = new Array(); chatIDs: number[]; receivedRequests: FriendRequest[] = new Array(); sentRequestUserIDs: number[] = new Array(); // IDs of users that already received requests of the logged in user diff --git a/src/app/services/login/login.service.ts b/src/app/services/login/login.service.ts index fff311a..f11d13c 100644 --- a/src/app/services/login/login.service.ts +++ b/src/app/services/login/login.service.ts @@ -7,6 +7,7 @@ import {Router} from '@angular/router'; import {environment} from 'src/environments/environment'; import { FriendRequest } from 'src/app/models/friendRequest'; import { FriendInfo } from 'src/app/models/friendinfo'; +import { GroupInfo } from 'src/app/models/groupinfo'; @Injectable({ providedIn: 'root' @@ -48,7 +49,10 @@ export class LoginService { for (const friend of response.data.login.friends) { user.friends.push(new FriendInfo(friend.id, friend.name, friend.level)); } - user.groupIDs = response.data.login.groups; + for (const group of response.data.login.groups) { + console.log(group.name); + user.groups.push(new GroupInfo(group.id, group.name)); + } user.chatIDs = response.data.login.chats; for (const request of response.data.login.sentRequests) { user.sentRequestUserIDs.push(request.receiver.id); @@ -85,7 +89,8 @@ export class LoginService { level }, groups { - id + id, + name }, chats{ id diff --git a/src/app/services/register/register.service.ts b/src/app/services/register/register.service.ts index 8d08ec9..e024306 100644 --- a/src/app/services/register/register.service.ts +++ b/src/app/services/register/register.service.ts @@ -51,7 +51,7 @@ export class RegisterService { for (const friend of response.data.register.friends) { user.friends.push(new FriendInfo(friend.id, friend.name, friend.level)); } - user.groupIDs = response.data.register.groups; + // user.groupIDs = response.data.register.groups; user.chatIDs = response.data.register.chats; for (const request of response.data.register.sentRequests) { user.sentRequestUserIDs.push(request.receiver.id); diff --git a/src/app/services/selfservice/self.service.ts b/src/app/services/selfservice/self.service.ts index e361551..f5fa47f 100644 --- a/src/app/services/selfservice/self.service.ts +++ b/src/app/services/selfservice/self.service.ts @@ -8,6 +8,7 @@ import {Router} from '@angular/router'; import { environment } from 'src/environments/environment'; import { FriendRequest } from 'src/app/models/friendRequest'; import { FriendInfo } from 'src/app/models/friendinfo'; +import { GroupInfo } from 'src/app/models/groupinfo'; @Injectable({ providedIn: 'root' @@ -32,6 +33,7 @@ export class SelfService { }, error => { this.notLoggedIn(); console.log(error.text()); + // this.fakeLogin(); } ); } @@ -56,7 +58,10 @@ export class SelfService { for (const friend of response.data.getSelf.friends) { user.friends.push(new FriendInfo(friend.id, friend.name, friend.level)); } - user.groupIDs = response.data.getSelf.groups; + for (const group of response.data.getSelf.groups) { + console.log(group.name); + user.groups.push(new GroupInfo(group.id, group.name)); + } user.chatIDs = response.data.getSelf.chats; for (const request of response.data.getSelf.sentRequests) { user.sentRequestUserIDs.push(request.receiver.id); @@ -113,7 +118,8 @@ export class SelfService { level }, groups { - id + id, + name }, chats{ id diff --git a/src/app/services/social/social.service.spec.ts b/src/app/services/social/social.service.spec.ts new file mode 100644 index 0000000..6bcfcff --- /dev/null +++ b/src/app/services/social/social.service.spec.ts @@ -0,0 +1,12 @@ +import { TestBed } from '@angular/core/testing'; + +import { SocialService } from './social.service'; + +describe('SocialService', () => { + beforeEach(() => TestBed.configureTestingModule({})); + + it('should be created', () => { + const service: SocialService = TestBed.get(SocialService); + expect(service).toBeTruthy(); + }); +}); diff --git a/src/app/services/social/social.service.ts b/src/app/services/social/social.service.ts new file mode 100644 index 0000000..4a4821b --- /dev/null +++ b/src/app/services/social/social.service.ts @@ -0,0 +1,37 @@ +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'; + +@Injectable({ + providedIn: 'root' +}) +export class SocialService { + + users: Array; + constructor(private http: Http, private data: DatasharingService, private router: Router) { + } + + createGroup(name: string) { + const headers = new Headers(); + 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 = { + query: `mutation($name: String!) { + createGroup(name: $name) { + id + } + }` + , variables: { + name: name_ + } + }; + return body; + } +}