Files
doneit-web/src/app/pages/chat/group-messages/group-messages.page.ts
T

224 lines
5.9 KiB
TypeScript
Raw Normal View History

2021-03-12 11:56:54 +01:00
import { Component, OnInit } from '@angular/core';
2021-01-19 16:10:40 +01:00
import { ActionSheetController, MenuController, ModalController, NavParams, PopoverController } from '@ionic/angular';
2021-04-13 14:14:55 +01:00
import { AlertService } from 'src/app/services/alert.service';
2021-01-19 16:10:40 +01:00
import { AuthService } from 'src/app/services/auth.service';
import { ChatService } from 'src/app/services/chat.service';
2020-12-29 12:40:19 +01:00
import { ChatOptionsPopoverPage } from 'src/app/shared/popover/chat-options-popover/chat-options-popover.page';
2020-12-28 10:11:00 +01:00
import { ChatPopoverPage } from 'src/app/shared/popover/chat-popover/chat-popover.page';
2020-12-29 12:40:19 +01:00
import { ContactsPage } from '../new-group/contacts/contacts.page';
import { NewGroupPage } from '../new-group/new-group.page';
2021-01-21 16:27:04 +01:00
import { GroupContactsPage } from './group-contacts/group-contacts.page';
2020-12-28 10:11:00 +01:00
@Component({
selector: 'app-group-messages',
templateUrl: './group-messages.page.html',
styleUrls: ['./group-messages.page.scss'],
})
2021-03-12 11:56:54 +01:00
export class GroupMessagesPage implements OnInit {
2021-01-27 16:01:49 +01:00
showLoader: boolean;
2021-01-21 16:27:04 +01:00
isGroupCreated:boolean;
2021-01-19 16:10:40 +01:00
loggedUser: any;
2020-12-28 10:11:00 +01:00
message:any;
2021-04-20 17:18:57 +01:00
leaveStatus:any;
2021-01-19 16:10:40 +01:00
messages:any;
2021-01-21 16:27:04 +01:00
2021-01-19 16:10:40 +01:00
room:any;
2021-01-26 15:18:03 +01:00
roomName:any;
2021-01-19 16:10:40 +01:00
members:any;
2021-03-12 11:56:54 +01:00
contacts: string[] = [" Ana M.", "Andre F.", "Bruno G.", "Catarina T", "Tiago"];
2020-12-28 10:11:00 +01:00
roomId: string;
2020-12-28 10:11:00 +01:00
constructor(
private menu: MenuController,
2020-12-29 12:40:19 +01:00
private modalController: ModalController,
2020-12-28 10:11:00 +01:00
private actionSheetController: ActionSheetController,
public popoverController: PopoverController,
2021-01-19 16:10:40 +01:00
private chatService: ChatService,
2021-03-12 11:56:54 +01:00
private navParams: NavParams,
2021-01-19 16:10:40 +01:00
private authService: AuthService,
2021-04-13 14:14:55 +01:00
private alertService: AlertService,
2021-01-21 16:27:04 +01:00
) {
this.isGroupCreated = true;
this.roomId = this.navParams.get('roomId');
2021-01-19 16:10:40 +01:00
}
2020-12-28 10:11:00 +01:00
ngOnInit() {
2021-01-19 16:10:40 +01:00
this.authService.userData$.subscribe((res:any)=>{
this.loggedUser=res;
console.log(this.loggedUser);
});
this.getRoomInfo();
}
2021-04-13 14:14:55 +01:00
notImplemented(){
this.alertService.presentAlert('Funcionalidade em desenvolvimento');
}
getRoomInfo(){
this.showLoader = true;
this.chatService.getRoomInfo(this.roomId).subscribe(room=>{
this.room = room['room'];
this.roomName = this.room.name.split('-').join(' ');
this.getGroupContacts(this.room);
this.loadGroupMessages(this.room);
this.showLoader = false;
});
2021-03-12 11:56:54 +01:00
}
/* load(){
2021-03-12 11:56:54 +01:00
this.getGroupContacts();
this.loadGroupMessages();
} */
2021-01-26 15:18:03 +01:00
2021-01-22 15:28:52 +01:00
close(){
this.modalController.dismiss();
}
2021-01-26 15:18:03 +01:00
2021-01-27 16:01:49 +01:00
doRefresh(ev:any){
this.getRoomInfo();
2021-01-27 16:01:49 +01:00
ev.target.complete();
2021-01-26 15:18:03 +01:00
}
getGroupContacts(room:any){
2021-01-27 16:01:49 +01:00
this.showLoader = true;
2021-01-19 16:10:40 +01:00
//If group is private call getGroupMembers
2021-03-12 11:56:54 +01:00
if(this.room.t === 'p'){
this.chatService.getGroupMembers(this.roomId).subscribe(res=>{
2021-01-19 16:10:40 +01:00
console.log(res);
this.members = res['members'];
2021-01-27 16:01:49 +01:00
this.showLoader = false;
2021-01-19 16:10:40 +01:00
});
}
//Otherwise call getChannelMembers for públic groups
else{
this.chatService.getChannelMembers(this.roomId).subscribe(res=>{
2021-01-19 16:10:40 +01:00
console.log(res);
this.members = res['members'];
2021-01-27 16:01:49 +01:00
this.showLoader = false;
2021-01-19 16:10:40 +01:00
});
}
}
loadGroupMessages(room:any){
2021-01-27 16:01:49 +01:00
this.showLoader = true;
2021-01-19 16:10:40 +01:00
//If group is private call getGroupMembers
2021-03-12 11:56:54 +01:00
if(this.room.t === 'p'){
this.chatService.getPrivateGroupMessages(this.roomId).subscribe(res=>{
2021-01-19 16:10:40 +01:00
console.log(res);
2021-01-22 15:28:52 +01:00
let msgOnly = res['messages'].filter(data => data.t != 'au');
this.messages = msgOnly.reverse();
2021-01-27 16:01:49 +01:00
this.showLoader = false;
2021-01-19 16:10:40 +01:00
});
}
//Otherwise call getChannelMembers for públic groups
else{
this.chatService.getPublicGroupMessages(this.roomId).subscribe(res=>{
2021-01-19 16:10:40 +01:00
console.log(res);
this.messages = res['messages'].reverse();
});
}
}
sendMessage(){
let body = {
"message":
{
"rid": this.roomId, "msg": this.message
2021-01-19 16:10:40 +01:00
}
}
this.chatService.sendMessage(body).subscribe(res=> {
this.getRoomInfo();
2021-01-19 16:10:40 +01:00
});
this.message = "";
2020-12-28 10:11:00 +01:00
}
2021-04-15 23:57:14 +01:00
async openOptions() {
console.log('OK');
const modal = await this.modalController.create({
2020-12-28 10:11:00 +01:00
component: ChatPopoverPage,
cssClass: 'chat-popover',
2021-01-20 13:17:54 +01:00
componentProps: {
2021-04-15 23:57:14 +01:00
roomId: this.roomId,
2021-01-20 13:17:54 +01:00
},
2020-12-28 10:11:00 +01:00
});
2021-04-15 23:57:14 +01:00
await modal.present();
modal.onDidDismiss().then(res=>{
2021-04-20 17:18:57 +01:00
if(res.data == 'leave'){
this.leaveStatus = this.loggedUser.me.name + ' saiu do grupo';
}
2021-04-15 23:57:14 +01:00
console.log('OK2');
2021-04-20 17:18:57 +01:00
console.log(res.data);
2021-04-15 23:57:14 +01:00
/* if(res.data ==){
2021-01-27 16:01:49 +01:00
this.roomName = res.data.name.split('-').join(' ');
console.log(this.roomName);
this.getRoomInfo();
2021-04-15 23:57:14 +01:00
//this.modalController.dismiss();
}; */
2021-01-20 16:58:04 +01:00
});
2020-12-28 10:11:00 +01:00
}
2020-12-29 12:40:19 +01:00
async openChatOptions(ev: any) {
const popover = await this.popoverController.create({
component: ChatOptionsPopoverPage,
cssClass: 'chat-options-popover',
event: ev,
2021-01-20 10:23:59 +01:00
componentProps: {
2021-01-20 13:17:54 +01:00
room: this.room,
2021-01-20 10:23:59 +01:00
},
2020-12-29 12:40:19 +01:00
translucent: true
});
return await popover.present();
}
async addContacts(){
console.log(this.members);
2021-01-26 15:18:03 +01:00
2020-12-29 12:40:19 +01:00
const modal = await this.modalController.create({
2021-01-21 16:27:04 +01:00
component: GroupContactsPage,
componentProps: {
isCreated: this.isGroupCreated,
room: this.room,
members: this.members,
name: this.room.name,
2021-01-21 16:27:04 +01:00
},
2020-12-29 12:40:19 +01:00
cssClass: 'contacts',
backdropDismiss: false
});
await modal.present();
2021-01-26 15:18:03 +01:00
modal.onDidDismiss().then(()=>{
this.getRoomInfo();
2021-01-26 15:18:03 +01:00
});
2020-12-29 12:40:19 +01:00
}
2021-03-12 11:56:54 +01:00
2021-01-27 11:23:57 +01:00
/* async actionSheet() {
const actionSheet = await this.actionSheetController.create({
cssClass: 'my-custom-class',
buttons: [{
text: 'Sair do grupo',
handler: () => {
console.log('Delete clicked');
}
}, {
text: 'Alterar nome do grupo1',
handler: () => {
console.log('Alterar nome do grupo');
this.openChangeGroupName()
}
}, {
text: 'Apagar o grupo',
handler: () => {
console.log('Play clicked');
}
},
]
});
await actionSheet.present();
}
*/
2020-12-28 10:11:00 +01:00
2021-03-12 13:10:20 +01:00
}