Skip to content

Commit 4cfdb48

Browse files
committed
Settings: Progress definition: Accept changes
1 parent ef2ea56 commit 4cfdb48

2 files changed

Lines changed: 49 additions & 26 deletions

File tree

src/app/pages/settings/settings.component.html

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,14 +87,19 @@ <h2>Progress Definitions</h2>
8787
<ng-container [formGroup]="getDefinitionGroup(i)">
8888
<mat-form-field appearance="outline" class="grid-cell progress-key edit">
8989
<mat-label>Name</mat-label>
90-
<input matInput formControlName="key" />
90+
<input
91+
matInput
92+
required minlength="1"
93+
formControlName="key" />
9194
</mat-form-field>
9295

9396
<mat-form-field appearance="outline" class="grid-cell progress-score edit">
9497
<mat-label>Score</mat-label>
9598
<input
9699
matInput
97100
type="number"
101+
min="0"
102+
max="100"
98103
class="progress-score"
99104
formControlName="score"
100105
[disabled]="getDefinitionGroup(i).get('mandatory')?.value" />
@@ -105,6 +110,7 @@ <h2>Progress Definitions</h2>
105110
<mat-label>Definition</mat-label>
106111
<textarea
107112
matInput
113+
required minlength="1"
108114
formControlName="definition"
109115
cdkTextareaAutosize
110116
cdkAutosizeMinRows="1"

src/app/pages/settings/settings.component.ts

Lines changed: 42 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)