mirror of
https://github.com/Kareadita/Kavita.git
synced 2025-12-28 15:50:23 -05:00
94 lines
3.9 KiB
TypeScript
94 lines
3.9 KiB
TypeScript
import {
|
|
ChangeDetectionStrategy,
|
|
ChangeDetectorRef,
|
|
Component, DestroyRef,
|
|
EventEmitter,
|
|
inject,
|
|
Input,
|
|
OnChanges,
|
|
OnInit,
|
|
Output,
|
|
SimpleChanges
|
|
} from '@angular/core';
|
|
import { FormControl, FormGroup, Validators, ReactiveFormsModule } from '@angular/forms';
|
|
import { ToastrService } from 'ngx-toastr';
|
|
import { Device } from 'src/app/_models/device/device';
|
|
import { DevicePlatform, devicePlatforms } from 'src/app/_models/device/device-platform';
|
|
import { DeviceService } from 'src/app/_services/device.service';
|
|
import {takeUntilDestroyed} from "@angular/core/rxjs-interop";
|
|
import { DevicePlatformPipe } from '../../_pipes/device-platform.pipe';
|
|
import { NgbTooltip } from '@ng-bootstrap/ng-bootstrap';
|
|
import {NgIf, NgFor, NgTemplateOutlet} from '@angular/common';
|
|
import {translate, TranslocoModule} from "@ngneat/transloco";
|
|
|
|
@Component({
|
|
selector: 'app-edit-device',
|
|
templateUrl: './edit-device.component.html',
|
|
styleUrls: ['./edit-device.component.scss'],
|
|
changeDetection: ChangeDetectionStrategy.OnPush,
|
|
standalone: true,
|
|
imports: [ReactiveFormsModule, NgIf, NgbTooltip, NgFor, DevicePlatformPipe, TranslocoModule, NgTemplateOutlet]
|
|
})
|
|
export class EditDeviceComponent implements OnInit, OnChanges {
|
|
|
|
@Input() device: Device | undefined;
|
|
|
|
@Output() deviceAdded: EventEmitter<void> = new EventEmitter();
|
|
@Output() deviceUpdated: EventEmitter<Device> = new EventEmitter();
|
|
private readonly destroyRef = inject(DestroyRef);
|
|
|
|
settingsForm: FormGroup = new FormGroup({});
|
|
devicePlatforms = devicePlatforms;
|
|
|
|
|
|
constructor(public deviceService: DeviceService, private toastr: ToastrService,
|
|
private readonly cdRef: ChangeDetectorRef) { }
|
|
|
|
ngOnInit(): void {
|
|
|
|
this.settingsForm.addControl('name', new FormControl(this.device?.name || '', [Validators.required]));
|
|
this.settingsForm.addControl('email', new FormControl(this.device?.emailAddress || '', [Validators.required, Validators.email]));
|
|
this.settingsForm.addControl('platform', new FormControl(this.device?.platform || DevicePlatform.Custom, [Validators.required]));
|
|
|
|
// If user has filled in email and the platform hasn't been explicitly updated, try to update it for them
|
|
this.settingsForm.get('email')?.valueChanges.pipe(takeUntilDestroyed(this.destroyRef)).subscribe(email => {
|
|
if (this.settingsForm.get('platform')?.dirty) return;
|
|
if (email === null || email === undefined || email === '') return;
|
|
if (email.endsWith('@kindle.com')) this.settingsForm.get('platform')?.setValue(DevicePlatform.Kindle);
|
|
else if (email.endsWith('@pbsync.com')) this.settingsForm.get('platform')?.setValue(DevicePlatform.PocketBook);
|
|
else this.settingsForm.get('platform')?.setValue(DevicePlatform.Custom);
|
|
this.cdRef.markForCheck();
|
|
});
|
|
}
|
|
|
|
ngOnChanges(changes: SimpleChanges): void {
|
|
if (this.device) {
|
|
this.settingsForm.get('name')?.setValue(this.device.name);
|
|
this.settingsForm.get('email')?.setValue(this.device.emailAddress);
|
|
this.settingsForm.get('platform')?.setValue(this.device.platform);
|
|
this.cdRef.markForCheck();
|
|
this.settingsForm.markAsPristine();
|
|
}
|
|
}
|
|
|
|
addDevice() {
|
|
if (this.device !== undefined) {
|
|
this.deviceService.updateDevice(this.device.id, this.settingsForm.value.name, parseInt(this.settingsForm.value.platform, 10), this.settingsForm.value.email).subscribe(() => {
|
|
this.settingsForm.reset();
|
|
this.toastr.success(translate('toasts.device-updated'));
|
|
this.cdRef.markForCheck();
|
|
this.deviceUpdated.emit();
|
|
})
|
|
return;
|
|
}
|
|
|
|
this.deviceService.createDevice(this.settingsForm.value.name, parseInt(this.settingsForm.value.platform, 10), this.settingsForm.value.email).subscribe(() => {
|
|
this.settingsForm.reset();
|
|
this.toastr.success(translate('toasts.device-created'));
|
|
this.cdRef.markForCheck();
|
|
this.deviceAdded.emit();
|
|
});
|
|
}
|
|
|
|
}
|