Skip to content

Commit

Permalink
portalicious: add onDestroy -> unsubscribe to avoid memory leak
Browse files Browse the repository at this point in the history
AB#31008
  • Loading branch information
aberonni committed Oct 30, 2024
1 parent 1a93e7e commit 1bfbafe
Showing 1 changed file with 27 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
inject,
input,
model,
OnDestroy,
signal,
} from '@angular/core';
import {
Expand All @@ -21,6 +22,7 @@ import { ButtonModule } from 'primeng/button';
import { DialogModule } from 'primeng/dialog';
import { DropdownModule } from 'primeng/dropdown';
import { RadioButtonModule } from 'primeng/radiobutton';
import { Subscription } from 'rxjs';

import { FormErrorComponent } from '~/components/form-error/form-error.component';
import { NotificationApiService } from '~/domains/notification/notification.api.service';
Expand Down Expand Up @@ -60,14 +62,16 @@ type SendMessageFormGroup =
styles: ``,
changeDetection: ChangeDetectionStrategy.OnPush,
})
export class SendMessageDialogComponent {
export class SendMessageDialogComponent implements OnDestroy {
readonly projectId = input.required<number>();

private messagingService = inject(MessagingService);
private notificationApiService = inject(NotificationApiService);
private registrationApiService = inject(RegistrationApiService);
private toastService = inject(ToastService);

private messageTypeFieldSubscription: Subscription;

actionData = signal<ActionDataWithPaginateQuery<Registration> | undefined>(
undefined,
);
Expand Down Expand Up @@ -111,23 +115,25 @@ export class SendMessageDialogComponent {
const messageTemplateKeyField = this.formGroup.controls.messageTemplateKey;
const customMessageField = this.formGroup.controls.customMessage;

messageTypeField.valueChanges.subscribe((type) => {
if (type === 'template') {
// eslint-disable-next-line @typescript-eslint/unbound-method
messageTemplateKeyField.setValidators([Validators.required]);
customMessageField.clearValidators();
} else {
customMessageField.setValidators([
this.messageTypeFieldSubscription = messageTypeField.valueChanges.subscribe(
(type) => {
if (type === 'template') {
// eslint-disable-next-line @typescript-eslint/unbound-method
Validators.required,
Validators.minLength(20),
]);
messageTemplateKeyField.clearValidators();
}

messageTemplateKeyField.updateValueAndValidity();
customMessageField.updateValueAndValidity();
});
messageTemplateKeyField.setValidators([Validators.required]);
customMessageField.clearValidators();
} else {
customMessageField.setValidators([
// eslint-disable-next-line @typescript-eslint/unbound-method
Validators.required,
Validators.minLength(20),
]);
messageTemplateKeyField.clearValidators();
}

messageTemplateKeyField.updateValueAndValidity();
customMessageField.updateValueAndValidity();
},
);
}

triggerAction(actionData: ActionDataWithPaginateQuery<Registration>) {
Expand Down Expand Up @@ -181,4 +187,8 @@ export class SendMessageDialogComponent {
onFormSubmit(): void {
this.sendMessageMutation.mutate(this.formGroup.getRawValue());
}

ngOnDestroy(): void {
this.messageTypeFieldSubscription.unsubscribe();
}
}

0 comments on commit 1bfbafe

Please sign in to comment.