# tapcart-codeblocks-sdk-test

> Tapcart Web Bridge enables web pages to render inside of Tapcart apps and synchronize state between the web and native layers, invoke native functionalities, and react to real-time updates from the native app.

Latest version **1.3.4** (published 2023-08-01) · MIT license · 0 weekly downloads

## Install

```sh
npm install tapcart-codeblocks-sdk-test
pnpm add tapcart-codeblocks-sdk-test
yarn add tapcart-codeblocks-sdk-test
bun add tapcart-codeblocks-sdk-test
```

## Health

**Score 20/100 (F)** — status: abandoned.

Positive: esm support; no vulnerabilities.

Warnings: low downloads; no types.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.3.4 |
| Published | 2023-08-01 |
| First published | 2023-06-12 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | ESM |
| Dependencies | 5 |
| Unpacked size | 154.5 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Maintainers | chinapalace |

## Links

- npm: https://www.npmjs.com/package/tapcart-codeblocks-sdk-test
- npm.io page: https://npm.io/package/tapcart-codeblocks-sdk-test

## Dependencies (5)

- [yup](https://npm.io/package/yup.md) ^0.32.11
- [react](https://npm.io/package/react.md) ^18.2.0
- [nanoid](https://npm.io/package/nanoid.md) ^3.3.2
- [react-dom](https://npm.io/package/react-dom.md) ^18.2.0
- [handlebars](https://npm.io/package/handlebars.md) ^4.7.7

## Recent versions

- 1.3.4 (latest) — 2023-08-01
- 1.3.3 — 2023-08-01
- 1.3.2 — 2023-08-01
- 1.3.1 — 2023-08-01
- 1.3.0 — 2023-08-01
- 1.2.9 — 2023-06-21
- 1.2.8 — 2023-06-15
- 1.2.7 — 2023-06-15
- 1.2.6 — 2023-06-15
- 1.2.5 — 2023-06-15
- 1.2.4 — 2023-06-15
- 1.2.3 — 2023-06-12
- 1.2.2 — 2023-06-12
- 1.2.1 — 2023-06-12
- 1.2.0 — 2023-06-12

## README

# Tapcart Codeblocks SDK

Tapcart Web Bridge enables web pages to render inside of Tapcart apps and synchronize state between the web and native layers, invoke native functionalities, and react to real-time updates from the native app.

## Installation

All dependencies are already added into `package.json`. Install them using yarn.

```
yarn install
```

## Building

Webpack and Babel are used to build both the vanilla and react versions of the SDK. Run the following command to build after editing `src`.

```
yarn build
```

The final build files are put in the `dist` folder.

## Examples

All sample projects which use the SDK are located in the `examples` folder. These can be used for testing the SDK locally during development.

### Vanilla

A sample server has been setup to serve the vanillaJS SDK. Once the SDK source files have been edited, run the following commands from root of the project.

```
yarn build
yarn serve-local
```

The server is setup with hot reloading so any updates to the SDK will be reflected automatically after running `yarn build`.

#### Example Usage

```
<!-- index.html -->

<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="utf-8" />
        <meta name="viewport" content="width=device-width, initial-scale=1" />
        <title>SDK Test HTML</title>
        <script type="text/javascript" defer src="http://localhost:3000/tapcart.js"></script>
        <script type="text/javascript" defer src="script.js"></script>
    </head>
    <body id="body" style="height: 100vh;">
        <div id="tapcart-root"></div>
    </body>
</html>
```

```
// script.js

(function () {
	let merchantFunc = Function(`Tapcart.registerEventHandler("cart/updated",function(eventName,eventData){ console.log("Event Handler:", eventName  ,eventData); })`);
	Tapcart.registerMerchantJavascript(merchantFunc);
})()

// Test script - Simulates what app can/needs to do after loading codeblock/webview
setTimeout(function () {
	Tapcart.init({ variables: { firstName: "Gopi" } });
	Tapcart.triggerEvent("cart/updated",{"total": "10.00",currency: "USD"})
},3000);
```

### React

To test the react SDK, `yalc` is used to facilitate local npm package linking.

**1.)** First, install `yalc` globally

```
npm i -g yalc
```

**2.)** Build/Publish to local `yalc` repo. Run following from project root

```
yarn build
yarn publish-local
```

**3.)** Run example react app from `/examples/react/react-app` directory

```
yarn codeblocks/add
yarn install
yarn start
```

**4.)** To reflect changes made to SDK in the example React App

- Build & Publish from project root

```
yarn build
yarn publish-local
```

- Update tapcart-codeblocks-react package in `/examples/react/react-app` directory

```
yarn codeblocks/update
```

#### Example Usage

```
// App.js

import {TapcartProvider,createClient} from "tapcart-codeblocks-react";
import InnerComponent from "./InnerComponent";

function App() {

  const tapcartClient = createClient({test: false});

  return (
    <div className="App">
      <TapcartProvider client={tapcartClient}>
         <InnerComponent/>
         ...
      </TapcartProvider>
    </div>

  );
}

export default App;

```

```
// InnerComponent.js

import {useActions} from "tapcart-codeblocks-react";

function InnerComponent(props) {
    let {openProduct,openCollection,openCart} = useActions();
    return <button onClick={() => openProduct("1232242232132")}>Inner Component</button>
}

export default InnerComponent;
```

## Docs

The SDK acts as a communication bridge between the native iOS/Android Webviews and the Web Apps built by the merchants. This two way communication is powered by `events` and `actions`.

Actions allow the SDK users to interact with native app functionality via JS. The following actions are supported currently:

`Tapcart.addToCart`
`Tapcart.openProduct`
`Tapcart.openCollection`
`Tapcart.openCart`
`Tapcart.applyDiscount`
`Tapcart.applyGiftCard`

Events allow SDK users to respond to changes in the native apps; The following events are supported currently:

`cart/updated`
`discount/applied`
`giftcard/applied`
`product/opened`
`collection/opened`

---
_Source: https://npm.io/package/tapcart-codeblocks-sdk-test · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
