Skip to content

Feat/query triggers #1443

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

Merged
merged 5 commits into from
Jan 14, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
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
Next Next commit
added onInputChange and onPageLoad triggers in query
  • Loading branch information
raheeliftikhar5 committed Jan 14, 2025
commit e71bd321ea6df714089fa167fa6cc0d10c959147
42 changes: 36 additions & 6 deletions client/packages/lowcoder/src/comps/queries/queryComp.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -97,10 +97,18 @@ interface AfterExecuteQueryAction {
result: QueryResult;
}

const TriggerTypeOptions = [
export const TriggerTypeOptions = [
{ label: "On Page Load", value: "onPageLoad"},
{ label: "On Input Change", value: "onInputChange"},
{ label: trans("query.triggerTypeAuto"), value: "automatic" },
{ label: trans("query.triggerTypeManual"), value: "manual" },
] as const;

export const JSTriggerTypeOptions = [
{ label: trans("query.triggerTypeAuto"), value: "automatic" },
{ label: trans("query.triggerTypeManual"), value: "manual" },
];

export type TriggerType = ValueFromOption<typeof TriggerTypeOptions>;

const EventOptions = [
Expand Down Expand Up @@ -174,6 +182,7 @@ export type QueryChildrenType = InstanceType<typeof QueryCompTmp> extends MultiB
? X
: never;

let blockInputChangeQueries = true;
/**
* Logic to automatically trigger execution
*/
Expand Down Expand Up @@ -222,10 +231,16 @@ QueryCompTmp = class extends QueryCompTmp {
const isJsQuery = this.children.compType.getView() === "js";
const notExecuted = this.children.lastQueryStartTime.getView() === -1;
const isAutomatic = getTriggerType(this) === "automatic";
const isPageLoadTrigger = getTriggerType(this) === "onPageLoad";
const isInputChangeTrigger = getTriggerType(this) === "onInputChange";

if (
action.type === CompActionTypes.UPDATE_NODES_V2 &&
isAutomatic &&
(
isAutomatic
|| isInputChangeTrigger
|| (isPageLoadTrigger && notExecuted)
) &&
(!isJsQuery || (isJsQuery && notExecuted)) // query which has deps can be executed on page load(first time)
) {
const next = super.reduce(action);
Expand All @@ -250,6 +265,18 @@ QueryCompTmp = class extends QueryCompTmp {
const dependsChanged = !_.isEqual(preDepends, depends);
const dslNotChanged = _.isEqual(preDsl, dsl);

if(isInputChangeTrigger && blockInputChangeQueries && dependsChanged) {
// block executing input change queries initially on page refresh
setTimeout(() => {
blockInputChangeQueries = false;
}, 500)

return setFieldsNoTypeCheck(next, {
[lastDependsKey]: depends,
[lastDslKey]: dsl,
});
}

// If the dsl has not changed, but the dependent node value has changed, then trigger the query execution
// FIXME, this should be changed to a reference judgement, but for unknown reasons if the reference is modified once, it will change twice.
if (dependsChanged) {
Expand Down Expand Up @@ -277,7 +304,10 @@ function QueryView(props: QueryViewProps) {
useEffect(() => {
// Automatically load when page load
if (
getTriggerType(comp) === "automatic" &&
(
getTriggerType(comp) === "automatic"
|| getTriggerType(comp) === "onPageLoad"
) &&
(comp as any).isDepReady &&
!comp.children.isNewCreate.value
) {
Expand All @@ -292,9 +322,9 @@ function QueryView(props: QueryViewProps) {
getPromiseAfterDispatch(comp.dispatch, executeQueryAction({}), {
notHandledError: trans("query.fixedDelayError"),
}),
getTriggerType(comp) === "automatic" && comp.children.periodic.getView()
? comp.children.periodicTime.getView()
: null
getTriggerType(comp) === "automatic" && comp.children.periodic.getView()
? comp.children.periodicTime.getView()
: null
);

return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import { useSelector } from "react-redux";
import { getDataSource, getDataSourceTypes } from "redux/selectors/datasourceSelectors";
import { BottomResTypeEnum } from "types/bottomRes";
import { EditorContext } from "../../editorState";
import { QueryComp } from "../queryComp";
import { JSTriggerTypeOptions, QueryComp, TriggerType, TriggerTypeOptions } from "../queryComp";
import { ResourceDropdown } from "../resourceDropdown";
import { NOT_SUPPORT_GUI_SQL_QUERY, SQLQuery } from "../sqlQuery/SQLQuery";
import { StreamQuery } from "../httpQuery/streamQuery";
Expand Down Expand Up @@ -226,6 +226,13 @@ export const QueryGeneralPropertyView = (props: {
comp.children.datasourceId.dispatchChangeValueAction(QUICK_REST_API_ID);
}

const triggerOptions = useMemo(() => {
if (datasourceType === "js" || datasourceType === "streamApi") {
return JSTriggerTypeOptions;
}
return TriggerTypeOptions;
}, [datasourceType]);

return (
<QueryPropertyViewWrapper>
<QuerySectionWrapper>
Expand Down Expand Up @@ -333,20 +340,22 @@ export const QueryGeneralPropertyView = (props: {
<Dropdown
placement={"bottom"}
label={trans("query.triggerType")}
options={
[
{
label:
(children.compType.getView() === "js" || children.compType.getView() === "streamApi")
? trans("query.triggerTypePageLoad")
: trans("query.triggerTypeAuto"),
value: "automatic",
},
{ label: trans("query.triggerTypeManual"), value: "manual" },
] as const
options={ triggerOptions
// [
// { label: "On Page Load", value: "onPageLoad"},
// { label: "On Input Change", value: "onInputChange"},
// {
// label:
// (children.compType.getView() === "js" || children.compType.getView() === "streamApi")
// ? trans("query.triggerTypePageLoad")
// : trans("query.triggerTypeAuto"),
// value: "automatic",
// },
// { label: trans("query.triggerTypeManual"), value: "manual" },
// ] as const
}
value={children.triggerType.getView()}
onChange={(value) => children.triggerType.dispatchChangeValueAction(value)}
onChange={(value) => children.triggerType.dispatchChangeValueAction(value as TriggerType)}
/>
</TriggerTypeStyled>
)}
Expand Down