# Module

When developing an application, it is essential to streamline the process by reusing components and queries across multiple applications. This can be achieved through the use of modules, which are a combination of components and queries. These modules can be easily integrated into various applications or other modules, which are referred to as external applications in the context of this discussion.&#x20;

{% hint style="info" %}
Refer to [Your Modules](https://docs.appizap.ae/appizap-dashboard/your-modules) for more information on how to create new modules.
{% endhint %}

By incorporating a module into an application or other modules, it can operate seamlessly as a single component within the overall framework.

<figure><img src="https://125370873-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FzdpN2Lz0hzXpnNScDzVt%2Fuploads%2Fgit-blob-890de16454f06e2c138f2b045955907d67139857%2Fmodule-1.png?alt=media" alt=""><figcaption><p>Build a <a href="#demo-a-statistics-module">statistics module</a></p></figcaption></figure>

<figure><img src="https://125370873-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FzdpN2Lz0hzXpnNScDzVt%2Fuploads%2Fgit-blob-05e9f72fdf4d1f73fd84be95e23000fe44ec2251%2Fmodule-2%20(1).png?alt=media" alt=""><figcaption><p>Reuse this module anywhere</p></figcaption></figure>

## Module basics

### Create a new module

<figure><img src="https://125370873-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FzdpN2Lz0hzXpnNScDzVt%2Fuploads%2FEv6ypARoxOCVwhpAX5yf%2FAddNewModule.gif?alt=media&#x26;token=bf749c5f-f4af-417f-b9a9-4c28e3449381" alt=""><figcaption></figcaption></figure>

### Configure module settings

You can configure a module's settings in the **Settings** tab of data browser:

<figure><img src="https://125370873-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FzdpN2Lz0hzXpnNScDzVt%2Fuploads%2Fe1upGFQF0MvhBX5d7gOM%2FModuleSettings.gif?alt=media&#x26;token=d3a41ea9-b088-4086-ac25-b214d95ff928" alt=""><figcaption></figcaption></figure>

* **Input**: parameters passed into the current module from external apps.

<figure><img src="https://125370873-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FzdpN2Lz0hzXpnNScDzVt%2Fuploads%2F1m6hvpnbewF1cjAfeOrR%2Fimage.png?alt=media&#x26;token=7e2ab70c-185c-49c5-8a14-e62aa8cf1cb1" alt=""><figcaption></figcaption></figure>

* **Output**: data exposed to external apps.

<figure><img src="https://125370873-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FzdpN2Lz0hzXpnNScDzVt%2Fuploads%2FZc5G8jgWsByZqc8iN1UN%2Fimage.png?alt=media&#x26;token=37e57379-aaa5-43df-857d-a877babe44c9" alt=""><figcaption></figcaption></figure>

* **Method**: methods exposed to external apps. For example, you can define a module with a `resetTitle` method, so that external apps can call it to reset the module's title.

<figure><img src="https://125370873-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FzdpN2Lz0hzXpnNScDzVt%2Fuploads%2Fz0jfiKy2EXhZEykOGJvm%2Fimage.png?alt=media&#x26;token=e71bde9f-e957-4085-a17a-304b42f2f81e" alt=""><figcaption></figcaption></figure>

* **Event**: module events can be handled by external apps. For example, you can define an event named `dataChanged`, so that external apps can add event handlers to it once the `dataChanged` event is fired by the module.

<figure><img src="https://125370873-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FzdpN2Lz0hzXpnNScDzVt%2Fuploads%2FmtX5FuAv4DDtYQegXXBe%2Fimage.png?alt=media&#x26;token=ca78cfb3-735c-423e-adb3-208a86dbf9df" alt=""><figcaption></figcaption></figure>

For more details, check out chapter [Module settings](#module-settings).

### Design module UI

The other areas of the module editor are similar to those of the web app editor. Insert components and create queries the same way as your app-building process. In the module editor, you can resize a module by dragging the bottom-right corner to the desirable default size. Toggle the button in the **Properties** panel to control whether the **component height scales with the container**.&#x20;

{% hint style="info" %}
This only works for the fixed-height components within the module.
{% endhint %}

### Insert module to apps or other modules

When editing an app or module, select **Insert > Extensions > Modules** to display the modules that you have access to.

<figure><img src="https://125370873-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FzdpN2Lz0hzXpnNScDzVt%2Fuploads%2FSjUHkcfmYXlHkn0LCaLF%2Fimage.png?alt=media&#x26;token=812fd852-ed33-496d-ac9d-697bdfd45307" alt=""><figcaption></figcaption></figure>

Then drag and drop the module onto the canvas, and set input parameters, change styles inside the module's **Properties** panel.

<figure><img src="https://125370873-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FzdpN2Lz0hzXpnNScDzVt%2Fuploads%2F3tJQ5bWGy6fyX0qs1dhD%2Fimage.png?alt=media&#x26;token=d883b0d5-468e-4056-97dd-dcebcc87348c" alt=""><figcaption></figcaption></figure>

Inspect the data exposed by the module in the data browser.

<figure><img src="https://125370873-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FzdpN2Lz0hzXpnNScDzVt%2Fuploads%2FuLNStkVSknTjDo8T8Mrh%2Fimage.png?alt=media&#x26;token=4359ea0d-f8e0-464f-8798-5b2d90aef2e5" alt=""><figcaption></figcaption></figure>

## Module settings

### Input

Module inputs are parameters passed to the module from external apps. The supported input types are **data**, **string**, **number**, **array**, b**oolean**, and **query**. The first five are data types. Choose **data** to not restrict it to a specific data type. When choosing the type of **query**, you can pass in a query from external apps and trigger it inside the module, then you can reference a module input parameter by its **name**.

<figure><img src="https://125370873-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FzdpN2Lz0hzXpnNScDzVt%2Fuploads%2FJMr5L2ByARYB2See55Ln%2Fimage.png?alt=media&#x26;token=5c264b7a-df45-4515-8274-614f88d42823" alt=""><figcaption></figcaption></figure>

## Demo: A statistics module

This section guides you through the steps to build a statistics module and reuse it in an app.

<figure><img src="https://125370873-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FzdpN2Lz0hzXpnNScDzVt%2Fuploads%2Fgit-blob-05e9f72fdf4d1f73fd84be95e23000fe44ec2251%2Fmodule-2%20(1).png?alt=media" alt=""><figcaption></figcaption></figure>

1. Create module inputs:

   In the **Settings** tab, click **+** **Add** to create a new module input, and then set its **name**, **type**, **default value** (optional) and **tooltip** (optional). In this example, three module inputs are added: two string inputs `title`, `imageUrl`, and a number input `count`:

   <figure><img src="https://125370873-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FzdpN2Lz0hzXpnNScDzVt%2Fuploads%2Fgit-blob-e5a5441d675834f03a5cc30d009483a575cd96c5%2Fmodule-14.png?alt=media" alt=""><figcaption></figcaption></figure>
2. Design the module UI:
   1. Insert a **container** into the module.
   2. Add two **text** components and an **image** component to the container.
   3. Adjust their properties, such as the layout, height, and style.
3. Bind the components' value to the module inputs. For example, the **Image source** of the image is bound to input parameter `imageUrl.value`.

   <figure><img src="https://125370873-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FzdpN2Lz0hzXpnNScDzVt%2Fuploads%2Fgit-blob-c18498c90a9b8df09af57ac886396a1519704991%2Fmodule-15.png?alt=media" alt=""><figcaption></figcaption></figure>
4. Insert the newly built module into an external app or module, and you can pass in values to configure its properties.

   <figure><img src="https://125370873-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FzdpN2Lz0hzXpnNScDzVt%2Fuploads%2Fgit-blob-a08665420a439d40ebb22958fd727930372822bb%2Fmodule-17.png?alt=media" alt=""><figcaption></figcaption></figure>
5. Reuse the module multiple times by passing different input values.

   <figure><img src="https://125370873-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FzdpN2Lz0hzXpnNScDzVt%2Fuploads%2Fgit-blob-05e9f72fdf4d1f73fd84be95e23000fe44ec2251%2Fmodule-2%20(1).png?alt=media" alt=""><figcaption></figcaption></figure>

#### Input Test

To quickly test the effectiveness of input parameters during editing a module, you can use the **Input Test** function. In the **module editor**, select the whole module to conduct tests with simulated input data in the **Properties** panel. In the example previously mentioned, you can set the test values for the three module inputs: `title`, `imageUrl`, and `count`, as shown below:

<figure><img src="https://125370873-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FzdpN2Lz0hzXpnNScDzVt%2Fuploads%2Fgit-blob-1b7d26b75ae123b625817bc735a1db311e2f2829%2Fmodule-18.png?alt=media" alt=""><figcaption></figcaption></figure>

### Output

Module outputs are the data exposed to an app or other modules. You can inspect the outputs of a module in the data browser and access them via JavaScript code in `{{ }}`.The following example shows the outputs in a user-filtering module.

<figure><img src="https://125370873-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FzdpN2Lz0hzXpnNScDzVt%2Fuploads%2Fm02KYcJyG3yYihRmt1q8%2Fimage.png?alt=media&#x26;token=92294ee2-71fb-4acf-a4e6-c211ce5d3531" alt=""><figcaption></figcaption></figure>

Then you can check out and reference the outputs of a module in apps or other modules using it.

```sql
select * from public.users
where phone like {{’%’ + userFilterModule.tel + ‘%’}}
```

### Method

#### Add a new method

In the **Settings** tab, click **+** **Add** to create a new module method. Click the method to rename it and select an action. In the following example, the method `clearAll` is designed to clear the content in both input boxes. To achieve this, select **Run JavaScript** as the action, and write JS code to call the `clearValue` method of both two input components.

<figure><img src="https://125370873-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FzdpN2Lz0hzXpnNScDzVt%2Fuploads%2Fgit-blob-01feb567b75d05fbed971b8923f4d9962cebe48c%2Fmodule-21.png?alt=media" alt=""><figcaption></figcaption></figure>

{% hint style="warning" %}
When creating a method for a module, you cannot create input parameters for the method and can only reference parameters within the scope of the current module.
{% endhint %}

#### Call a method

When using modules, you can call a module method in two ways:

* In event handlers, select **Action** > **Control component**, and then select the **component** and **method**.

  <figure><img src="https://125370873-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FzdpN2Lz0hzXpnNScDzVt%2Fuploads%2Fgit-blob-2103744eff0855a0fa54e9cd6517d5638b651346%2Fmodule-22.png?alt=media" alt=""><figcaption></figcaption></figure>
* Use dot notation in JavaScript queries. For example, `module1.clearAll()` calls the method `clearAll()` of `module1`.

#### Method Test

You can simulate testing method calling in the **Method Test** function.

### Event

Events are used to transfer signals from a module to external apps, such as defining `orderPlaced` `OrderModified` events for an order management module. You can add and fire events within a module, and deal with them using [event-handlers](https://docs.appizap.ae/build-apps/event-handlers "mention") externally.

#### Add a new event

In the **Settings** tab, click **+** **Add** to create a new module event. Click the event to set its name.

<figure><img src="https://125370873-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FzdpN2Lz0hzXpnNScDzVt%2Fuploads%2Fgit-blob-dc001d9c55e738c7650a6922f47f4c31cc848c35%2Fmodule-24.png?alt=media" alt=""><figcaption></figcaption></figure>

#### Trigger an event

In the following example, the event `contentChange` is triggered when the content in any input box changes. You can trigger it in two ways:

* In **Event handlers** of both input components, click **+ Add**, select **Change** as the component event and **Trigger module event** as the action, and finally select the module event `contentChange`.

  <figure><img src="https://125370873-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FzdpN2Lz0hzXpnNScDzVt%2Fuploads%2Fgit-blob-d5c0a117f1c98da8e0ae61bd74299a6fe9f1b1c2%2Fmodule-25.png?alt=media" alt=""><figcaption></figcaption></figure>
* Use dot notation in JavaScript queries: `contentChange.trigger()`.

#### Event handlers

When an app uses a module, you can configure how the current app reacts to the module's events. For example, in **Event handlers** of `module1`, click **+ Add**, and select the module event `contentChange`and the action **Show notification**.

<figure><img src="https://125370873-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FzdpN2Lz0hzXpnNScDzVt%2Fuploads%2FYloIReBIdAiZirps9EJl%2Fimage.png?alt=media&#x26;token=008de74c-815d-4090-b094-c81805d3ecd1" alt=""><figcaption></figcaption></figure>

## Permissions

Users are given access to different modules within applications based on their permissions. As an editor, in order to utilize modules in external applications, you must have at least the viewer role for that specific module. This ensures that users have the appropriate level of access to view and interact with the necessary modules.&#x20;

## Releases and versions

Appizap stores the history versions of your modules for your references. Click **Preview > Publish** at the top right. The release management of a module is identical to that of an app. For detailed information, see [#release-management](https://docs.appizap.ae/build-apps/version-and-release-management#release-management "mention")

{% hint style="info" %}
For an unpublished module, the apps and modules use its latest version; while for a published one, you need to publish it again to sync your latest changes.
{% endhint %}
