Chat test

master
lampe_n1 5 years ago
parent 317acb8293
commit e869d168cb

@ -10,7 +10,7 @@
</div>
</div>
<div id="newmessage">
<textarea id='input' placeholder="Write message here ..." rows='3' wrap="soft"></textarea>
<button id="send" type='submit'><span><i class="fa fa-send-o fa-2x" aria-hidden="true"></i></span></button>
<textarea #content id='input' placeholder="Write message here ..." rows='3' wrap="soft"></textarea>
<button id="send" type='submit' (click)="sendMessage(content)"><span><i class="fa fa-send-o fa-2x" aria-hidden="true"></i></span></button>
</div>
</div>

@ -16,6 +16,7 @@ export class ChatComponent implements OnInit {
new Chatmessage("Hallo", "01.01.",true)]
@Output() goBackEvent = new EventEmitter<boolean>();
@Output() refreshEvent = new EventEmitter<boolean>()
@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 = ""
}
}

@ -4,10 +4,10 @@
<button id="newchat" (click)="newChat()"><span><i class="fa fa-plus fa-3x" aria-hidden="true"></i></span></button>
</div>
<div id="chats">
<div class="chatitem" *ngFor="let chat of chats"
<div class="chatitem" *ngFor="let chat of childChats"
[class.selected]="chat === selectedChat" (click)="showChat(chat)">
<div class="picture">Pic</div>
<div class="name"><span>{{chat.id}}</span></div>
<div class="name"><span>{{chat.memberName}}</span></div>
<div class="date"> <a>Date of last message</a></div>
</div>
</div>

@ -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<Chat>
@Output() showChatEvent = new EventEmitter<Chat>();
@Output() showCreateChatEvent = new EventEmitter<boolean>();
selectedChat: Chat;
chats: Array<Chat>
constructor(private chatService: ChatService) { }
ngOnInit() {
this.chats = this.chatService.chats
}
showNewChat() {

@ -1,8 +1,8 @@
<div *ngIf="showChatlist">
<chatmanager-chatlist (showChatEvent)="showSpecialChat($event)" (showCreateChatEvent)="showNewChat($event)"></chatmanager-chatlist>
<chatmanager-chatlist (showChatEvent)="showSpecialChat($event)" (showCreateChatEvent)="showNewChat($event)" [childChats]="parentChats"></chatmanager-chatlist>
</div>
<div *ngIf="showChat">
<chatmanager-chat (goBackEvent)="goBackToChatlist($event)" [childChat]="parentSelectedChat"></chatmanager-chat>
<chatmanager-chat (goBackEvent)="goBackToChatlist($event)" (refreshEvent)="refresh()" [childChat]="parentSelectedChat"></chatmanager-chat>
</div>
<div *ngIf="showCreateNewChat">
<chatmanager-chatcontacts (goBackEvent)="goBackToChatlist($event)"></chatmanager-chatcontacts>

@ -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<Chat>
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())
})
}
}

@ -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<Chat> {
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<Chatmessage> {
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<Chatmessage> {
let messages = new Array<Chatmessage>()
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<Chat> {
public renderAllChats(pResponse: any): Array<Chat> {
let chats = Array<Chat>()
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
}
}
}
}`
}

Loading…
Cancel
Save