Files
doneit-web/src/app/ui/chat/modal/messages/contacts/contacts.page.ts
T
2024-10-07 13:27:49 +01:00

200 lines
5.2 KiB
TypeScript

import { Component, OnInit } from '@angular/core';
import { ModalController } from '@ionic/angular';
import { ThemeService } from 'src/app/services/theme.service'
import { SessionStore } from 'src/app/store/session.service';
import { ContactRepositoryService } from 'src/app/services/Repositorys/contacts/repository/contacts-repository.service';
import { UserContacts } from 'src/app/services/Repositorys/contacts/data-source/contacts-data-source.service';
import { HttpErrorHandle } from 'src/app/services/http-error-handle.service';
import { ToastService } from 'src/app/services/toast.service';
import { ChatServiceService } from 'src/app/module/chat/domain/chat-service.service'
import { MessageEntity } from 'src/app/core/chat/entity/message';
import { RoomType } from "src/app/core/chat/entity/group";
import { RoomViewModel } from '../../../store/model/room';
@Component({
selector: 'app-contacts',
templateUrl: './contacts.page.html',
styleUrls: ['./contacts.page.scss'],
})
export class ContactsPage implements OnInit {
loading: boolean;
loggedUser: any;
users = [];
contacts:any;
textSearch:string;
room:any;
dm:any;
sessionStore = SessionStore
userList: UserContacts[] = []
currentMembers:UserContacts[];
allChatUsers: UserContacts[] = [];
userContainer: {[key: string]: ( UserContacts & {isChecked: boolean})[] } = {}
selectedUsers: number[] =[]
SessionStore = SessionStore
constructor(
private modalController: ModalController,
public ThemeService: ThemeService,
private contactsRepositoryService: ContactRepositoryService,
private httpErrorHandle: HttpErrorHandle,
private toastService: ToastService,
private chatServiceService: ChatServiceService
)
{
this.loggedUser = SessionStore.user.ChatData['data'];
this.textSearch="";
this.dm=null;
this.room=null;
}
ngOnInit(): void {
this.loadUsers()
}
get hasMemberToUpload() {
return this.selectedUsers.length >= 1
}
async updateGroup() {
}
openGroupMessagesPage() {
// this.openGroupMessage.emit(this.roomId)
}
async loadUsers() {
this.loading = true
const getallChatUsers = await this.chatServiceService.getContactList()
if(getallChatUsers.isOk()) {
this.allChatUsers = getallChatUsers.value.sort((a,b) => {
if(a.wxFullName < b.wxFullName) {
return -1;
}
if(a.wxFullName > b.wxFullName) {
return 1;
}
return 0;
});
for(const user of this.allChatUsers) {
const firstLetter = user.wxFullName.charAt(0)
if(!this.userContainer[firstLetter]) {
user['isChecked'] = false
this.userContainer[firstLetter] = [user as any]
} else {
const userIds = this.userContainer[firstLetter].map( e => e.wxUserId)
if(!userIds.includes(user.wxUserId)) {
user['isChecked'] = false
this.userContainer[firstLetter].push(user as any)
}
}
}
}
else if (getallChatUsers.isErr() ) {
console.log(getallChatUsers.error)
} else {
this.toastService._badRequest("Pedimos desculpa mas não foi possível executar a acção. Por favor, contacte o apoio técnico.")
}
this.loading = false;
}
doRefresh(ev) {
ev.target.complete();
}
FilterUserListedByTextSearch() {
return this.allChatUsers.filter( e => e.wxFullName.toLowerCase().includes(this.textSearch.toLowerCase())).sort((a,b) => {
if(a.wxFullName < b.wxFullName) {
return -1;
}
if(a.wxFullName > b.wxFullName) {
return 1;
}
return 0;
});
}
onChangeCheckBox(user: UserContacts & {isChecked: boolean}) {
if(user.isChecked) {
this.selectedUsers.push(user.wxUserId)
} else {
this.selectedUsers = this.selectedUsers.filter(e => e!= user.wxUserId)
}
}
onChange(event) {
this.textSearch = event.detail.value;
const filteredUserList: (UserContacts & {isChecked: boolean})[] = this.FilterUserListedByTextSearch() as any
const userContainer = {}
for(const user of filteredUserList) {
const firstLetter = user.wxFullName.charAt(0)
if(!userContainer[firstLetter]) {
user.isChecked = this.selectedUsers.includes(user.wxUserId)
userContainer[firstLetter] = [user]
} else {
user.isChecked = this.selectedUsers.includes(user.wxUserId)
userContainer[firstLetter].push(user)
}
}
this.userContainer = userContainer
}
close() {
this.modalController.dismiss(null);
}
async select(user: UserContacts) {
// const result = await this.chatServiceService.sendMessage(message, RoomType.Direct)
const result = await this.chatServiceService.roomCreateLocalDirectMessage({
roomName: user.wxFullName,
receiverId: user.wxUserId,
});
if(result.isOk()) {
const room = await this.chatServiceService.roomGetLocalById({
$roomId: result.value
})
if(room.isOk()) {
this.modalController.dismiss(new RoomViewModel(room.value))
}
} else {
const room = await this.chatServiceService.roomGetLocalById({
$roomId: user.wxUserId.toString()
})
if(room.isOk()) {
this.modalController.dismiss(new RoomViewModel(room.value))
}
}
}
}