Files
LoreMind/web/src/app/campaigns/character/character-edit/character-edit.component.ts
IETM_FIXE\ietm6 af3a6d443c
Some checks failed
Build & Push Images / build (brain) (push) Has been cancelled
Build & Push Images / build (core) (push) Has been cancelled
Build & Push Images / build (web) (push) Has been cancelled
Build & Push Images / build-switcher (push) Has been cancelled
Mise en place de l'anglais comme deuxième langue pour l'application
2026-06-14 16:24:05 +02:00

175 lines
6.5 KiB
TypeScript

import { Component, OnInit } from '@angular/core';
import { ActivatedRoute, Router } from '@angular/router';
import { FormsModule } from '@angular/forms';
import { LucideAngularModule, Save, ArrowLeft, User, Trash2, Sparkles } from 'lucide-angular';
import { TranslatePipe, TranslateService } from '@ngx-translate/core';
import { CharacterService } from '../../../services/character.service';
import { CampaignService } from '../../../services/campaign.service';
import { GameSystemService } from '../../../services/game-system.service';
import { CampaignSidebarService } from '../../../services/campaign-sidebar.service';
import { TemplateField } from '../../../services/template.model';
import { AiChatDrawerComponent } from '../../../shared/ai-chat-drawer/ai-chat-drawer.component';
import { DynamicFieldsFormComponent } from '../../../shared/dynamic-fields-form/dynamic-fields-form.component';
import { SingleImagePickerComponent } from '../../../shared/single-image-picker/single-image-picker.component';
import { ConfirmDialogService } from '../../../shared/confirm-dialog/confirm-dialog.service';
/**
* Editeur plein ecran d'une fiche de personnage (PJ).
* Refonte 2026-04-30 : remplace le markdown libre par un formulaire dynamique
* pilote par le characterTemplate du GameSystem associe a la campagne.
*
* Comportements :
* - Si la campagne n'a pas de GameSystem ou si son template est vide, affiche
* uniquement les champs universels (nom, portrait, header).
* - Le picker d'images dedie portrait/header est hors scope MVP — pour l'instant
* saisie manuelle d'IDs d'images.
*/
@Component({
selector: 'app-character-edit',
imports: [FormsModule, LucideAngularModule, TranslatePipe, AiChatDrawerComponent, DynamicFieldsFormComponent, SingleImagePickerComponent],
templateUrl: './character-edit.component.html',
styleUrls: ['./character-edit.component.scss']
})
export class CharacterEditComponent implements OnInit {
readonly Save = Save;
readonly ArrowLeft = ArrowLeft;
readonly User = User;
readonly Trash2 = Trash2;
readonly Sparkles = Sparkles;
chatOpen = false;
get chatQuickSuggestions(): string[] {
return [
this.translate.instant('characterEdit.chatSuggestion1'),
this.translate.instant('characterEdit.chatSuggestion2'),
this.translate.instant('characterEdit.chatSuggestion3')
];
}
toggleChat(): void { this.chatOpen = !this.chatOpen; }
campaignId: string | null = null;
/** Partie propriétaire du PJ — lue depuis la route (les PJ sont scoping Playthrough). */
playthroughId: string | null = null;
characterId: string | null = null;
name = '';
portraitImageId: string | null = null;
headerImageId: string | null = null;
values: Record<string, string> = {};
imageValues: Record<string, string[]> = {};
keyValueValues: Record<string, Record<string, string>> = {};
templateFields: TemplateField[] = [];
private order = 0;
constructor(
private route: ActivatedRoute,
private router: Router,
private service: CharacterService,
private campaignService: CampaignService,
private gameSystemService: GameSystemService,
private campaignSidebar: CampaignSidebarService,
private confirmDialog: ConfirmDialogService,
private translate: TranslateService
) {}
ngOnInit(): void {
const params = this.route.snapshot.paramMap;
this.campaignId = params.get('campaignId');
this.playthroughId = params.get('playthroughId');
this.characterId = params.get('characterId');
if (this.campaignId) {
this.loadTemplateForCampaign(this.campaignId);
this.campaignSidebar.show(this.campaignId);
}
if (this.characterId) {
this.service.getById(this.characterId).subscribe({
next: (c) => {
this.name = c.name;
this.portraitImageId = c.portraitImageId ?? null;
this.headerImageId = c.headerImageId ?? null;
this.values = c.values ?? {};
this.imageValues = c.imageValues ?? {};
this.keyValueValues = c.keyValueValues ?? {};
this.order = c.order ?? 0;
},
error: () => this.back()
});
}
}
private loadTemplateForCampaign(campaignId: string): void {
this.campaignService.getCampaignById(campaignId).subscribe({
next: (campaign) => {
if (!campaign.gameSystemId) {
this.templateFields = [];
return;
}
this.gameSystemService.getById(campaign.gameSystemId).subscribe({
next: (gs) => { this.templateFields = gs.characterTemplate ?? []; },
error: () => { this.templateFields = []; }
});
},
error: () => { this.templateFields = []; }
});
}
submit(): void {
if (!this.name.trim() || !this.campaignId || !this.playthroughId) return;
const payload = {
name: this.name.trim(),
portraitImageId: this.portraitImageId,
headerImageId: this.headerImageId,
values: this.values,
imageValues: this.imageValues,
keyValueValues: this.keyValueValues,
playthroughId: this.playthroughId
};
const isCreation = !this.characterId;
const req = this.characterId
? this.service.update(this.characterId, { ...payload, id: this.characterId, order: this.order })
: this.service.create(payload);
req.subscribe({
next: (saved) => {
if (isCreation && saved.id) {
this.router.navigate(['/campaigns', this.campaignId, 'playthroughs', this.playthroughId, 'characters', saved.id]);
} else {
this.back();
}
},
error: () => console.error('Erreur sauvegarde Character')
});
}
deleteCharacter(): void {
if (!this.characterId) return;
this.confirmDialog.confirm({
title: this.translate.instant('characterEdit.deleteTitle'),
message: this.translate.instant('characterEdit.deleteMessage', { name: this.name }),
details: [this.translate.instant('characterEdit.irreversible')],
confirmLabel: this.translate.instant('common.delete'),
variant: 'danger'
}).then(ok => {
if (!ok || !this.characterId) return;
this.service.delete(this.characterId).subscribe({
next: () => this.back(),
error: () => console.error('Erreur suppression Character')
});
});
}
back(): void {
if (this.campaignId && this.playthroughId) {
this.router.navigate(['/campaigns', this.campaignId, 'playthroughs', this.playthroughId]);
} else if (this.campaignId) {
this.router.navigate(['/campaigns', this.campaignId]);
} else {
this.router.navigate(['/campaigns']);
}
}
}