12.0.38 • Published 8 months ago

@heymantle/polaris v12.0.38

Weekly downloads
-
License
MIT
Repository
github
Last release
8 months ago

Mantle App API React Client and Components

A react interface for interacting with the Mantle App API and drop-in components for your billing UI.

Installation

You can install the Mantle react package using npm:

$ npm install @heymantle/polaris

Usage

Include the MantleProvider react context somewhere near the root of your component tree, after you have access to your customer / shop data, you'll need the stored Mantle customer token from the identify request you did server-side. For example:

import { MantleProvider } from "@heymantle/react";

const App = (Component) => {
  const [shop, setShop] = useState();

  const fetchShop = async () => {
    const response = await fetch('/api/shop');
    setShop(await response.json());
  }

  useEffect(() => {
    fetchShop();
  }, [])

  if (!shop) {
    return <Loading />;
  }

  ReactDOM.render(
    <BrowserRouter>
      <AppFrame host={host}>
        <MantleProvider
          appId={process.env.MANTLE_APP_ID}
          customerApiToken={shop.mantleApiToken}
          apiUrl={process.env.MANTLE_API_URL}
        >
          <Component />
        </MantleProvider>
      </AppFrame>
    </BrowserRouter>,
    document.getElementById("app-container")
  );
};

Furthur down the stack you can then use the useMantle hook for most data and operations, for example:

import { useMantle } from "@heymantle/react";
import { HorizontalPlanCards } from "@heymantle/polaris";

const HomePage = () => {
  const { customer, subscription, plans, subscribe, cancelSubscription, pushEvent } = useMantle();

  useEffect(() => {
    pushEvent({
      eventName: 'page_view',
      properties: {
        path: window.location.href,
      },
    });
  }, [window.location]);

  return (
    <div>
      {customer.subscription ? (
        <div>
          <span>You're currently susbcribed to: {customer.subscription.plan.name}</span>
          <button
            onclick={async () => {
              await cancelSubscription();
            }}
          >
            Cancel subscription
          </button>
        </div>
      ) : (
        <HorizontalPlanCards
          customer={customer}
          plans={plans}
          onSubscribe={async ({ planId, discountId }) => {
            const subscription = await subscribe({ planId, discountId });
            open(subscription.confirmationUrl, "_top");
          }}
        />
      )}
    </div>
  )
};

Documentation

Documentation is available at https://heymantle.com/docs/surfacing-mantle-data.

12.0.38

8 months ago

12.0.37

9 months ago

12.0.36

9 months ago

12.0.28

12 months ago

12.0.27

12 months ago

12.0.29

11 months ago

12.0.24

12 months ago

12.0.23

12 months ago

12.0.26

12 months ago

12.0.25

12 months ago

12.0.20

12 months ago

12.0.22

12 months ago

12.0.21

12 months ago

12.0.17

12 months ago

12.0.16

12 months ago

12.0.19

12 months ago

12.0.18

12 months ago

12.0.35

9 months ago

12.0.13

1 year ago

12.0.34

10 months ago

12.0.12

1 year ago

12.0.15

1 year ago

12.0.14

1 year ago

12.0.31

10 months ago

12.0.30

11 months ago

12.0.33

10 months ago

12.0.32

10 months ago

12.0.11

1 year ago

12.0.7

1 year ago

12.0.8

1 year ago

12.0.9

1 year ago

12.0.10

1 year ago

12.0.4

1 year ago

12.0.5

1 year ago

12.0.6

1 year ago

12.0.3

1 year ago

12.0.2

1 year ago

12.0.1

1 year ago

12.0.0

1 year ago