npm package: @pongguy/pong-smart-form
Description: We are using `Yarn` in this project.
Author: @pongguy
Latest version: 0.1.5

# Notice

We are using `Yarn` in this project.

This project was bootstrapped with [Create React App](https://github.com/facebook/create-react-app).

## Available Scripts

In the project directory, you can run:

### `yarn start`

Runs the app in the development mode.<br />
Open [http://localhost:3000](http://localhost:3000) to view it in the browser.

The page will reload if you make edits.<br />
You will also see any lint errors in the console.

### `yarn test`

Launches the test runner in the interactive watch mode.<br />
See the section about [running tests](https://facebook.github.io/create-react-app/docs/running-tests) for more information.

### `yarn build`

Builds the app for production to the `build` folder.<br />
It correctly bundles React in production mode and optimizes the build for the best performance.

The build is minified and the filenames include the hashes.<br />
Your app is ready to be deployed!

See the section about [deployment](https://facebook.github.io/create-react-app/docs/deployment) for more information.

### `yarn eject`

**Note: this is a one-way operation. Once you `eject`, you can’t go back!**

If you aren’t satisfied with the build tool and configuration choices, you can `eject` at any time. This command will remove the single build dependency from your project.

Instead, it will copy all the configuration files and the transitive dependencies (webpack, Babel, ESLint, etc) right into your project so you have full control over them. All of the commands except `eject` will still work, but they will point to the copied scripts so you can tweak them. At this point you’re on your own.

You don’t have to ever use `eject`. The curated feature set is suitable for small and middle deployments, and you shouldn’t feel obligated to use this feature. However we understand that this tool wouldn’t be useful if you couldn’t customize it when you are ready for it.

## Learn More

You can learn more in the [Create React App documentation](https://facebook.github.io/create-react-app/docs/getting-started).

To learn React, check out the [React documentation](https://reactjs.org/).

## Usage

### In your React App

```ts
import React, { useEffect } from "react"
import SmartForms, { EventCallbacks } from "smart-form/dist/src/index"
// import sass theme
import "smart-form/scss/theme/pong.scss"
// or import css theme
import "smart-form/dist/theme/pong.css"

const App: React.FC<{}> = () => {
  useEffect(
    () => {
      const smartForm = SmartForms({
        apiKey: "api-key",
        endpoint: "server-endpoint/api",
      })

      const getSupportEvents: EventCallbacks = {
        onCancel: () => {
          // We should have an action in smartform admin
          // with a dataKey = cancel
          // Redirect to home
          window.location.href = "/"
        },
        onComplete: (payload) => {
          if (payload.form?.topic) {
            setRequestTitle(payload.form?.topic)
          }
          setMessageResult("Everything went well.")
        },
        onLoadingStart: () => {
          setLoadingForm(true)
        },
        onLoadingEnd: () => {
          //  Do something when form is ready.
          setLoadingForm(false)
        },
        onConfigurationSetup: (instance) => {
          // instance.setValue(key: string, value: string)
          // instance.submitAction() => to submit the form with an external action.

          // Set default field based on query string param.
          try {
            const search = window.location.search.split("?")
            const queryParams = search[1]?.split("&") || []
            queryParams.forEach((param) => {
              const [dataKey, value] = param.split("=")
              instance.setValue(dataKey, decodeURIComponent(value))
            })
            // dataKey => should match the dataKey configured in smartform admin.
          } catch (error) {
            console.error("Filed to initialize form fields")
          }
        },
      }

      smartForm.init().then((token: string) => {
        smartForm.form({
          // selector is the element #id
          selector: "contact-us",
          id: "81u8u9-q1e2e-qeq2eq2eq2-eq2e2qe2q",
          token,
          events: {
            ...getSupportEvents,
          },
        })
      })
    },
    // eslint-disable-next-line
    []
  )

  return (
    <div>
      ... ...
      <div class="smart-form">
        <div class="smart-form-panel">
          <div id="contact-us"></div>
        </div>
      </div>
    </div>
  )
}
```

## Build the assets:

You can run `yarn run build-rollup` command, which will take all the files, will transpile and export into the dist folder.

Configuration for this can be found at `rollup.config.js` and it's essentially bundling esm module and css under theme folder.

## Publishing the package

To publish the package, use the following command:

```sh
npm publish
```