Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: set_item action: optionally target an index or id #2215

Merged
merged 43 commits into from
Apr 30, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
Show all changes
43 commits
Select commit Hold shift + click to select a range
882cc4e
wip: set_item_at_index action
jfmcquade Mar 1, 2024
33ac196
feat: add rest_data action
jfmcquade Mar 1, 2024
a406ad2
chore: clarified comment
jfmcquade Mar 1, 2024
93d7102
Merge branch 'master' into feat/set-item-at-index
jfmcquade Mar 5, 2024
11831a9
refactor: incorporate set_item_at_index logic into set_item instead
jfmcquade Mar 5, 2024
63f4d7f
Merge branch 'master' into feat/set-item-at-index
jfmcquade Mar 6, 2024
4107611
chore: remove workaround for fixed cdr issue; properly handle _index …
jfmcquade Mar 6, 2024
60d5ef0
chore: handle _index === 0
jfmcquade Mar 6, 2024
89885d5
Merge branch 'feat/data-items-eval-context' into feat/set-item-at-index
jfmcquade Mar 6, 2024
40e86f9
fix: @item references in expressions within data-items loops
jfmcquade Mar 6, 2024
e1ea106
fix: complete merge of feat/data-items-eval-context
jfmcquade Mar 6, 2024
8b3b134
chore: remove comments
jfmcquade Mar 6, 2024
afad693
Merge branch 'feat/data-items-eval-context' into feat/set-item-at-index
jfmcquade Mar 6, 2024
c3a014d
Merge branch 'feat/data-items-eval-context' into feat/set-item-at-index
jfmcquade Mar 6, 2024
06cc641
chore: code tidy
jfmcquade Mar 6, 2024
3ca52ca
chore: code tidy
jfmcquade Mar 6, 2024
c75bb43
Merge branch 'master' into feat/set-item-at-index
jfmcquade Apr 3, 2024
989a715
chore: post-merge fix
jfmcquade Apr 3, 2024
5108803
chore: tidy comments
jfmcquade Apr 4, 2024
f9f17ca
Merge branch 'master' into feat/set-item-at-index
jfmcquade Apr 5, 2024
5c21fd8
chore: code tidy
jfmcquade Apr 5, 2024
fda82d6
fix: handle itemIndex passed from data items component
jfmcquade Apr 5, 2024
f96209d
chore: remove debug logs
jfmcquade Apr 5, 2024
40c98cf
Merge branch 'master' into feat/set-item-at-index
esmeetewinkel Apr 12, 2024
ef3047a
chore: review code tidying
chrismclarke Apr 12, 2024
b8074b9
Merge branch 'master' into feat/set-item-at-index
esmeetewinkel Apr 19, 2024
e262c20
chore: tidy console logs
jfmcquade Apr 22, 2024
350bd47
chore: reset theme file
jfmcquade Apr 22, 2024
9f9f483
chore: tidy getTargetItemRow logic
jfmcquade Apr 22, 2024
1b1e782
wip: create spec file for template variables service
jfmcquade Apr 22, 2024
2e04f56
Merge branch 'refactor/extract-dynamic-fields' into feat/set-item-at-…
jfmcquade Apr 22, 2024
b315003
wip: template variable spec test setup
jfmcquade Apr 23, 2024
72f89bb
test: add spec tests for template-variables service
jfmcquade Apr 24, 2024
9ea2aeb
refactor: set_item logic
jfmcquade Apr 24, 2024
c327d3a
test: add spec tests for dynamic data setItem logic
jfmcquade Apr 24, 2024
8a95b0d
refactor: set_item logic
jfmcquade Apr 24, 2024
2a2f1e5
test: add spec tests for dynamic data setItem logic
jfmcquade Apr 24, 2024
daddda1
merge
jfmcquade Apr 24, 2024
a0120e3
Merge branch 'master' into feat/set-item-at-index
jfmcquade Apr 24, 2024
378f256
chore: mock template-field and error-handler service bootstrap
chrismclarke Apr 29, 2024
149c868
refactor: template-variable specs
chrismclarke Apr 30, 2024
04168d3
chore: remove focused test
chrismclarke Apr 30, 2024
74a3efb
Merge branch 'master' into feat/set-item-at-index
chrismclarke Apr 30, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions packages/data-models/flowTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -389,9 +389,11 @@ export namespace FlowTypes {
"pop_up",
"process_template",
"reset_app",
"reset_data",
"save_to_device",
"set_field",
"set_item",
"set_item_at_index",
"set_items",
"set_local",
"share",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,14 @@ export class TmplDataItemsComponent extends TemplateBaseComponent implements OnD
// TODO - deep diff and only update changed
this.itemRows = replacedActionRows;
this.cdr.markForCheck();
// Without this second check, and without the delay, templates nested inside data-items loops do not render.
chrismclarke marked this conversation as resolved.
Show resolved Hide resolved
// There must be some async process somewhere but I'm not sure what it is
await new Promise<void>((resolve) => {
setTimeout(() => {
resolve();
}, 500);
});
this.cdr.markForCheck();
}

