Skip to content

Commit 827295d

Browse files
committed
refactor: add typed forms to planet rating
1 parent 1a0957d commit 827295d

File tree

1 file changed

+34
-16
lines changed

1 file changed

+34
-16
lines changed

src/app/shared/forms/planet-rating.component.ts

Lines changed: 34 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { Component, Input, OnChanges } from '@angular/core';
2-
import { UntypedFormBuilder, UntypedFormGroup } from '@angular/forms';
2+
import { FormBuilder, FormControl, FormGroup } from '@angular/forms';
33
import { CouchService } from '../couchdb.service';
44
import { PlanetMessageService } from '../planet-message.service';
55
import { UserService } from '../user.service';
@@ -26,6 +26,17 @@ const popupFormFields = [
2626
}
2727
];
2828

29+
interface RateFormModel {
30+
rate: number;
31+
}
32+
33+
interface PopupFormModel extends RateFormModel {
34+
comment: string;
35+
}
36+
37+
type RateFormControls = { [K in keyof RateFormModel]: FormControl<RateFormModel[K]> };
38+
type PopupFormControls = { [K in keyof PopupFormModel]: FormControl<PopupFormModel[K]> };
39+
2940
@Component({
3041
templateUrl: './planet-rating.component.html',
3142
styles: [ ` .list-item-rating {
@@ -41,31 +52,36 @@ export class PlanetRatingComponent implements OnChanges {
4152
@Input() ratingType = '';
4253
@Input() disabled = false;
4354

44-
rateForm: UntypedFormGroup;
45-
popupForm: UntypedFormGroup;
55+
rateForm: FormGroup<RateFormControls>;
56+
popupForm: FormGroup<PopupFormControls>;
4657
isPopupOpen = false;
4758
stackedBarData = [];
4859
enrolled = true;
49-
get rateFormField() {
60+
get rateFormField(): RateFormModel {
5061
return { rate: this.rating.userRating.rate || 0 };
5162
}
52-
get commentField() {
63+
get commentField(): Pick<PopupFormModel, 'comment'> {
5364
return { comment: this.rating.userRating.comment || '' };
5465
}
5566

5667
private dbName = 'ratings';
5768

5869
constructor(
59-
private fb: UntypedFormBuilder,
70+
private fb: FormBuilder,
6071
private couchService: CouchService,
6172
private planetMessage: PlanetMessageService,
6273
private userService: UserService,
6374
private dialogsForm: DialogsFormService,
6475
private ratingService: RatingService,
6576
private stateService: StateService
6677
) {
67-
this.rateForm = this.fb.group(this.rateFormField);
68-
this.popupForm = this.fb.group(Object.assign({}, this.rateFormField, this.commentField));
78+
this.rateForm = this.fb.group<RateFormControls>({
79+
rate: this.fb.nonNullable.control(this.rateFormField.rate)
80+
});
81+
this.popupForm = this.fb.group<PopupFormControls>({
82+
rate: this.fb.nonNullable.control(this.rateFormField.rate),
83+
comment: this.fb.nonNullable.control(this.commentField.comment)
84+
});
6985
}
7086

7187
ngOnChanges() {
@@ -80,8 +96,13 @@ export class PlanetRatingComponent implements OnChanges {
8096
},
8197
{ class: 'accent-color', amount: this.rating.femaleRating, align: 'right' }
8298
];
83-
this.rateForm.setValue(this.rateFormField);
84-
this.popupForm.setValue(Object.assign({}, this.rateFormField, this.commentField));
99+
this.rateForm.setValue({
100+
rate: this.rateFormField.rate
101+
});
102+
this.popupForm.setValue({
103+
rate: this.rateFormField.rate,
104+
comment: this.commentField.comment
105+
});
85106
}
86107

87108
isEnrolled(id: any, type: any): boolean {
@@ -90,7 +111,7 @@ export class PlanetRatingComponent implements OnChanges {
90111
return inShelf;
91112
}
92113

93-
onStarClick(form = this.rateForm) {
114+
onStarClick(form: FormGroup<RateFormControls> | FormGroup<PopupFormControls> = this.rateForm) {
94115
if (!this.isEnrolled(this.item._id, this.ratingType)) {
95116
if (this.ratingType === 'course') {
96117
this.planetMessage.showMessage($localize`Please join the ${this.ratingType} before rating!`);
@@ -121,7 +142,7 @@ export class PlanetRatingComponent implements OnChanges {
121142
});
122143
}
123144

124-
updateRating(form) {
145+
updateRating(form: FormGroup<RateFormControls> | FormGroup<PopupFormControls>) {
125146
// Later parameters of Object.assign will overwrite values from previous objects
126147
const configuration = this.stateService.configuration;
127148
const newRating = {
@@ -162,9 +183,6 @@ export class PlanetRatingComponent implements OnChanges {
162183
ratingError() {
163184
this.planetMessage.showAlert($localize`There was an issue updating your rating`);
164185
this.rateForm.patchValue({ rate: this.rating.userRating.rate || 0 });
165-
// If the dialog is open, then there will also be a comment control to reset
166-
if (this.rateForm.controls.comment) {
167-
this.rateForm.patchValue({ comment: this.rating.userRating.comment || '' });
168-
}
186+
this.popupForm.patchValue({ comment: this.rating.userRating.comment || '' });
169187
}
170188
}

0 commit comments

Comments
 (0)