Files
doneit-web/src/app/shared/popover/chat-popover/chat-popover.page.ts
T

141 lines
3.7 KiB
TypeScript
Raw Normal View History

2020-12-28 10:11:00 +01:00
import { Component, OnInit } from '@angular/core';
2021-01-27 11:23:57 +01:00
import { ModalController, NavParams, PopoverController } from '@ionic/angular';
2021-01-20 10:23:59 +01:00
import { ChatService } from 'src/app/services/chat.service';
2021-09-08 15:02:14 +01:00
import { ToastService } from 'src/app/services/toast.service';
2021-10-25 15:31:43 +01:00
import { ThemeService } from 'src/app/services/theme.service'
2022-01-24 19:09:26 +01:00
import { SetRoomOwnerPage } from 'src/app/modals/set-room-owner/set-room-owner.page';
2022-09-30 15:13:36 +01:00
import { ChatSystemService } from 'src/app/services/chat/chat-system.service';
2022-06-08 11:56:56 +01:00
import { HttpErrorResponse } from '@angular/common/http';
import { SessionStore } from 'src/app/store/session.service';
2023-03-09 09:09:10 +01:00
import { GroupContactsPage } from '../../chat/group-messages/group-contacts/group-contacts.page';
2021-10-25 15:31:43 +01:00
2020-12-28 10:11:00 +01:00
@Component({
selector: 'app-chat-popover',
templateUrl: './chat-popover.page.html',
styleUrls: ['./chat-popover.page.scss'],
})
export class ChatPopoverPage implements OnInit {
2021-04-13 14:45:56 +01:00
roomId:string;
room: any;
2022-01-24 19:09:26 +01:00
members:any;
isAdmin = false;
2023-03-09 09:09:10 +01:00
isGroupCreated: boolean;
2020-12-28 10:11:00 +01:00
constructor(
private popoverController: PopoverController,
2021-01-27 11:23:57 +01:00
private modalController: ModalController,
2021-01-20 10:23:59 +01:00
private navParams: NavParams,
private chatService: ChatService,
2021-09-08 15:02:14 +01:00
private toastService: ToastService,
2022-01-26 09:19:54 +01:00
public ThemeService: ThemeService,
2022-09-30 15:13:36 +01:00
public ChatSystemService: ChatSystemService,
2021-08-17 14:17:19 +01:00
) {
2021-04-13 14:45:56 +01:00
this.roomId = this.navParams.get('roomId');
2022-01-24 19:09:26 +01:00
this.members = this.navParams.get('members');
this.isAdmin = this.navParams.get('isAdmin');
2021-01-20 10:23:59 +01:00
}
2020-12-28 10:11:00 +01:00
ngOnInit() {
2022-10-11 13:56:56 +01:00
// this.chatService.refreshtoken();
2020-12-28 10:11:00 +01:00
}
2021-04-21 15:14:43 +01:00
2021-09-08 15:02:14 +01:00
close(action:any){
2022-02-11 17:15:01 +01:00
if( window.innerWidth < 701){
2021-09-08 15:02:14 +01:00
this.popoverController.dismiss(action);
2021-03-18 09:25:59 +01:00
}
else{
2023-03-09 16:35:54 +01:00
this.modalController.dismiss(action)
2021-03-18 09:25:59 +01:00
}
}
2020-12-28 10:11:00 +01:00
2021-01-20 10:23:59 +01:00
//Top menu options
2021-01-20 16:58:04 +01:00
//Close
2022-01-24 19:09:26 +01:00
async setRoomOwner(){
let classs;
2022-02-11 17:15:01 +01:00
if (window.innerWidth < 701) {
2022-01-24 19:09:26 +01:00
classs = 'modal modal-desktop'
} else {
classs = 'centered-rounded-modal'
}
const modal = await this.modalController.create({
component: SetRoomOwnerPage,
cssClass: classs,
backdropDismiss: true,
componentProps: {
2022-01-26 09:19:54 +01:00
roomId: this.roomId,
2022-01-24 19:09:26 +01:00
members: this.members,
isAdmin: this.isAdmin
2022-01-24 19:09:26 +01:00
}
});
await modal.present();
modal.onDidDismiss().then((res)=>{
if(res.data == 'success'){
this.leaveGroup();
2022-09-30 15:13:36 +01:00
//this.ChatSystemService.hidingRoom(this.roomId);
}
2022-01-24 19:09:26 +01:00
});
}
2022-01-26 09:19:54 +01:00
async leaveGroup(){
2021-08-17 14:17:19 +01:00
2021-04-13 14:45:56 +01:00
let body = { "roomId":this.roomId, }
2022-12-20 17:06:19 +01:00
let res:any;
try {
res = await this.ChatSystemService.leaveRoom(this.roomId);
} catch (error) {
console.error(error)
}
2022-01-26 09:19:54 +01:00
if(res.error){
if(res.error.error = "error-you-are-last-owner"){
this.toastService._badRequest("Você é o último administrador do grupo. Por favor, defina o novo administrador antes de sair da grupo.");
this.setRoomOwner();
}
else if(res.error.error == 'error-user-not-in-room'){
this.toastService._badRequest("Você já não esta nesta conversa");
}
else{
this.toastService._badRequest("Não foi possível sair do grupo");
}
}
2022-10-12 11:38:04 +01:00
else {
this.ChatSystemService.deleteRoom(this.roomId)
2022-01-26 09:19:54 +01:00
this.close('leave');
}
2021-01-20 16:58:04 +01:00
}
2021-04-21 15:14:43 +01:00
2021-01-20 16:58:04 +01:00
//Delete
2023-08-08 09:43:26 +01:00
deleteGroup() {
let body = { "roomId":this.roomId }
this.chatService.getRoomInfo(this.roomId).subscribe(room => {
2021-04-13 14:45:56 +01:00
this.room = room['room'];
2022-10-12 11:38:04 +01:00
if(this.room.t === 'p') {
2021-04-13 14:45:56 +01:00
this.chatService.deleteGroup(body).subscribe(res=>{
2023-08-11 16:46:06 +01:00
this.ChatSystemService.deleteRoom(this.roomId)
2021-04-13 14:45:56 +01:00
});
}
2022-10-12 11:38:04 +01:00
else {
2021-04-13 14:45:56 +01:00
this.chatService.deleteChannel(body).subscribe(res=>{
2023-08-11 16:46:06 +01:00
this.ChatSystemService.deleteRoom(this.roomId)
2021-04-13 14:45:56 +01:00
});
}
});
2023-03-10 11:41:44 +01:00
this.close('delete');
2021-01-20 10:23:59 +01:00
}
2021-04-14 14:10:17 +01:00
2021-01-27 11:23:57 +01:00
async openChangeGroupName(){
2023-03-10 11:41:44 +01:00
this.close('edit');
2021-01-27 11:23:57 +01:00
}
2021-01-20 10:23:59 +01:00
2023-03-09 16:35:54 +01:00
async addUser() {
2023-03-10 11:41:44 +01:00
this.close('addUser');
2023-03-09 16:35:54 +01:00
}
2020-12-28 10:11:00 +01:00
}