Skip to content

Commit 66a7829

Browse files
committed
refactor: use typed forms in chat share dialog
1 parent 49370f6 commit 66a7829

File tree

1 file changed

+31
-15
lines changed

1 file changed

+31
-15
lines changed

src/app/shared/dialogs/dialogs-chat-share.component.ts

Lines changed: 31 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { Component, Inject, OnInit, ViewChild } from '@angular/core';
2+
import { StepperSelectionEvent } from '@angular/cdk/stepper';
23
import { MatStepper } from '@angular/material/stepper';
3-
import { UntypedFormBuilder, UntypedFormGroup } from '@angular/forms';
4+
import { FormBuilder, FormControl, FormGroup } from '@angular/forms';
45
import {
56
MAT_LEGACY_DIALOG_DATA as MAT_DIALOG_DATA, MatLegacyDialogRef as MatDialogRef, MatLegacyDialog as MatDialog
67
} from '@angular/material/legacy-dialog';
@@ -22,15 +23,25 @@ import { DialogsAnnouncementSuccessComponent } from '../../shared/dialogs/dialog
2223
}
2324
` ]
2425
})
26+
interface TeamForm {
27+
message: FormControl<string>;
28+
linkId: FormControl<string>;
29+
teamType: FormControl<string>;
30+
}
31+
32+
interface CommunityForm {
33+
message: FormControl<string>;
34+
}
35+
2536
export class DialogsChatShareComponent implements OnInit {
2637
user = this.userService.get();
2738
conversation: any;
2839
teamInfo: any;
2940
membersInfo: any;
3041
excludeIds: any[] = [];
3142
showForm: boolean;
32-
teamForm: UntypedFormGroup;
33-
communityForm: UntypedFormGroup;
43+
teamForm: FormGroup<TeamForm>;
44+
communityForm: FormGroup<CommunityForm>;
3445

3546
@ViewChild('linkStepper') linkStepper: MatStepper;
3647
selectedLink: { db, title, selector? };
@@ -43,7 +54,7 @@ export class DialogsChatShareComponent implements OnInit {
4354
public dialogRef: MatDialogRef<DialogsChatShareComponent>,
4455
@Inject(MAT_DIALOG_DATA) public data: any,
4556
private couchService: CouchService,
46-
private formBuilder: UntypedFormBuilder,
57+
private formBuilder: FormBuilder,
4758
private newsService: NewsService,
4859
private teamsService: TeamsService,
4960
private userService: UserService,
@@ -54,27 +65,31 @@ export class DialogsChatShareComponent implements OnInit {
5465
}
5566

5667
ngOnInit() {
57-
this.communityForm = this.formBuilder.group({
68+
this.communityForm = this.formBuilder.nonNullable.group({
5869
message: ''
5970
});
60-
this.teamForm = this.formBuilder.group({
71+
this.teamForm = this.formBuilder.nonNullable.group({
6172
message: '',
6273
linkId: '',
6374
teamType: ''
6475
});
6576
this.getTeams();
6677
}
6778

68-
teamSelect({ teamId, teamType }) {
79+
teamSelect({ teamId, teamType }: { teamId: string; teamType: string }) {
6980
this.teamForm.controls.linkId.setValue(teamId);
7081
this.teamForm.controls.teamType.setValue(teamType);
7182
this.linkStepper.selected.completed = true;
7283
this.linkStepper.next();
7384
}
7485

75-
linkStepperChange({ selectedIndex }) {
86+
linkStepperChange({ selectedIndex }: StepperSelectionEvent) {
7687
if (selectedIndex === 0 && this.teamForm.pristine !== true) {
77-
this.teamForm.reset();
88+
this.teamForm.reset({
89+
message: '',
90+
linkId: '',
91+
teamType: ''
92+
});
7893
}
7994
}
8095

@@ -111,13 +126,14 @@ export class DialogsChatShareComponent implements OnInit {
111126
}
112127

113128
shareWithTeam() {
114-
let linkId, teamType;
115-
if (this.teamForm.valid) {
116-
const team = this.teamForm.value;
117-
this.conversation.message = team.message ? team.message : '</br>';
118-
({ linkId, teamType } = team);
129+
if (!this.teamForm.valid) {
130+
return;
119131
}
120132

133+
const team = this.teamForm.getRawValue();
134+
this.conversation.message = team.message ? team.message : '</br>';
135+
const { linkId, teamType } = team;
136+
121137
this.getTeam(linkId).pipe(
122138
switchMap((teamData) => {
123139
this.teamInfo = teamData;
@@ -139,7 +155,7 @@ export class DialogsChatShareComponent implements OnInit {
139155

140156
shareWithCommunity() {
141157
if (this.communityForm.valid) {
142-
const message = this.communityForm.get('message').value;
158+
const message = this.communityForm.controls.message.value;
143159
this.conversation.message = message ? { text: message, images: [] } : { text: '</br>', images: [] };
144160
}
145161
this.conversation.chat = true;

0 commit comments

Comments
 (0)