/** * 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, inject, Input } from '@angular/core'; import { UntypedFormControl, ReactiveFormsModule } from '@angular/forms'; import { MatDialog } from '@angular/material/dialog'; import { UntypedFormBuilder, Validators } from '@angular/forms'; /** Custom Services */ import { SettingsService } from 'app/settings/settings.service'; import { MatFormField, MatLabel, MatPrefix, MatError, SubscriptSizing } from '@angular/material/form-field'; import { MatIcon } from '@angular/material/icon'; import { STANDALONE_SHARED_IMPORTS } from 'app/standalone-shared.module'; /** * Server Selector Component */ @Component({ selector: 'mifosx-server-selector', templateUrl: './server-selector.component.html', styleUrls: ['./server-selector.component.scss'], imports: [ ...STANDALONE_SHARED_IMPORTS, MatPrefix, MatIcon ], changeDetection: ChangeDetectionStrategy.OnPush }) export class ServerSelectorComponent implements OnInit { private settingsService = inject(SettingsService); dialog = inject(MatDialog); private formBuilder = inject(UntypedFormBuilder); /** Appearance of the form field (fill, outline). Defaults to 'fill'. */ @Input() appearance: 'fill' | 'outline' = 'fill'; /** Show label in the form field. Defaults to true. */ @Input() showLabel: boolean = true; /** * Whether the hint/error row below the control reserves space when empty. * Defaults to 'fixed', matching Angular Material. Pass 'dynamic' where the * selector sits in a row that has to align with something else. */ @Input() subscriptSizing: SubscriptSizing = 'fixed'; /** Input server. */ form: any; /** Server Settings. */ servers: string[]; /** Server Setting */ serverSelector = new UntypedFormControl(''); /** Server list to show */ existMoreThanOneServer = false; ngOnInit(): void { this.servers = this.settingsService.servers; this.existMoreThanOneServer = this.servers && this.servers.length > 1; if (!this.existMoreThanOneServer && this.servers?.[0]) { this.settingsService.setServer(this.servers[0]); } else { this.existMoreThanOneServer = true; this.serverSelector.patchValue(this.settingsService.server); this.form = this.formBuilder.group({ url: [ '', [Validators.required] ] }); } } /** * Set backend server from the list */ setServer(): void { this.settingsService.setServer(this.serverSelector.value); } /** * Add new server to the list. */ addNewServer(): void { let servers; let url = this.form.value.url; if (url.endsWith('/')) { url = url.slice(0, -1); } this.settingsService.setServer(url); servers = this.settingsService.servers; servers.push(url); this.settingsService.setServers(servers); window.location.reload(); } }