From e869d168cb260c0c46c4470eef1f48b436ecefc6 Mon Sep 17 00:00:00 2001 From: lampe_n1 Date: Tue, 29 Oct 2019 16:45:03 +0100 Subject: [PATCH] Chat test --- src/app/components/chat/chat.component.html | 4 +- src/app/components/chat/chat.component.ts | 9 ++-- .../chatlist/chatlist.component.html | 4 +- .../components/chatlist/chatlist.component.ts | 6 +-- .../chatmanager/chatmanager.component.html | 4 +- .../chatmanager/chatmanager.component.ts | 21 +++++--- src/app/services/chat/chat.service.ts | 54 ++++++++++++++++--- 7 files changed, 76 insertions(+), 26 deletions(-) diff --git a/src/app/components/chat/chat.component.html b/src/app/components/chat/chat.component.html index 30f7be2..b29ddc6 100644 --- a/src/app/components/chat/chat.component.html +++ b/src/app/components/chat/chat.component.html @@ -10,7 +10,7 @@
- - + +
\ No newline at end of file diff --git a/src/app/components/chat/chat.component.ts b/src/app/components/chat/chat.component.ts index 92b6c2a..524dc30 100644 --- a/src/app/components/chat/chat.component.ts +++ b/src/app/components/chat/chat.component.ts @@ -16,6 +16,7 @@ export class ChatComponent implements OnInit { new Chatmessage("Hallo", "01.01.",true)] @Output() goBackEvent = new EventEmitter(); + @Output() refreshEvent = new EventEmitter() @Input() childChat: Chat; constructor(private chatService: ChatService) { } @@ -25,12 +26,12 @@ export class ChatComponent implements OnInit { goBack() { this.goBackEvent.emit(true) - this.chatService.getAllChats() } - sendMessage(pContent: string) { - this.chatService.sendMessage(this.childChat.id, pContent) - this.chatService.getMessages(this.childChat.id) + sendMessage(pElement) { + this.chatService.sendMessage(this.childChat.id, pElement.value) + this.refreshEvent.emit(true) + pElement.value = "" } } diff --git a/src/app/components/chatlist/chatlist.component.html b/src/app/components/chatlist/chatlist.component.html index 8186327..be19e38 100644 --- a/src/app/components/chatlist/chatlist.component.html +++ b/src/app/components/chatlist/chatlist.component.html @@ -4,10 +4,10 @@
-
Pic
-
{{chat.id}}
+
{{chat.memberName}}
diff --git a/src/app/components/chatlist/chatlist.component.ts b/src/app/components/chatlist/chatlist.component.ts index 998c1a6..341c809 100644 --- a/src/app/components/chatlist/chatlist.component.ts +++ b/src/app/components/chatlist/chatlist.component.ts @@ -1,4 +1,4 @@ -import { Component, OnInit, Output, EventEmitter } from '@angular/core'; +import { Component, OnInit, Output, EventEmitter, Input } from '@angular/core'; import { Chatinfo } from 'src/app/models/chatinfo'; import { Chat } from 'src/app/models/chat'; import { ChatService } from 'src/app/services/chat/chat.service'; @@ -10,15 +10,15 @@ import { ChatService } from 'src/app/services/chat/chat.service'; }) export class ChatlistComponent implements OnInit { + @Input() childChats: Array @Output() showChatEvent = new EventEmitter(); @Output() showCreateChatEvent = new EventEmitter(); selectedChat: Chat; - chats: Array constructor(private chatService: ChatService) { } ngOnInit() { - this.chats = this.chatService.chats + } showNewChat() { diff --git a/src/app/components/chatmanager/chatmanager.component.html b/src/app/components/chatmanager/chatmanager.component.html index 039d4a1..2be65b3 100644 --- a/src/app/components/chatmanager/chatmanager.component.html +++ b/src/app/components/chatmanager/chatmanager.component.html @@ -1,8 +1,8 @@
- +
- +
diff --git a/src/app/components/chatmanager/chatmanager.component.ts b/src/app/components/chatmanager/chatmanager.component.ts index c92aeee..4d7ebe3 100644 --- a/src/app/components/chatmanager/chatmanager.component.ts +++ b/src/app/components/chatmanager/chatmanager.component.ts @@ -15,23 +15,25 @@ export class ChatmanagerComponent implements OnInit { showChatlist: boolean = true showChat: boolean = false showCreateNewChat: boolean = false - parentSelectedChat: Chat - chatIds: number[] = [] + parentSelectedChat: Chat + parentChats: Array constructor(private data: DatasharingService, private chatService: ChatService) { } ngOnInit() { - this.data.currentChatIDs.subscribe(chatIDs => { - this.chatIds = chatIDs - }) - this.chatService.getChatsByID(this.chatIds) + /*this.data.currentChatIDs.subscribe(chatIDs => { + this.parentChatIds = chatIDs + })*/ + this.refresh() } goBackToChatlist($event) { this.showChatlist = $event this.showChat = false this.showCreateNewChat = false + + this.refresh() } showSpecialChat($event) { @@ -47,4 +49,11 @@ export class ChatmanagerComponent implements OnInit { this.showCreateNewChat = $event } + refresh() { + this.chatService.getAllChatsRaw() + .subscribe(response => { + this.parentChats = this.chatService.renderAllChats(response.json()) + }) + } + } diff --git a/src/app/services/chat/chat.service.ts b/src/app/services/chat/chat.service.ts index dded0b4..01837be 100644 --- a/src/app/services/chat/chat.service.ts +++ b/src/app/services/chat/chat.service.ts @@ -29,11 +29,21 @@ export class ChatService { this.http.post(url, this.getBodyForGetAllChats()) .subscribe(response => { - this.chats = this.updateAllChats(response.json()) + this.chats = this.renderAllChats(response.json()) }); return this.chats } + public getAllChatsRaw(): any { + console.log("Getting all chats ..") + let url = 'https://greenvironment.net/graphql' + + let headers = new Headers() + headers.set('Content-Type', 'application/json') + + return this.http.post(url, this.getBodyForGetAllChats()) + } + public getChatsByID(pChatIDs: number[]): Array { this.chats = [] console.log("Getting chats by ID..") @@ -52,6 +62,23 @@ export class ChatService { return this.chats } + public getChatsByIDRaw(pChatIDs: number[]): any { + console.log("Getting chats by ID..") + + for(let chatId of pChatIDs) { + let url = 'https://greenvironment.net/graphql' + + let headers = new Headers() + headers.set('Content-Type', 'application/json') + + this.http.post(url, this.getBodyForGetChatsByID(chatId)) + .subscribe(response => { + this.updateChat(response.json()) + }) + } + return this.chats + } + public createNewChat(pUserID: number) { let url = 'https://greenvironment.net/graphql' @@ -108,12 +135,21 @@ export class ChatService { this.http.post(url, this.getBodyForGetMessagesInChat(pChatID)).subscribe(response => { console.log("Downloading messages ...") - messages = this.updateMessages(response.json()) + messages = this.renderMessages(response.json()) }) return messages } - updateMessages(pResponse: any): Array { + public getMessagesRaw(pChatID): any { + let url = 'https://greenvironment.net/graphql' + + let headers = new Headers() + headers.set('Content-Type', 'application/json') + + return this.http.post(url, this.getBodyForGetMessagesInChat(pChatID)) + } + + public renderMessages(pResponse: any): Array { let messages = new Array() for(let message of pResponse.data.getChat.messages) { if(message.author.id == this.ownID) { @@ -125,7 +161,7 @@ export class ChatService { return messages } - updateAllChats(pResponse: any): Array { + public renderAllChats(pResponse: any): Array { let chats = Array() for(let chat of pResponse.data.getSelf.chats) { let memberID: number @@ -204,9 +240,13 @@ export class ChatService { getBodyForGetAllChats() { const body = {query: `query { - getUser { - chats(first: 1000, offset: 0) {id, members{name, id}, - messages(first: 1000, offset: 0) {author {id}, createdAt, content}} + getSelf { + chats(first: 1000, offset: 0) { + id, members{name, id}, + messages(first: 1000, offset: 0) { + author {id}, createdAt, content + } + } } }` }