/**
Expand All @@ -103,6 +111,11 @@ export class TmplDataItemsComponent extends TemplateBaseComponent implements OnD
// for each individual item (default will be just to pick the first)
a.args = [this.dataListName, Object.values(dataList).map((v) => v.id)];
}
if (a.action_id === "set_item_at_index") {
const row_ids = Object.values(dataList).map((v) => v.id);
const currentItemIndex = row_ids.indexOf(r._evalContext.itemContext.id);
a.args = a.args.concat([this.dataListName, currentItemIndex, row_ids]);
}
return a;
});
}
Expand Down
30 changes: 29 additions & 1 deletion src/app/shared/services/dynamic-data/dynamic-data.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { FlowTypes } from "data-models";
import { environment } from "src/environments/environment";
import { AppDataService } from "../data/app-data.service";
import { AsyncServiceBase } from "../asyncService.base";
import { arrayToHashmap, deepMergeObjects } from "../../utils";
import { arrayToHashmap, deepMergeObjects, evaluateJSExpression } from "../../utils";
import { PersistedMemoryAdapter } from "./adapters/persistedMemory";
import { ReactiveMemoryAdapater, REACTIVE_SCHEMA_BASE } from "./adapters/reactiveMemory";
import { TemplateActionRegistry } from "../../components/template/services/instance/template-action.registry";
Expand Down Expand Up @@ -70,13 +70,27 @@ export class DynamicDataService extends AsyncServiceBase {
const [flow_name, row_id] = args;
await this.update("data_list", flow_name, row_id, params);
},
set_item_at_index: async ({ args, params }) => {
const [targetIndexString, flow_name, currentItemIndex, row_ids] = args;
const targetIndex = this.evaluateItemIndexString(targetIndexString, currentItemIndex);
const targetRowId = row_ids[targetIndex];
if (targetRowId) {
await this.update("data_list", flow_name, targetRowId, params);
} else {
console.warn(`[SET ITEM AT INDEX] - No item at index ${targetIndex}`);
}
},
set_items: async ({ args, params }) => {
const [flow_name, row_ids] = args;
// Hack, no current method for bulk update so make successive (changes debounced in component)
for (const row_id of row_ids) {
await this.update("data_list", flow_name, row_id, params);
}
},
// "Reset" the data of a given data list by removing its dynamic user overwrites
reset_data: async ({ args }) => {
jfmcquade marked this conversation as resolved.
Show resolved Hide resolved
this.resetFlow("data_list", args[0]);
},
});
}

Expand Down Expand Up @@ -223,4 +237,18 @@ export class DynamicDataService extends AsyncServiceBase {
}
return schema;
}

evaluateItemIndexString(targetIndexString: string, currentIndex: number) {
if (isNaN(Number(targetIndexString))) {
try {
// HACK use keyword "item_index" to refer to index of current item in loop
const jsExpression = targetIndexString.replace("item_index", currentIndex.toString());
jfmcquade marked this conversation as resolved.
Show resolved Hide resolved
return evaluateJSExpression(jsExpression);
} catch {
console.error(`[SET ITEM AT INDEX] - Invalid index selector, "${targetIndexString}"`);
}
} else {
return Number(targetIndexString);
}
}
}
Loading