11/** @format */
22
33import { CommonModule } from "@angular/common" ;
4- import { Component , EventEmitter , inject , Output } from "@angular/core" ;
4+ import {
5+ Component ,
6+ EventEmitter ,
7+ inject ,
8+ Input ,
9+ OnChanges ,
10+ OnInit ,
11+ Output ,
12+ SimpleChanges ,
13+ } from "@angular/core" ;
514import { tagColorsList } from "projects/core/src/consts/lists/tag-colots.list.const" ;
6- import { FormBuilder , FormGroup , FormsModule , ReactiveFormsModule } from "@angular/forms" ;
15+ import {
16+ FormBuilder ,
17+ FormGroup ,
18+ FormsModule ,
19+ ReactiveFormsModule ,
20+ Validators ,
21+ } from "@angular/forms" ;
22+
23+ export interface TagData {
24+ id ?: number ;
25+ name : string ;
26+ color : string ;
27+ }
728
829@Component ( {
930 selector : "app-create-tag-form" ,
@@ -12,15 +33,17 @@ import { FormBuilder, FormGroup, FormsModule, ReactiveFormsModule } from "@angul
1233 imports : [ CommonModule , FormsModule , ReactiveFormsModule ] ,
1334 standalone : true ,
1435} )
15- export class CreateTagFormComponent {
16- @Output ( ) createTag = new EventEmitter < { name : string ; color : string } > ( ) ;
36+ export class CreateTagFormComponent implements OnInit , OnChanges {
37+ @Input ( ) editingTag : TagData | null = null ;
38+ @Output ( ) createTag = new EventEmitter < TagData > ( ) ;
39+ @Output ( ) updateTag = new EventEmitter < TagData > ( ) ;
1740
1841 private readonly fb = inject ( FormBuilder ) ;
1942
2043 constructor ( ) {
2144 this . tagForm = this . fb . group ( {
22- tagName : [ "" ] ,
23- tagColor : [ tagColorsList [ 0 ] . color ] ,
45+ tagName : [ "" , [ Validators . required , Validators . maxLength ( 30 ) ] ] ,
46+ tagColor : [ tagColorsList [ 0 ] . name , [ Validators . required ] ] ,
2447 } ) ;
2548 }
2649
@@ -32,11 +55,27 @@ export class CreateTagFormComponent {
3255 }
3356
3457 get selectedColor ( ) : string {
35- return this . tagForm . get ( "tagColor" ) ?. value || tagColorsList [ 0 ] . color ;
58+ const colorName = this . tagForm . get ( "tagColor" ) ?. value ;
59+ const found = tagColorsList . find ( c => c . name === colorName ) ;
60+ return found ? found . color : tagColorsList [ 0 ] . color ;
61+ }
62+
63+ get isEditMode ( ) : boolean {
64+ return ! ! this . editingTag ;
65+ }
66+
67+ ngOnChanges ( changes : SimpleChanges ) : void {
68+ if ( changes [ "editingTag" ] ) {
69+ this . initFormFromEditingTag ( ) ;
70+ }
71+ }
72+
73+ ngOnInit ( ) : void {
74+ this . initFormFromEditingTag ( ) ;
3675 }
3776
38- selectTagColor ( color : string ) {
39- this . tagForm . patchValue ( { tagColor : color } ) ;
77+ selectTagColor ( colorName : string ) {
78+ this . tagForm . patchValue ( { tagColor : colorName } ) ;
4079 this . openPickColors = false ;
4180 }
4281
@@ -45,17 +84,36 @@ export class CreateTagFormComponent {
4584 event . preventDefault ( ) ;
4685
4786 const { tagName, tagColor } = this . tagForm . value ;
87+ const tagData : TagData = {
88+ name : tagName ,
89+ color : tagColor ,
90+ } ;
4891
4992 if ( ! tagName ?. trim ( ) || ! tagColor ) return ;
5093
51- this . createTag . emit ( {
52- name : tagName ,
53- color : tagColor ,
54- } ) ;
94+ if ( this . isEditMode && this . editingTag ?. id ) {
95+ this . updateTag . emit ( { ...tagData , id : this . editingTag . id } ) ;
96+ } else {
97+ this . createTag . emit ( tagData ) ;
98+ }
99+ this . resetForm ( ) ;
100+ }
55101
102+ private resetForm ( ) : void {
56103 this . tagForm . reset ( {
57104 tagName : "" ,
58- tagColor : tagColorsList [ 0 ] . color ,
105+ tagColor : tagColorsList [ 0 ] . name ,
59106 } ) ;
60107 }
108+
109+ private initFormFromEditingTag ( ) {
110+ if ( this . editingTag ) {
111+ this . tagForm . patchValue ( {
112+ tagName : this . editingTag . name ,
113+ tagColor : this . editingTag . color ,
114+ } ) ;
115+ } else {
116+ this . resetForm ( ) ;
117+ }
118+ }
61119}
0 commit comments