PromucFlow_constructor/contributions/docs/GlobalFunctions.md
albinAppsmith 629999f124
feat: [epic] appsmith design system version 2 deduplication (#22030)
## Description

### Fixes
- [x] https://github.com/appsmithorg/appsmith/issues/19383
- [x] https://github.com/appsmithorg/appsmith/issues/19384
- [x] https://github.com/appsmithorg/appsmith/issues/19385
- [x] https://github.com/appsmithorg/appsmith/issues/19386
- [x] https://github.com/appsmithorg/appsmith/issues/19387
- [x] https://github.com/appsmithorg/appsmith/issues/19388
- [x] https://github.com/appsmithorg/appsmith/issues/19389
- [x] https://github.com/appsmithorg/appsmith/issues/19390
- [x] https://github.com/appsmithorg/appsmith/issues/19391
- [x] https://github.com/appsmithorg/appsmith/issues/19392
- [x] https://github.com/appsmithorg/appsmith/issues/19393
- [x] https://github.com/appsmithorg/appsmith/issues/19394
- [x] https://github.com/appsmithorg/appsmith/issues/19395
- [x] https://github.com/appsmithorg/appsmith/issues/19396
- [x] https://github.com/appsmithorg/appsmith/issues/19397
- [x] https://github.com/appsmithorg/appsmith/issues/19398
- [x] https://github.com/appsmithorg/appsmith/issues/19399
- [x] https://github.com/appsmithorg/appsmith/issues/19400
- [x] https://github.com/appsmithorg/appsmith/issues/19401
- [x] https://github.com/appsmithorg/appsmith/issues/19402
- [x] https://github.com/appsmithorg/appsmith/issues/19403
- [x] https://github.com/appsmithorg/appsmith/issues/19404
- [x] https://github.com/appsmithorg/appsmith/issues/19405
- [x] https://github.com/appsmithorg/appsmith/issues/19406
- [x] https://github.com/appsmithorg/appsmith/issues/19407
- [x] https://github.com/appsmithorg/appsmith/issues/19408
- [x] https://github.com/appsmithorg/appsmith/issues/19409

Fixes # (issue)
> if no issue exists, please create an issue and ask the maintainers
about this first


Media
> A video or a GIF is preferred. when using Loom, don’t embed because it
looks like it’s a GIF. instead, just link to the video


## Type of change

> Please delete options that are not relevant.

- Bug fix (non-breaking change which fixes an issue)
- New feature (non-breaking change which adds functionality)
- Breaking change (fix or feature that would cause existing
functionality to not work as expected)
- Chore (housekeeping or task changes that don't impact user perception)
- This change requires a documentation update


## How Has This Been Tested?
> Please describe the tests that you ran to verify your changes. Provide
instructions, so we can reproduce.
> Please also list any relevant details for your test configuration.
> Delete anything that is not important

- Manual
- Jest
- Cypress

### Test Plan
> Add Testsmith test cases links that relate to this PR

### Issues raised during DP testing
> Link issues raised during DP testing for better visiblity and tracking
(copy link from comments dropped on this PR)


## Checklist:
### Dev activity
- [ ] My code follows the style guidelines of this project
- [ ] I have performed a self-review of my own code
- [ ] I have commented my code, particularly in hard-to-understand areas
- [ ] I have made corresponding changes to the documentation
- [ ] My changes generate no new warnings
- [ ] I have added tests that prove my fix is effective or that my
feature works
- [ ] New and existing unit tests pass locally with my changes
- [ ] PR is being merged under a feature flag


### QA activity:
- [ ] Test plan has been approved by relevant developers
- [ ] Test plan has been peer reviewed by QA
- [ ] Cypress test cases have been added and approved by either SDET or
manual QA
- [ ] Organized project review call with relevant stakeholders after
Round 1/2 of QA
- [ ] Added Test Plan Approved label after reveiwing all Cypress test

---------

Co-authored-by: Ankita Kinger <ankita@appsmith.com>
Co-authored-by: akash-codemonk <67054171+akash-codemonk@users.noreply.github.com>
Co-authored-by: Tanvi Bhakta <tanvi@appsmith.com>
Co-authored-by: Arsalan <arsalanyaldram0211@outlook.com>
Co-authored-by: Aman Agarwal <aman@appsmith.com>
Co-authored-by: Rohit Agarwal <rohit_agarwal@live.in>
Co-authored-by: Nilesh Sarupriya <nilesh@appsmith.com>
Co-authored-by: Nilesh Sarupriya <20905988+nsarupr@users.noreply.github.com>
Co-authored-by: Tanvi Bhakta <tanvibhakta@gmail.com>
Co-authored-by: Aishwarya UR <aishwarya@appsmith.com>
Co-authored-by: Parthvi Goswami <parthvigoswami@Parthvis-MacBook-Pro.local>
Co-authored-by: Vijetha-Kaja <vijetha@appsmith.com>
Co-authored-by: Parthvi <80334441+Parthvi12@users.noreply.github.com>
Co-authored-by: Apple <nandan@thinkify.io>
Co-authored-by: Saroj <43822041+sarojsarab@users.noreply.github.com>
Co-authored-by: Sangeeth Sivan <74818788+berzerkeer@users.noreply.github.com>
Co-authored-by: Ashok Kumar M <35134347+marks0351@users.noreply.github.com>
Co-authored-by: Aishwarya-U-R <91450662+Aishwarya-U-R@users.noreply.github.com>
Co-authored-by: rahulramesha <rahul@appsmith.com>
Co-authored-by: Aswath K <aswath.sana@gmail.com>
Co-authored-by: Preet Sidhu <preetsidhu.bits@gmail.com>
Co-authored-by: Vijetha-Kaja <119562824+Vijetha-Kaja@users.noreply.github.com>
Co-authored-by: Shrikant Sharat Kandula <shrikant@appsmith.com>
2023-05-20 00:07:06 +05:30

183 lines
7.6 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# Global Function docs
Global functions in Appsmith are available through the right-hand pane and in the JS editor. They allow users to perform different tasks throughout the Appsmith application.
#### Here are some pull requests you can use as an example:
1. Post message API - https://github.com/appsmithorg/appsmith/pull/12551/files
2. SetInterval and ClearInterval API - https://github.com/appsmithorg/appsmith/pull/8158
3. Geolocation API - https://github.com/appsmithorg/appsmith/pull/9295
#### Follow these steps to add a new global function to Appsmith:
1. Go to the [ActionCreator/Fields.tsx](https://github.com/appsmithorg/appsmith/blob/release/app/client/src/components/editorComponents/ActionCreator/Fields.tsx) file. This file contains the parameters and their types that will be the input to the global function. This file also includes the type of field that will accept the value for these parameters, eg., if the input is a dropdown list of items or a text field, etc.,
1. Create a new entry in the *`ActionType` object.* This is the name of the function.
2. Define new fields in the `Fieldtype` object. These are the field names for each argument the function accepts.
3. Update `fieldConfigs` with your fields getter, setting, and view. The getter is the setting used to extract the field value from the function. the setter is used to set the value in function when the field is updated. The view is the component used to edit the field value
4. Update the `renderField` function to change things like field label etc.,
2. Go to the [ActionCreator/index.tsx](https://github.com/appsmithorg/appsmith/blob/release/app/client/src/components/editorComponents/ActionCreator/index.tsx) file.
1. Add the new action entry and its text in the `baseOptions` array (you will need to add a constant for the message in the `constants/messages.ts`) - This will show up as a message when you hover over the function.
![https://paper-attachments.dropbox.com/s_275F1BFE81CAEFA1A98D1AAB74D7D93E3258CDB4EC3F13B6904439E92AA6CF3F_1652893496513_Screenshot+2022-05-18+at+10.34.47+PM.png](https://paper-attachments.dropbox.com/s_275F1BFE81CAEFA1A98D1AAB74D7D93E3258CDB4EC3F13B6904439E92AA6CF3F_1652893496513_Screenshot+2022-05-18+at+10.34.47+PM.png)
3. Attach fields to the new action in the `getFieldFromValue` function (Look at the setInterval code example for guidance). After following these 3 steps, you should be able to view your global function listed on the right hand pane, along with the fields to enter parameters.
```jsx
if (value.indexOf("setInterval") !== -1) {
fields.push(
{
field: FieldType.CALLBACK_FUNCTION_FIELD,
},
{
field: FieldType.DELAY_FIELD,
},
{
field: FieldType.ID_FIELD,
},
);
}
```
4. Go to the [Datatree/actionTriggers.ts](https://github.com/appsmithorg/appsmith/blob/release/app/client/src/entities/DataTree/actionTriggers.ts) file:
1. Add a new entry in the `ActionTriggerType` enum
2. Add a new entry to the `ActionTriggerFunctionNames` datatype (Look at the code example for guidance)
```jsx
[ActionTriggerType.*SET_INTERVAL*]: "setInterval",
```
5. You will also need to add an entry containing the description of the global function, which will contain the type and the payload containing all the parameters and their types
```jsx
export type SetIntervalDescription = {
type: ActionTriggerType.SET_INTERVAL;
payload: {
callback: string;
interval: number;
id?: string;
};
};
```
6. Finally add this global function description to the `ActionDescription` data structure.
7. Go to the [sagas/ActionExecution](https://github.com/appsmithorg/appsmith/tree/b778b83ac45cd0d77421125106a483a4e723f2ca/app/client/src/sagas/ActionExecution) folder:
1. Add a new saga here for your global function. This will contain the logic used to implement your global function. Use this example to implement your global function
```jsx
import {
ClearIntervalDescription,
SetIntervalDescription,
} from "@appsmith/entities/DataTree/actionTriggers";
import {
executeAppAction,
TriggerMeta,
} from "@appsmith/sagas/ActionExecution/ActionExecutionSagas";
import { call, delay, spawn } from "redux-saga/effects";
import { EventType } from "constants/AppsmithActionConstants/ActionConstants";
import {
logActionExecutionError,
TriggerFailureError,
} from "sagas/ActionExecution/errorUtils";
const TIMER_WITHOUT_ID_KEY = "timerWithoutId";
const activeTimers: Record<string, true | string> = {
[TIMER_WITHOUT_ID_KEY]: true,
};
export function* setIntervalSaga(
payload: SetIntervalDescription["payload"],
eventType: EventType,
triggerMeta: TriggerMeta,
) {
if (payload.id) {
activeTimers[payload.id] = payload.callback;
}
yield spawn(executeInIntervals, payload, eventType, triggerMeta);
}
function* executeInIntervals(
payload: SetIntervalDescription["payload"],
eventType: EventType,
triggerMeta: TriggerMeta,
) {
const { callback, id = TIMER_WITHOUT_ID_KEY, interval } = payload;
while (
// only execute if the id exists in the activeTimers obj
id in activeTimers &&
/*
While editing the callback can change for the same id.
At that time we want only execute the new callback
so end the loop if the callback is not the same as the one this
saga was started
But if no id is provided, it will always run
*/
(activeTimers[id] === callback || id === TIMER_WITHOUT_ID_KEY)
) {
// Even if there is an error, the set interval should still keep
// running. This is according to the spec of setInterval
try {
yield call(executeAppAction, {
dynamicString: `{{${callback}}}`,
// pass empty object to execute it as a callback function
callbackData: [{}],
event: { type: eventType },
triggerPropertyName: triggerMeta.triggerPropertyName,
source: triggerMeta.source,
});
} catch (e) {
logActionExecutionError(
e.message,
);
}
yield delay(interval);
}
}
export function* clearIntervalSaga(
payload: ClearIntervalDescription["payload"],
) {
if (!(payload.id in activeTimers)) {
throw new TriggerFailureError(
`Failed to clear interval. No timer active with id "${payload.id}"`,
);
}
delete activeTimers[payload.id];
}
```
8. Add an entry to the [sagas/ActionCreator/ActionExecutionSagas.ts](https://github.com/appsmithorg/appsmith/blob/b778b83ac45cd0d77421125106a483a4e723f2ca/app/client/src/sagas/ActionExecution/ActionExecutionSagas.ts) files `executeActionTriggers` function. Use this example for guidance
```jsx
case ActionTriggerType.SET_INTERVAL:
yield call(setIntervalSaga, trigger.payload, eventType, triggerMeta);
break;
```
9. In the [workers/Actions.ts](https://github.com/appsmithorg/appsmith/blob/b778b83ac45cd0d77421125106a483a4e723f2ca/app/client/src/workers/Actions.ts) file - This file has all the global functions listed in this file. Add the entry for the global function you have created, using this example as a guide:
```jsx
setInterval: function(callback: Function, interval: number, id?: string) {
return {
type: ActionTriggerType.SET_INTERVAL,
payload: {
callback: callback.toString(),
interval,
id,
},
executionType: ExecutionType.TRIGGER,
};
}
```
10. Lastly, add the global functions metadata to the [autocomplete/EntityDefinitions.ts](https://github.com/appsmithorg/appsmith/blob/release/app/client/src/utils/autocomplete/EntityDefinitions.ts) so the data shows up for auto-complete. Use this code sample as guidance:
```jsx
setInterval: {
"!doc": "Execute triggers at a given interval",
"!type": "fn(callback: fn, interval: number, id?: string) -> void",
},
```