Skip to content

Commit

Permalink
Merge pull request #2644 from IDEMSInternational/feat/combo-box-items
Browse files Browse the repository at this point in the history
feat: add support for combo_box child data_items
  • Loading branch information
chrismclarke authored Dec 20, 2024
2 parents ea428a4 + c000eac commit 5d66e2a
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
<div
class="container-radio"
[class.checked-radio]="form.get('answer').value === radio.name"
*ngFor="let radio of valuesFromListAnswers"
*ngFor="let radio of answerOptions()"
>
<label>
<input
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { Component, Input, OnInit } from "@angular/core";
import { Component, input, Input, OnInit } from "@angular/core";
import { FlowTypes } from "src/app/shared/model";
import {
getAnswerListParamFromTemplateRow,
getBooleanParamFromTemplateRow,
getNumberParamFromTemplateRow,
getStringParamFromTemplateRow,
Expand All @@ -16,8 +15,8 @@ import { ModalController } from "@ionic/angular";
styleUrls: ["./combo-box-modal.component.scss"],
})
export class ComboBoxModalComponent implements OnInit {
public answerOptions = input.required<IAnswerListItem[]>();
@Input() row: FlowTypes.TemplateRow;
@Input() template: FlowTypes.Template;
@Input() selectedValue: string;
@Input() customAnswerSelected: boolean;
@Input() style: string;
Expand All @@ -29,15 +28,17 @@ export class ComboBoxModalComponent implements OnInit {
inputPosition: boolean = false;
maxLength: number = 30;
placeholder: string = "";
constructor(private fb: FormBuilder, private modalController: ModalController) {}
constructor(
private fb: FormBuilder,
private modalController: ModalController
) {}

ngOnInit() {
this.getParams();
this.calculateAnswer();
}

getParams() {
this.valuesFromListAnswers = getAnswerListParamFromTemplateRow(this.row, "answer_list", null);
this.textTitle = getStringParamFromTemplateRow(this.row, "text", null);
this.inputAllowed = getBooleanParamFromTemplateRow(this.row, "input_allowed", false);
this.inputPosition =
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { Component, Input, OnDestroy, OnInit } from "@angular/core";
import { FlowTypes } from "../../../../model";
import { Component, computed, OnDestroy, OnInit } from "@angular/core";
import { ModalController } from "@ionic/angular";
import { ComboBoxModalComponent } from "./combo-box-modal/combo-box-modal.component";
import {
Expand All @@ -10,8 +9,9 @@ import {
} from "src/app/shared/utils";
import { TemplateBaseComponent } from "../base";
import { ITemplateRowProps } from "../../models";
import { TemplateService } from "../../services/template.service";
import { ReplaySubject } from "rxjs";
import { ReplaySubject, map, filter, switchMap } from "rxjs";
import { DataItemsService } from "../data-items/data-items.service";
import { toObservable, toSignal } from "@angular/core/rxjs-interop";

@Component({
selector: "plh-combo-box",
Expand All @@ -22,19 +22,34 @@ export class TmplComboBoxComponent
extends TemplateBaseComponent
implements ITemplateRowProps, OnInit, OnDestroy
{
@Input() template: FlowTypes.Template;
placeholder: string;
prioritisePlaceholder: boolean;
style: string;
text = "";
customAnswerSelected: boolean = false;
customAnswerText: string;
answerList: IAnswerListItem[];
public placeholder: string;
public prioritisePlaceholder: boolean;
private style: string;
public text = "";
private customAnswerSelected: boolean = false;
private customAnswerText: string;
private componentDestroyed$ = new ReplaySubject(1);

// HACK - allow combo_box to include data_items child row to define answer list
private dataItemRows = toSignal(
toObservable(this.rows).pipe(
map((rows) => rows.find((r) => r.type === "data_items")),
filter((row) => row !== undefined),
switchMap((row) => this.dataItemsService.getItemsObservable(row, this.parent.templateRowMap))
)
);

private answerOptions = computed(() => {
const dataItemRows = this.dataItemRows();
if (dataItemRows !== undefined) {
return (dataItemRows as IAnswerListItem[]) || [];
}
return getAnswerListParamFromTemplateRow(this.rowSignal(), "answer_list", []);
});

constructor(
private modalController: ModalController,
private templateService: TemplateService
private dataItemsService: DataItemsService
) {
super();
}
Expand All @@ -43,15 +58,16 @@ export class TmplComboBoxComponent
this.getParams();

this.customAnswerSelected =
this.answerList.length > 0 && this._row.value
? !this.answerList.find((x) => x.name === this._row.value)
this.answerOptions().length > 0 && this._row.value
? !this.answerOptions().find((x) => x.name === this._row.value)
: false;

this.text = "";
if (this._row.value) {
this.text = this.customAnswerSelected
? this.customAnswerText
: this.answerList.find((answerListItem) => answerListItem.name === this._row.value)?.text;
: this.answerOptions().find((answerListItem) => answerListItem.name === this._row.value)
?.text;
}
}

Expand All @@ -63,16 +79,15 @@ export class TmplComboBoxComponent
false
);
this.style = getStringParamFromTemplateRow(this._row, "style", "");
this.answerList = getAnswerListParamFromTemplateRow(this._row, "answer_list", []);
}

async openModal() {
const modal = await this.modalController.create({
component: ComboBoxModalComponent,
cssClass: "combo-box-modal",
componentProps: {
answerOptions: this.answerOptions,
row: this._row,
template: this.template,
selectedValue: this.customAnswerSelected ? this.text : this._row.value,
customAnswerSelected: this.customAnswerSelected,
style: this.style,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,11 @@ export class DataItemsService {
const parsedItemList = await this.hackParseDataList(data);
const itemProcessor = new ItemProcessor(parsedItemList, parameter_list);
const { itemTemplateRows, itemData } = itemProcessor.process(rows);
// if no child rows for data_items loop assume want back raw items
if (rows.length === 0) {
return itemData;
}
// otherwise process generated template rows
const itemRowsWithMeta = updateItemMeta(itemTemplateRows, itemData, dataListName);
const parsedItemRows = await this.hackProcessRows(
itemRowsWithMeta,
Expand Down
1 change: 0 additions & 1 deletion src/app/shared/utils/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,6 @@ export function getAnswerListParamFromTemplateRow(
_default: IAnswerListItem[]
): IAnswerListItem[] {
const params = row.parameter_list || {};
console.log(params[name]);
return params.hasOwnProperty(name) ? parseAnswerList(params[name]) : _default;
}

Expand Down

0 comments on commit 5d66e2a

Please sign in to comment.