refactor: add service for chains settings

This commit is contained in:
2026-02-14 00:28:41 -03:00
parent 6babbea1c4
commit 7de993a765
6 changed files with 191 additions and 133 deletions

View File

@@ -1,14 +1,14 @@
import { ChangeDetectorRef, Component, inject, OnInit } from '@angular/core';
import { ChainFormGroup } from '../chain-formgroup';
import { ChainForm } from '../chain-form/chain-form';
import { ActivatedRoute, Router } from '@angular/router';
import { ActivatedRoute } from '@angular/router';
import { Observable, take, tap } from 'rxjs';
import { Chain } from '../../../../models/Chain';
import { ImageStorage } from '../../../../services/image-storage';
import { ActionBtn } from '../../../../components/action-btn/action-btn';
import { ChainDAO } from '../../../../dao/ChainDAO';
import { TranslatePipe } from '@ngx-translate/core';
import { UpperfirstPipe } from "../../../../pipes/upperfirst-pipe";
import { UpperfirstPipe } from '../../../../pipes/upperfirst-pipe';
import { ChainSettings } from '../../../../services/chain-settings';
@Component({
selector: 'app-chain-edit',
@@ -21,9 +21,8 @@ export class ChainEdit implements OnInit {
protected readonly activatedRoute = inject(ActivatedRoute);
private readonly cd = inject(ChangeDetectorRef);
private chain?: Chain;
private readonly chainDAO = inject(ChainDAO);
private readonly imageStorage = inject(ImageStorage);
private readonly router = inject(Router);
private readonly chainSettings = inject(ChainSettings);
ngOnInit() {
(<Observable<{ chain: Chain }>>this.activatedRoute.data)
@@ -36,9 +35,9 @@ export class ChainEdit implements OnInit {
}
get disabled() {
return (this.form.invalid || this.form.pristine)
return this.form.invalid || this.form.pristine;
}
async patchForm(chain: Chain) {
try {
this.form.controls.name.patchValue(chain.name);
@@ -51,31 +50,16 @@ export class ChainEdit implements OnInit {
}
} catch (e) {
console.error(e);
//TODO: reportar error
//TODO: reportar error
}
}
async update() {
try {
if (this.chain) {
this.chain.name = this.form.controls.name.value;
const formImgFile = this.form.controls.image.value;
let imgName = this.chain.name;
if (formImgFile) {
if (formImgFile.name !== imgName) {
imgName = await this.imageStorage.saveImage(formImgFile);
if (this.chain.image) {
this.imageStorage.deleteImage(this.chain.image);
}
this.chain.image = imgName;
}
}
await this.chainDAO.update(this.chain, { id: this.chain.id });
this.router.navigate(['settings', 'chains']);
}
} catch (e) {
console.error(e);
//TODO: reportar error
if (this.chain) {
const updatedName = this.form.controls.name.value;
const updatedImg = this.form.controls.image.value;
const updatedChain = new Chain(updatedName, '', this.chain.id);
await this.chainSettings.update(this.chain, updatedChain, updatedImg);
}
}
}