Skip to content

Commit

Permalink
refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
asliayk committed Dec 9, 2024
1 parent 43d71ed commit a75f02c
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -169,11 +169,15 @@ export class ConversationInfoComponent implements OnInit, OnDestroy {
}

private updateGroupChat<K extends keyof GroupChatDTO>(groupChat: GroupChatDTO, propertyName: K, updateValue: GroupChatDTO[K]) {
const courseId = this.course()?.id;
if (!courseId) {
return;
}
const updateDTO = new GroupChatDTO();
updateDTO[propertyName] = updateValue;

this.groupChatService
.update(this.course()?.id!, groupChat.id!, updateDTO)
.update(courseId, groupChat.id!, updateDTO)
.pipe(
map((res: HttpResponse<GroupChatDTO>) => res.body),
takeUntil(this.ngUnsubscribe),
Expand All @@ -188,10 +192,14 @@ export class ConversationInfoComponent implements OnInit, OnDestroy {
}

private updateChannel<K extends keyof ChannelDTO>(channel: ChannelDTO, propertyName: K, updateValue: ChannelDTO[K]) {
const courseId = this.course()?.id;
if (!courseId) {
return;
}
const updateDTO = new ChannelDTO();
updateDTO[propertyName] = updateValue;
this.channelService
.update(this.course()?.id!, channel.id!, updateDTO)
.update(courseId, channel.id!, updateDTO)
.pipe(
map((res: HttpResponse<ChannelDTO>) => res.body),
takeUntil(this.ngUnsubscribe),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,15 @@ export class ConversationMemberRowComponent implements OnInit, OnDestroy {
channelName: channel.name!,
userName: this.userLabel,
};
const confirmedCallback = () => this.channelService.grantChannelModeratorRole(this.course()?.id!, channel.id!, [this.conversationMember()?.login!]);
const confirmedCallback = () => {
const courseId = this.course?.()?.id;
const channelId = channel?.id;
const memberLogin = this.conversationMember?.()?.login;
if (!courseId || !channelId || !memberLogin) {
throw new Error('Required parameters are missing');
}
return this.channelService.grantChannelModeratorRole(courseId, channelId, [memberLogin]);
};
this.openConfirmationDialog(translationKeys, translationParams, confirmedCallback);
}

Expand All @@ -143,7 +151,15 @@ export class ConversationMemberRowComponent implements OnInit, OnDestroy {
channelName: channel.name!,
userName: this.userLabel,
};
const confirmedCallback = () => this.channelService.revokeChannelModeratorRole(this.course()?.id!, channel.id!, [this.conversationMember()?.login!]);
const confirmedCallback = () => {
const courseId = this.course?.()?.id;
const channelId = channel?.id;
const memberLogin = this.conversationMember?.()?.login;
if (!courseId || !channelId || !memberLogin) {
throw new Error('Required parameters are missing');
}
return this.channelService.revokeChannelModeratorRole(courseId, channelId, [memberLogin]);
};
this.openConfirmationDialog(translationKeys, translationParams, confirmedCallback);
}

Expand Down Expand Up @@ -174,7 +190,15 @@ export class ConversationMemberRowComponent implements OnInit, OnDestroy {
userName: this.userLabel,
channelName: channel.name!,
};
const confirmedCallback = () => this.channelService.deregisterUsersFromChannel(this.course()?.id!, this.activeConversation()?.id!, [this.conversationMember()?.login!]);
const confirmedCallback = () => {
const courseId = this.course?.()?.id;
const activeConversationId = this.activeConversation()?.id;
const memberLogin = this.conversationMember?.()?.login;
if (!courseId || !activeConversationId || !memberLogin) {
throw new Error('Required parameters are missing');
}
return this.channelService.deregisterUsersFromChannel(courseId, activeConversationId, [memberLogin]);
};
this.openConfirmationDialog(translationKeys, translationParams, confirmedCallback);
}

Expand All @@ -193,7 +217,15 @@ export class ConversationMemberRowComponent implements OnInit, OnDestroy {
const translationParams = {
userName: this.userLabel,
};
const confirmedCallback = () => this.groupChatService.removeUsersFromGroupChat(this.course()?.id!, this.activeConversation()?.id!, [this.conversationMember()?.login!]);
const confirmedCallback = () => {
const courseId = this.course?.()?.id;
const activeConversationId = this.activeConversation()?.id;
const memberLogin = this.conversationMember?.()?.login;
if (!courseId || !activeConversationId || !memberLogin) {
throw new Error('Required parameters are missing');
}
return this.groupChatService.removeUsersFromGroupChat(courseId, activeConversationId, [memberLogin]);
};
this.openConfirmationDialog(translationKeys, translationParams, confirmedCallback);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,16 +112,20 @@ export class ConversationMembersComponent implements OnInit, OnDestroy {
this.isSearching = true;
this.searchTerm = searchTerm;
}),
switchMap(() =>
this.conversationService.searchMembersOfConversation(
this.course()?.id!,
this.activeConversation()?.id!,
this.searchTerm,
this.page - 1,
this.itemsPerPage,
Number(this.selectedFilter),
),
),
switchMap(() => {
if (this.course()?.id && this.activeConversation()?.id) {
return this.conversationService.searchMembersOfConversation(
this.course()?.id!,
this.activeConversation()?.id!,
this.searchTerm,
this.page - 1,
this.itemsPerPage,
Number(this.selectedFilter),
);
} else {
return EMPTY;
}
}),
takeUntil(this.ngUnsubscribe),
)
.subscribe({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,12 @@ export class ConversationSettingsComponent implements OnInit, OnDestroy {
private alertService = inject(AlertService);

ngOnInit(): void {
this.canLeaveConversation = canLeaveConversation(this.activeConversation()!);

this.conversationAsChannel = getAsChannelDTO(this.activeConversation());
const conversation = this.activeConversation();
if (!conversation) {
return;
}
this.canLeaveConversation = canLeaveConversation(conversation);
this.conversationAsChannel = getAsChannelDTO(conversation);
this.canChangeChannelArchivalState = this.conversationAsChannel ? canChangeChannelArchivalState(this.conversationAsChannel) : false;
this.canDeleteChannel = this.conversationAsChannel ? canDeleteChannel(this.course()!, this.conversationAsChannel) : false;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,14 +45,18 @@ examples.forEach((activeConversation) => {
fixture = TestBed.createComponent(ConversationSettingsComponent);
component = fixture.componentInstance;
TestBed.runInInjectionContext(() => {
component = fixture.componentInstance;
component.course = input<Course>(course);
component.activeConversation = input<ConversationDTO>(activeConversation);
component.ngOnInit();
});
fixture.detectChanges();
});

afterEach(() => {
// Reset injection context
TestBed.resetTestingModule();
});

it('should create', () => {
expect(component).toBeTruthy();
});
Expand Down

0 comments on commit a75f02c

Please sign in to comment.