Skip to content

Commit e71bd32

Browse files
added onInputChange and onPageLoad triggers in query
1 parent 4ced9ee commit e71bd32

File tree

2 files changed

+58
-19
lines changed

2 files changed

+58
-19
lines changed

client/packages/lowcoder/src/comps/queries/queryComp.tsx

Lines changed: 36 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -97,10 +97,18 @@ interface AfterExecuteQueryAction {
9797
result: QueryResult;
9898
}
9999

100-
const TriggerTypeOptions = [
100+
export const TriggerTypeOptions = [
101+
{ label: "On Page Load", value: "onPageLoad"},
102+
{ label: "On Input Change", value: "onInputChange"},
101103
{ label: trans("query.triggerTypeAuto"), value: "automatic" },
102104
{ label: trans("query.triggerTypeManual"), value: "manual" },
103105
] as const;
106+
107+
export const JSTriggerTypeOptions = [
108+
{ label: trans("query.triggerTypeAuto"), value: "automatic" },
109+
{ label: trans("query.triggerTypeManual"), value: "manual" },
110+
];
111+
104112
export type TriggerType = ValueFromOption<typeof TriggerTypeOptions>;
105113

106114
const EventOptions = [
@@ -174,6 +182,7 @@ export type QueryChildrenType = InstanceType<typeof QueryCompTmp> extends MultiB
174182
? X
175183
: never;
176184

185+
let blockInputChangeQueries = true;
177186
/**
178187
* Logic to automatically trigger execution
179188
*/
@@ -222,10 +231,16 @@ QueryCompTmp = class extends QueryCompTmp {
222231
const isJsQuery = this.children.compType.getView() === "js";
223232
const notExecuted = this.children.lastQueryStartTime.getView() === -1;
224233
const isAutomatic = getTriggerType(this) === "automatic";
234+
const isPageLoadTrigger = getTriggerType(this) === "onPageLoad";
235+
const isInputChangeTrigger = getTriggerType(this) === "onInputChange";
225236

226237
if (
227238
action.type === CompActionTypes.UPDATE_NODES_V2 &&
228-
isAutomatic &&
239+
(
240+
isAutomatic
241+
|| isInputChangeTrigger
242+
|| (isPageLoadTrigger && notExecuted)
243+
) &&
229244
(!isJsQuery || (isJsQuery && notExecuted)) // query which has deps can be executed on page load(first time)
230245
) {
231246
const next = super.reduce(action);
@@ -250,6 +265,18 @@ QueryCompTmp = class extends QueryCompTmp {
250265
const dependsChanged = !_.isEqual(preDepends, depends);
251266
const dslNotChanged = _.isEqual(preDsl, dsl);
252267

268+
if(isInputChangeTrigger && blockInputChangeQueries && dependsChanged) {
269+
// block executing input change queries initially on page refresh
270+
setTimeout(() => {
271+
blockInputChangeQueries = false;
272+
}, 500)
273+
274+
return setFieldsNoTypeCheck(next, {
275+
[lastDependsKey]: depends,
276+
[lastDslKey]: dsl,
277+
});
278+
}
279+
253280
// If the dsl has not changed, but the dependent node value has changed, then trigger the query execution
254281
// FIXME, this should be changed to a reference judgement, but for unknown reasons if the reference is modified once, it will change twice.
255282
if (dependsChanged) {
@@ -277,7 +304,10 @@ function QueryView(props: QueryViewProps) {
277304
useEffect(() => {
278305
// Automatically load when page load
279306
if (
280-
getTriggerType(comp) === "automatic" &&
307+
(
308+
getTriggerType(comp) === "automatic"
309+
|| getTriggerType(comp) === "onPageLoad"
310+
) &&
281311
(comp as any).isDepReady &&
282312
!comp.children.isNewCreate.value
283313
) {
@@ -292,9 +322,9 @@ function QueryView(props: QueryViewProps) {
292322
getPromiseAfterDispatch(comp.dispatch, executeQueryAction({}), {
293323
notHandledError: trans("query.fixedDelayError"),
294324
}),
295-
getTriggerType(comp) === "automatic" && comp.children.periodic.getView()
296-
? comp.children.periodicTime.getView()
297-
: null
325+
getTriggerType(comp) === "automatic" && comp.children.periodic.getView()
326+
? comp.children.periodicTime.getView()
327+
: null
298328
);
299329

300330
return null;

client/packages/lowcoder/src/comps/queries/queryComp/queryPropertyView.tsx

Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ import { useSelector } from "react-redux";
2626
import { getDataSource, getDataSourceTypes } from "redux/selectors/datasourceSelectors";
2727
import { BottomResTypeEnum } from "types/bottomRes";
2828
import { EditorContext } from "../../editorState";
29-
import { QueryComp } from "../queryComp";
29+
import { JSTriggerTypeOptions, QueryComp, TriggerType, TriggerTypeOptions } from "../queryComp";
3030
import { ResourceDropdown } from "../resourceDropdown";
3131
import { NOT_SUPPORT_GUI_SQL_QUERY, SQLQuery } from "../sqlQuery/SQLQuery";
3232
import { StreamQuery } from "../httpQuery/streamQuery";
@@ -226,6 +226,13 @@ export const QueryGeneralPropertyView = (props: {
226226
comp.children.datasourceId.dispatchChangeValueAction(QUICK_REST_API_ID);
227227
}
228228

229+
const triggerOptions = useMemo(() => {
230+
if (datasourceType === "js" || datasourceType === "streamApi") {
231+
return JSTriggerTypeOptions;
232+
}
233+
return TriggerTypeOptions;
234+
}, [datasourceType]);
235+
229236
return (
230237
<QueryPropertyViewWrapper>
231238
<QuerySectionWrapper>
@@ -333,20 +340,22 @@ export const QueryGeneralPropertyView = (props: {
333340
<Dropdown
334341
placement={"bottom"}
335342
label={trans("query.triggerType")}
336-
options={
337-
[
338-
{
339-
label:
340-
(children.compType.getView() === "js" || children.compType.getView() === "streamApi")
341-
? trans("query.triggerTypePageLoad")
342-
: trans("query.triggerTypeAuto"),
343-
value: "automatic",
344-
},
345-
{ label: trans("query.triggerTypeManual"), value: "manual" },
346-
] as const
343+
options={ triggerOptions
344+
// [
345+
// { label: "On Page Load", value: "onPageLoad"},
346+
// { label: "On Input Change", value: "onInputChange"},
347+
// {
348+
// label:
349+
// (children.compType.getView() === "js" || children.compType.getView() === "streamApi")
350+
// ? trans("query.triggerTypePageLoad")
351+
// : trans("query.triggerTypeAuto"),
352+
// value: "automatic",
353+
// },
354+
// { label: trans("query.triggerTypeManual"), value: "manual" },
355+
// ] as const
347356
}
348357
value={children.triggerType.getView()}
349-
onChange={(value) => children.triggerType.dispatchChangeValueAction(value)}
358+
onChange={(value) => children.triggerType.dispatchChangeValueAction(value as TriggerType)}
350359
/>
351360
</TriggerTypeStyled>
352361
)}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy