-
Notifications
You must be signed in to change notification settings - Fork 930
Expand file tree
/
Copy pathsettings.component.ts
More file actions
189 lines (177 loc) · 5.63 KB
/
settings.component.ts
File metadata and controls
189 lines (177 loc) · 5.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
/**
* Copyright since 2025 Mifos Initiative
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/
/** Angular Imports */
import { ChangeDetectionStrategy, Component, OnInit, OnDestroy, inject } from '@angular/core';
import { FormControl, ReactiveFormsModule } from '@angular/forms';
import { Subject, merge } from 'rxjs';
import { takeUntil } from 'rxjs/operators';
/** Custom Services */
import { SettingsService } from './settings.service';
import { AlertService } from 'app/core/alert/alert.service';
import { TranslateService } from '@ngx-translate/core';
import {
MatAccordion,
MatExpansionPanel,
MatExpansionPanelHeader,
MatExpansionPanelTitle
} from '@angular/material/expansion';
import { FileUploadComponent } from '../shared/file-upload/file-upload.component';
import { ThemePickerComponent } from '../shared/theme-picker/theme-picker.component';
import { LanguageSelectorComponent } from '../shared/language-selector/language-selector.component';
import { STANDALONE_SHARED_IMPORTS } from 'app/standalone-shared.module';
/**
* Settings component.
*/
@Component({
selector: 'mifosx-settings',
templateUrl: './settings.component.html',
styleUrls: ['./settings.component.scss'],
imports: [
...STANDALONE_SHARED_IMPORTS,
MatAccordion,
MatExpansionPanel,
MatExpansionPanelHeader,
MatExpansionPanelTitle,
FileUploadComponent,
ThemePickerComponent,
LanguageSelectorComponent
],
changeDetection: ChangeDetectionStrategy.OnPush
})
export class SettingsComponent implements OnInit, OnDestroy {
private settingsService = inject(SettingsService);
private alertService = inject(AlertService);
private translateService = inject(TranslateService);
private destroy$ = new Subject<void>();
hasChanges = false;
/** Date formats. */
dateFormats: string[] = [
'dd MMMM yyyy',
'dd/MM/yyyy',
'dd/MMMM/yyyy',
'dd-MM-yyyy',
'dd-MMMM-yyyy',
'dd-MM-yy',
'MM/dd/yyyy',
'MMMM-dd-yyyy',
'MMMM dd yyyy',
'MMMM/dd/yyyy',
'MM-dd-yy',
'yyyy-MM-dd'
];
datetimeFormats: string[] = [
// All date formats with HH:mm:ss (seconds)
'dd MMMM yyyy HH:mm:ss',
'dd/MMMM/yyyy HH:mm:ss',
'dd-MMMM-yyyy HH:mm:ss',
'dd-MM-yy HH:mm:ss',
'MMMM-dd-yyyy HH:mm:ss',
'MMMM dd yyyy HH:mm:ss',
'MMMM/dd/yyyy HH:mm:ss',
'MM-dd-yy HH:mm:ss',
'yyyy-MM-dd HH:mm:ss',
// All date formats with HH:mm (no seconds)
'dd MMMM yyyy HH:mm',
'dd/MMMM/yyyy HH:mm',
'dd-MMMM-yyyy HH:mm',
'dd-MM-yy HH:mm',
'MMMM-dd-yyyy HH:mm',
'MMMM dd yyyy HH:mm',
'MMMM/dd/yyyy HH:mm',
'MM-dd-yy HH:mm',
'yyyy-MM-dd HH:mm'
];
/** Decimals. */
decimals: string[] = [
'0',
'1',
'2',
'3',
'4',
'5',
'6',
'7',
'8'
];
/** List of available fonts. */
fonts: string[] = [
'Roboto',
'Inter',
'Open Sans',
'Outfit',
'Montserrat'
];
/** Date Format Setting */
dateFormat = new FormControl('');
/** Datetime Format Setting */
datetimeFormat = new FormControl('');
/** Decimals to Display Setting */
decimalsToDisplay = new FormControl('');
/** Font Family Setting */
fontFamily = new FormControl('');
private initialValues: {
dateFormat: string;
datetimeFormat: string;
decimals: string;
fontFamily: string;
};
ngOnInit() {
this.initialValues = {
dateFormat: this.settingsService.dateFormat,
datetimeFormat: this.settingsService.datetimeFormat,
decimals: this.settingsService.decimals,
fontFamily: this.settingsService.fontFamily
};
this.dateFormat.patchValue(this.initialValues.dateFormat, { emitEvent: false });
this.datetimeFormat.patchValue(this.initialValues.datetimeFormat, { emitEvent: false });
this.decimalsToDisplay.patchValue(this.initialValues.decimals, { emitEvent: false });
this.fontFamily.patchValue(this.initialValues.fontFamily, { emitEvent: false });
this.trackChanges();
}
trackChanges(): void {
merge(
this.dateFormat.valueChanges,
this.datetimeFormat.valueChanges,
this.decimalsToDisplay.valueChanges,
this.fontFamily.valueChanges
)
.pipe(takeUntil(this.destroy$))
.subscribe(() => {
this.hasChanges = this.hasFormChanged();
});
}
private hasFormChanged(): boolean {
return (
(this.dateFormat.value ?? '') !== this.initialValues.dateFormat ||
(this.datetimeFormat.value ?? '') !== this.initialValues.datetimeFormat ||
(this.decimalsToDisplay.value ?? '') !== this.initialValues.decimals ||
(this.fontFamily.value ?? '') !== this.initialValues.fontFamily
);
}
submit(): void {
this.settingsService.setDateFormat(this.dateFormat.value ?? this.initialValues.dateFormat);
this.settingsService.setDatetimeFormat(this.datetimeFormat.value ?? this.initialValues.datetimeFormat);
this.settingsService.setDecimalToDisplay(this.decimalsToDisplay.value ?? this.initialValues.decimals);
this.settingsService.setFontFamily(this.fontFamily.value ?? this.initialValues.fontFamily);
this.initialValues = {
dateFormat: this.dateFormat.value ?? '',
datetimeFormat: this.datetimeFormat.value ?? '',
decimals: this.decimalsToDisplay.value ?? '',
fontFamily: this.fontFamily.value ?? ''
};
this.hasChanges = false;
this.alertService.alert({
type: 'Settings Update',
message: this.translateService.instant('labels.text.Settings saved successfully')
});
}
ngOnDestroy(): void {
this.destroy$.next();
this.destroy$.complete();
}
}