@@ -10,6 +10,7 @@ import {
1010} from 'src/app/component/modal-message/modal-message.component' ;
1111import { dateStr , deepCopy } from 'src/app/util/util' ;
1212import { MetaStore } from 'src/app/model/meta-store' ;
13+ import { ProgressStore } from 'src/app/model/progress-store' ;
1314
1415@Component ( {
1516 selector : 'app-settings' ,
@@ -18,6 +19,7 @@ import { MetaStore } from 'src/app/model/meta-store';
1819} )
1920export class SettingsComponent implements OnInit {
2021 meta ! : MetaStore ;
22+ progressStore ! : ProgressStore ;
2123 dataStoreMaxLevel ! : number ;
2224 selectedMaxLevel ! : number ;
2325 selectedMaxLevelCaption : String = '' ;
@@ -82,6 +84,10 @@ export class SettingsComponent implements OnInit {
8284 this . selectedMaxLevel = this . settingsService . getMaxLevel ( ) || this . dataStoreMaxLevel ;
8385 this . updateMaxLevelCaption ( ) ;
8486
87+ if ( dataStore . progressStore ) {
88+ this . progressStore = dataStore . progressStore ;
89+ }
90+
8591 // Load progress definitions
8692 if ( dataStore . meta ) {
8793 this . meta = dataStore . meta ;
@@ -134,9 +140,10 @@ export class SettingsComponent implements OnInit {
134140 private updateProgressDefinitionsForm ( ) : void {
135141 this . definitionsFormArray . clear ( ) ;
136142
137- Object . entries ( this . tempProgressDefinitions ) . forEach ( ( [ key , progDef ] ) => {
143+ Object . entries ( this . tempProgressDefinitions ) . forEach ( ( [ key , progDef ] , index ) => {
138144 this . definitionsFormArray . push (
139145 this . formBuilder . group ( {
146+ pid : [ index ] ,
140147 key : [ key ] ,
141148 score : [ progDef . score * 100 ] ,
142149 definition : [ progDef . definition ] ,
@@ -154,6 +161,7 @@ export class SettingsComponent implements OnInit {
154161 this . definitionsFormArray . insert (
155162 index ,
156163 this . formBuilder . group ( {
164+ pid : [ - 1 ] , // -1 indicates a new item
157165 key : [ '' ] ,
158166 score : [ score ] ,
159167 definition : [ '' ] ,
@@ -166,35 +174,74 @@ export class SettingsComponent implements OnInit {
166174 }
167175
168176 saveProgressDefinitions ( ) : void {
177+ // Validate form
169178 if ( this . progressDefinitionsForm . invalid ) {
170179 this . progressDefinitionsForm . markAllAsTouched ( ) ;
171-
172- this . modal . openDialog ( new DialogInfo (
173- 'All definitions must have a name, and the score must be between 0% and 100%'
174- ) ) ;
175-
176- return ; // Exit early if form is invalid
180+
181+ this . modal . openDialog (
182+ new DialogInfo (
183+ 'All definitions must have a name, and the score must be between 0% and 100%'
184+ )
185+ ) ;
186+ return ;
177187 }
178188
179- // Build new progress definitions from form data
189+ // Get the original keys in order
190+ const originalKeys = Object . keys ( this . tempProgressDefinitions ) ;
191+
192+ // Build new progress definitions from form data and identify changes
180193 const newProgressDefinitions : ProgressDefinitions = { } ;
181-
182- this . definitionsFormArray . controls . forEach ( ( control ) => {
194+ const renamedItems : Array < { originalKey : string ; newKey : string ; pid : number } > = [ ] ;
195+
196+ this . definitionsFormArray . controls . forEach ( control => {
183197 const formGroup = control as FormGroup ;
198+ const pid = formGroup . get ( 'pid' ) ?. value ;
184199 const key = formGroup . get ( 'key' ) ?. value ;
185200 const score = formGroup . get ( 'score' ) ?. value / 100 ; // Convert from percentage back to decimal
186201 const definition = formGroup . get ( 'definition' ) ?. value ;
187-
188- if ( key && key . trim ( ) ) { // Only add if key is not empty
202+
203+ if ( key && key . trim ( ) ) {
204+ // Only add if key is not empty
189205 newProgressDefinitions [ key ] = {
190206 score : score ,
191- definition : definition
207+ definition : definition ,
192208 } ;
209+
210+ // Check if this is a renamed item
211+ if ( pid >= 0 && pid < originalKeys . length ) {
212+ const originalKey = originalKeys [ pid ] ;
213+ if ( originalKey !== key ) {
214+ renamedItems . push ( {
215+ originalKey : originalKey ,
216+ newKey : key ,
217+ pid : pid ,
218+ } ) ;
219+ }
220+ }
193221 }
194222 } ) ;
195223
224+ // Log renamed items for debugging/tracking
225+ if ( renamedItems . length > 0 ) {
226+ console . log ( 'Renamed progress definitions:' , renamedItems ) ;
227+ for ( const item of renamedItems ) {
228+ console . log ( `- PID ${ item . pid } : "${ item . originalKey } " renamed to "${ item . newKey } "` ) ;
229+ this . progressStore . renameProgressTitle ( item . originalKey , item . newKey ) ;
230+ }
231+ }
232+
196233 // Sort the definitions by score in ascending order
197234 this . tempProgressDefinitions = this . sortObjectByScore ( newProgressDefinitions ) ;
235+
236+ // Save the new progress definitions to MetaStore and localStorage
237+ this . meta . saveProgressDefinition ( this . tempProgressDefinitions ) ;
238+
239+ // Reinitialize the ProgressStore with the new definitions
240+ this . progressStore . init ( this . tempProgressDefinitions ) ;
241+
242+ // Save progress data to localStorage
243+ this . progressStore . saveToLocalStorage ( ) ;
244+
198245 this . editingProgressDefinitions = false ;
199246 this . updateProgressDefinitionsForm ( ) ;
200247 }
@@ -220,7 +267,7 @@ export class SettingsComponent implements OnInit {
220267 */
221268 sortObjectByScore < T extends { score : number } > ( obj : { [ key : string ] : T } ) : { [ key : string ] : T } {
222269 const sortedEntries = Object . entries ( obj ) . sort ( ( [ , a ] , [ , b ] ) => a . score - b . score ) ;
223-
270+
224271 // Convert back to object
225272 return Object . fromEntries ( sortedEntries ) ;
226273 }
0 commit comments