mirror of
https://code.equilibrium.co.ao/ITO/doneit-web.git
synced 2026-04-18 12:37:53 +00:00
197 lines
5.5 KiB
TypeScript
197 lines
5.5 KiB
TypeScript
import { Component, EventEmitter, Input, OnInit, Output } from '@angular/core';
|
|
import { ModalController } from '@ionic/angular';
|
|
import { GroupMessagesPage } from '../group-messages/group-messages.page';
|
|
import { ThemeService } from 'src/app/services/theme.service'
|
|
import { UserContacts } from 'src/app/services/Repositorys/contacts/data-source/contacts-data-source.service';
|
|
import { ContactRepositoryService } from 'src/app/services/Repositorys/contacts/repository/contacts-repository.service';
|
|
import { ChatServiceService } from 'src/app/module/chat/domain/chat-service.service';
|
|
import { HttpErrorHandle } from 'src/app/services/http-error-handle.service';
|
|
import { ToastService } from 'src/app/services/toast.service';
|
|
import { SessionStore } from 'src/app/store/session.service';
|
|
import { MessageEntity } from 'src/app/core/chat/entity/message';
|
|
// import { ChatSystemService } from 'src/app/services/chat/chat-system.service'
|
|
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 {
|
|
options:any;
|
|
|
|
loading = false
|
|
userList: UserContacts[] = []
|
|
currentMembers:UserContacts[];
|
|
allChatUsers: UserContacts[] = [];
|
|
userContainer: {[key: string]: ( UserContacts & {isChecked: boolean})[] } = {}
|
|
|
|
@Output() openMessage: EventEmitter<any> = new EventEmitter<any>();
|
|
@Output() emptyTextDescriptionOpen: EventEmitter<any> = new EventEmitter<any>();
|
|
@Output() backToChat: EventEmitter<any> = new EventEmitter<any>();
|
|
@Output() closeAllDesktopComponents: EventEmitter<any> = new EventEmitter<any>();
|
|
@Output() openMessagesToStartDirectConversation = new EventEmitter<RoomViewModel>();
|
|
|
|
@Input() roomId: string;
|
|
|
|
constructor(
|
|
private modalController: ModalController,
|
|
public ThemeService: ThemeService,
|
|
private contactsRepositoryService: ContactRepositoryService,
|
|
private httpErrorHandle: HttpErrorHandle,
|
|
private toastService: ToastService,
|
|
private chatServiceService: ChatServiceService,
|
|
)
|
|
{}
|
|
|
|
ngOnInit() {
|
|
this.loadUsers();
|
|
|
|
}
|
|
|
|
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;
|
|
}
|
|
|
|
separateLetter(record, recordIndex, records){
|
|
|
|
if(recordIndex == 0){
|
|
return record.first[0];
|
|
}
|
|
|
|
let first_prev = records[recordIndex - 1].first[0];
|
|
let first_current = record.first[0];
|
|
|
|
if(first_prev != first_current){
|
|
return first_current;
|
|
}
|
|
return null;
|
|
}
|
|
|
|
doRefresh(event){
|
|
|
|
}
|
|
close(roomId?: string) {
|
|
if (roomId) {
|
|
this.backToChat.emit( roomId );
|
|
} else {
|
|
this.closeAllDesktopComponents.emit();
|
|
}
|
|
}
|
|
|
|
openMessageComponent(room: RoomViewModel) {
|
|
this.openMessagesToStartDirectConversation.emit(room)
|
|
}
|
|
|
|
|
|
onChange(event) {
|
|
const textSearch = event.detail.value;
|
|
|
|
const filteredUserList: (UserContacts & {isChecked: boolean})[] = this.FilterUserListedByTextSearch(textSearch) as any
|
|
|
|
const userContainer = {}
|
|
for(const user of filteredUserList) {
|
|
const firstLetter = user.wxFullName.charAt(0)
|
|
if(!userContainer[firstLetter]) {
|
|
userContainer[firstLetter] = [user]
|
|
} else {
|
|
userContainer[firstLetter].push(user)
|
|
}
|
|
}
|
|
this.userContainer = userContainer
|
|
}
|
|
|
|
|
|
FilterUserListedByTextSearch(textSearch: string) {
|
|
return this.allChatUsers.filter( e => e.wxFullName.toLowerCase().includes(textSearch.toLowerCase())).sort((a,b) => {
|
|
if(a.wxFullName < b.wxFullName) {
|
|
return -1;
|
|
}
|
|
if(a.wxFullName > b.wxFullName) {
|
|
return 1;
|
|
}
|
|
return 0;
|
|
});
|
|
}
|
|
|
|
clicked() {}
|
|
|
|
|
|
selectOnce = true
|
|
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.openMessageComponent(new RoomViewModel(room.value))
|
|
}
|
|
|
|
} else {
|
|
const room = await this.chatServiceService.roomGetLocalById({
|
|
$roomId: user.wxUserId.toString()
|
|
})
|
|
|
|
if(room.isOk()) {
|
|
this.openMessageComponent(new RoomViewModel(room.value))
|
|
}
|
|
}
|
|
|
|
}
|
|
|
|
}
|