Untitled
@Injectable() export class AddBeneficiaryService { private readonly remitterService = inject(RemitterService); private readonly selectedCurrency = signal<Currency | null>(null); private readonly fb = inject(FormBuilder); addForm = this.fb.nonNullable.group({ currency_id: [''], formDetails: this.fb.nonNullable.group({}) }); formFields = computed(() => { if (!this.selectedCurrency()) { return []; } return this.selectedCurrency()!.additional_data.beneficiary_fields.filter( field => field.display !== 'hidden' ); }); dynamicForm = computed(() => { const formGroup = new FormGroup({}); this.formFieldsWithData() && this.formFieldsWithData()!.forEach(field => { const validators = field.display === 'required' ? [Validators.required] : []; const control = new FormControl('', validators); formGroup.setControl(field.field_name, control); }); return formGroup; }); formFields$ = toObservable(this.formFields).pipe( map(fields => fields.map(field => iif( () => field.field_data_source !== null, this.getOptions(field.field_data_source).pipe( map(options => ({ options: options, ...field })) ), of({ ...field, options: [] as { value: string; label: string; }[] }) ) ) ), switchMap(obs => forkJoin(obs)) ); private getOptions(type: OptionType) { return this.remitterService.getOptions(type).pipe( map(response => response.predefined_options.data), map(options => options.map(option => ({ value: option.option_value, label: option.option_name })) ) ); } formFieldsWithData = toSignal(this.formFields$); formFieldsByGroup = computed(() => { return { bank: this.formFieldsWithData()?.filter(field => field.field_group === 'bank'), bene: this.formFieldsWithData()?.filter(field => field.field_group === 'beneficiary') }; }); selectCurrency(currency: Currency | null) { this.selectedCurrency.set(currency); } }
Leave a Comment