@@ -166,35 +166,40 @@ export class SettingsComponent implements OnInit {
166166 }
167167
168168 saveProgressDefinitions ( ) : void {
169+ if ( this . progressDefinitionsForm . invalid ) {
170+ 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
177+ }
178+
179+ // Build new progress definitions from form data
180+ const newProgressDefinitions : ProgressDefinitions = { } ;
181+
182+ this . definitionsFormArray . controls . forEach ( ( control ) => {
183+ const formGroup = control as FormGroup ;
184+ const key = formGroup . get ( 'key' ) ?. value ;
185+ const score = formGroup . get ( 'score' ) ?. value / 100 ; // Convert from percentage back to decimal
186+ const definition = formGroup . get ( 'definition' ) ?. value ;
187+
188+ if ( key && key . trim ( ) ) { // Only add if key is not empty
189+ newProgressDefinitions [ key ] = {
190+ score : score ,
191+ definition : definition
192+ } ;
193+ }
194+ } ) ;
195+
196+ // Sort the definitions by score in ascending order
197+ this . tempProgressDefinitions = this . sortObjectByScore ( newProgressDefinitions ) ;
169198 this . editingProgressDefinitions = false ;
170- // const formValue = this.definitionsFormArray.value;
171- // if (formValue) {
172- // const definitions: ProgressDefinitions = {};
173- // formValue.forEach((item: { key: string; value: number; definition?: string }) => {
174- // if (item.key && !isNaN(item.value)) {
175- // definitions[item.key] = {
176- // value: item.value / 100,
177- // definition: item.definition || `Progress level ${item.value}`
178- // };
179- // }
180- // });
181- // this.progressDefinitions = definitions;
182- // this.loader.load().then((dataStore: DataStore) => {
183- // if (dataStore.meta) {
184- // dataStore.meta.saveProgressDefinition(definitions);
185- // }
186- // });
187- // }
199+ this . updateProgressDefinitionsForm ( ) ;
188200 }
189201
190202 resetProgressDefinitions ( ) : void {
191- // this.loader.load().then((dataStore: DataStore) => {
192- // if (dataStore.meta) {
193- // dataStore.meta.resetProgressDefinition();
194- // this.progressDefinitions = dataStore.meta.progressDefinition;
195- // this.updateProgressDefinitionsForm();
196- // }
197- // });
198203 this . tempProgressDefinitions = deepCopy ( this . meta . progressDefinition ) ;
199204 this . editingProgressDefinitions = false ;
200205 this . updateProgressDefinitionsForm ( ) ;
@@ -207,4 +212,16 @@ export class SettingsComponent implements OnInit {
207212 getFormGroupValue ( control : AbstractControl , field : string ) : any {
208213 return ( control as FormGroup ) . get ( field ) ?. value ;
209214 }
215+
216+ /**
217+ * Sorts an object by the 'score' attribute of its values and returns a new object with the sorted order
218+ * @param obj The object to sort (where values have a 'score' property)
219+ * @returns A new object with entries sorted by score in ascending order
220+ */
221+ sortObjectByScore < T extends { score : number } > ( obj : { [ key : string ] : T } ) : { [ key : string ] : T } {
222+ const sortedEntries = Object . entries ( obj ) . sort ( ( [ , a ] , [ , b ] ) => a . score - b . score ) ;
223+
224+ // Convert back to object
225+ return Object . fromEntries ( sortedEntries ) ;
226+ }
210227}
0 commit comments