Files
doneit-web/src/app/ui/chat/modal/group-messages/group-contacts/group-contacts.page.ts
T

290 lines
8.1 KiB
TypeScript
Raw Normal View History

2024-09-19 09:16:14 +01:00
import { HttpClient, HttpErrorResponse, HttpHeaders, HttpRequest, HttpResponse } from '@angular/common/http';
import { Component, OnInit } from '@angular/core';
2021-01-21 16:27:04 +01:00
import { ModalController, NavParams } from '@ionic/angular';
import * as _ from 'lodash';
2021-10-25 14:00:58 +01:00
import { ThemeService } from 'src/app/services/theme.service'
2022-04-18 15:12:27 +01:00
import { SessionStore } from 'src/app/store/session.service';
2024-09-19 09:16:14 +01:00
import { HttpErrorHandle } from 'src/app/services/http-error-handle.service';
import { ContactRepositoryService } from 'src/app/services/Repositorys/contacts/repository/contacts-repository.service';
import { MemberListLocalRepository } from 'src/app/module/chat/data/repository/member/member-list-local-repository.service'
import { UserContacts } from 'src/app/services/Repositorys/contacts/data-source/contacts-data-source.service';
import { ToastService } from 'src/app/services/toast.service';
import { ChatServiceService } from 'src/app/module/chat/domain/chat-service.service';
2024-09-19 23:36:59 +01:00
import { z, ZodError } from 'zod';
const GroupContactsPageOutPutSchema = z.object({
roomId: z.string().optional()
})
export type IGroupContactsPageOutPutSchema = z.infer<typeof GroupContactsPageOutPutSchema>
2024-09-19 09:16:14 +01:00
@Component({
selector: 'app-group-contacts',
templateUrl: './group-contacts.page.html',
styleUrls: ['./group-contacts.page.scss'],
})
export class GroupContactsPage implements OnInit {
showLoader: boolean;
users = [];
contact: string[] = [" Ana M.", "Andre F.", "Bruno G.", "Catarina T", "Tiago"];
headers: HttpHeaders;
options:any;
2021-01-22 15:28:52 +01:00
listContacts: any[];
contacts: any;
textSearch:string;
room:any;
members:any;
dm:any;
2021-01-21 16:27:04 +01:00
isGroupCreated:boolean;
groupName:string;
2021-01-22 15:28:52 +01:00
selectedUserList:any;
2022-04-18 15:12:27 +01:00
sessionStore = SessionStore
2023-08-20 00:26:24 +01:00
objectUserSingleStone = []
userContainer = {}
2024-09-19 09:16:14 +01:00
roomId:string;
allChatUsers: UserContacts[] = [];
currentMembers:UserContacts[];
selectedUsers: number[] =[]
get hasMemberToUpload() {
return this.selectedUsers.length >= 1
}
2023-09-19 10:21:23 +01:00
constructor(
private modalController: ModalController,
2022-04-24 20:05:04 +01:00
public ThemeService: ThemeService,
2024-09-19 09:16:14 +01:00
public chatServiceService: ChatServiceService,
private navParams: NavParams,
private httpErrorHandle: HttpErrorHandle,
private contactsRepositoryService: ContactRepositoryService,
private MemberListLocalRepository: MemberListLocalRepository,
private toastService: ToastService,
2021-07-23 14:43:51 +01:00
)
{
2021-07-23 14:43:51 +01:00
2024-06-11 11:46:04 +01:00
// this.textSearch="";
// this.dm=null;
// this.room=null;
// this.isGroupCreated = this.navParams.get('isCreated');
// this.groupName = this.navParams.get('name');
2024-09-19 09:16:14 +01:00
this.room = this.navParams.get('roomId');
2024-06-11 11:46:04 +01:00
// this.members = this.navParams.get('members');
}
ngOnInit() {
2022-10-11 13:56:56 +01:00
// this.chatService.refreshtoken();
this.loadUsers();
2023-08-20 22:00:23 +01:00
// this.getMembers();
2023-09-19 10:21:23 +01:00
}
2024-09-19 09:16:14 +01:00
async loadUsers() {
2023-08-20 00:26:24 +01:00
2024-09-19 09:16:14 +01:00
const getallChatUsers = await this.contactsRepositoryService.getUsers()
const getRoomById = await this.chatServiceService.getRoomById(this.roomId)
2023-08-20 00:26:24 +01:00
2024-09-19 09:16:14 +01:00
console.log({getallChatUsers, getRoomById})
2023-08-20 00:26:24 +01:00
2024-09-19 09:16:14 +01:00
if(getallChatUsers.isOk() && getRoomById.isOk()) {
2023-08-20 00:26:24 +01:00
2024-09-19 09:16:14 +01:00
this.allChatUsers = getallChatUsers.value.data.result.sort((a,b) => {
if(a.wxFullName < b.wxFullName) {
return -1;
}
if(a.wxFullName > b.wxFullName) {
return 1;
}
return 0;
});
2023-08-20 00:26:24 +01:00
2024-09-19 09:16:14 +01:00
const currentMemberToMap = await this.MemberListLocalRepository.getRoomMemberById(this.roomId)
2023-08-20 00:26:24 +01:00
2024-09-19 09:16:14 +01:00
console.log({currentMemberToMap})
2023-09-19 10:21:23 +01:00
2024-09-19 09:16:14 +01:00
this.currentMembers = currentMemberToMap.map((e)=> ({
userPhoto: e.userPhoto,
wxeMail: e.wxeMail,
wxFullName: e.wxFullName,
wxUserId: e.wxUserId
}))
2023-09-19 10:21:23 +01:00
2024-09-19 09:16:14 +01:00
const currentMemberIds = this.currentMembers.map(e => e.wxUserId)
2023-08-20 00:26:24 +01:00
2024-09-19 09:16:14 +01:00
const allSelectableUsers = this.allChatUsers.filter(e => !currentMemberIds.includes(e.wxUserId))
2023-09-19 10:21:23 +01:00
2024-09-19 09:16:14 +01:00
for(const user of allSelectableUsers) {
const firstLetter = user.wxFullName.charAt(0)
2023-08-21 11:42:34 +01:00
2024-09-19 09:16:14 +01:00
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)
}
}
}
2023-09-19 10:21:23 +01:00
2021-07-23 14:43:51 +01:00
2024-09-19 09:16:14 +01:00
}
else if (getRoomById.isErr() && getRoomById.error instanceof HttpResponse) {
this.httpErrorHandle.httpStatusHandle(getRoomById.error)
} else if (getallChatUsers.isErr() && getallChatUsers.error instanceof HttpResponse) {
this.httpErrorHandle.httpStatusHandle(getallChatUsers.error)
} else if (getRoomById.isErr() ) {
console.log(getRoomById.error)
} 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.showLoader = false;
}
async updateGroup() {
if(this.hasMemberToUpload) {
this.showLoader = true;
const addMembers = await this.chatServiceService.addMemberToRoom({
id: this.roomId,
members: this.selectedUsers
})
if(addMembers.isOk()) {
// this.addContacts(this.roomId);
//this.openGroupMessage.emit(this.roomId);
await this.chatServiceService.getRoomById(this.roomId);
this.modalController.dismiss({roomId: this.roomId})
} else if(addMembers.error instanceof HttpRequest) {
this.httpErrorHandle.httpStatusHandle(addMembers.error)
}
} else {
// this.openGroupMessage.emit(this.roomId);
console.log('nothoing to add')
}
this.showLoader = false;
}
2021-10-28 15:40:41 +01:00
getMembers(){
2024-08-17 22:05:57 +01:00
2021-10-28 15:40:41 +01:00
}
separateLetter(record, recordIndex, records){
if(recordIndex == 0){
return record.name[0];
}
let first_prev = records[recordIndex - 1].name[0];
let first_current = record.name[0];
if(first_prev != first_current){
return first_current;
}
return null;
}
2021-10-28 15:40:41 +01:00
2021-04-20 14:30:06 +01:00
doRefresh(ev){
2021-04-20 13:22:25 +01:00
this.loadUsers();
2023-08-21 11:42:34 +01:00
//this.getMembers();
2021-04-20 14:30:06 +01:00
ev.target.complete();
}
2021-01-21 16:27:04 +01:00
async close(){
this.modalController.dismiss();
}
2021-10-28 15:40:41 +01:00
2024-09-19 09:16:14 +01:00
FilterUserListedByTextSearch() {
return this.allChatUsers.filter( e => e.wxFullName.toLowerCase().includes(this.textSearch.toLowerCase())).sort((a,b) => {
if(a.wxFullName < b.wxFullName) {
2023-08-11 16:46:06 +01:00
return -1;
}
2024-09-19 09:16:14 +01:00
if(a.wxFullName > b.wxFullName) {
2023-08-11 16:46:06 +01:00
return 1;
}
return 0;
});
2024-09-19 09:16:14 +01:00
}
2023-08-15 12:03:37 +01:00
2024-09-19 09:16:14 +01:00
onChangeCheckBox(user: UserContacts & {isChecked: boolean}) {
console.log(user)
2023-08-15 12:03:37 +01:00
2024-09-19 09:16:14 +01:00
if(user.isChecked) {
2023-08-20 00:26:24 +01:00
2024-09-19 09:16:14 +01:00
this.selectedUsers.push(user.wxUserId)
} else {
this.selectedUsers = this.selectedUsers.filter(e => e!= user.wxUserId)
}
}
2023-08-20 00:26:24 +01:00
2024-09-19 09:16:14 +01:00
onChange(event) {
this.textSearch = event.detail.value;
2023-08-20 00:26:24 +01:00
2024-09-19 09:16:14 +01:00
const filteredUserList: (UserContacts & {isChecked: boolean})[] = this.FilterUserListedByTextSearch() as any
2023-09-19 10:21:23 +01:00
2024-09-19 09:16:14 +01:00
const userContainer = {}
for(const user of filteredUserList) {
const firstLetter = user.wxFullName.charAt(0)
2023-08-20 00:26:24 +01:00
2024-09-19 09:16:14 +01:00
if(!userContainer[firstLetter]) {
user.isChecked = this.selectedUsers.includes(user.wxUserId)
userContainer[firstLetter] = [user]
2023-08-20 00:26:24 +01:00
} else {
2024-09-19 09:16:14 +01:00
user.isChecked = this.selectedUsers.includes(user.wxUserId)
userContainer[firstLetter].push(user)
2023-08-20 00:26:24 +01:00
}
2023-09-19 10:21:23 +01:00
2023-08-20 00:26:24 +01:00
}
2024-09-19 09:16:14 +01:00
this.userContainer = userContainer
}
2023-09-19 10:21:23 +01:00
2021-07-23 14:43:51 +01:00
2024-09-19 09:16:14 +01:00
async deleteMember(user: UserContacts) {
this.showLoader = true;
2021-07-23 14:43:51 +01:00
2024-09-19 09:16:14 +01:00
const result = await this.chatServiceService.removeMemberToRoom({
id: this.roomId,
members: [ user.wxUserId]
})
2021-01-22 15:28:52 +01:00
2024-09-19 09:16:14 +01:00
if(result.isOk()) {
this.currentMembers = this.currentMembers.filter( e => e.wxUserId != user.wxUserId)
2023-09-19 10:21:23 +01:00
2024-09-19 09:16:14 +01:00
const firstLetter = user.wxFullName.charAt(0)
2021-07-23 14:43:51 +01:00
2024-09-19 09:16:14 +01:00
if(!this.userContainer[firstLetter]) {
user['isChecked'] = false
this.userContainer[firstLetter] = [user as any]
} else {
user['isChecked'] = false
this.userContainer[firstLetter].push(user as any)
2021-01-22 15:28:52 +01:00
}
2023-09-19 10:21:23 +01:00
2024-09-19 09:16:14 +01:00
} else if(result.error instanceof HttpRequest) {
this.httpErrorHandle.httpStatusHandle(result.error)
} else if(result.error instanceof ZodError) {
console.log(result.error.issues)
2023-08-14 21:26:23 +01:00
}
2023-09-19 10:21:23 +01:00
2024-09-19 09:16:14 +01:00
this.showLoader = false;
2024-08-17 22:05:57 +01:00
2021-01-21 16:27:04 +01:00
}
2023-09-19 10:21:23 +01:00
}