2.0.2 • Published 1 year ago

@smartwithfood/js-sdk v2.0.2

Weekly downloads
14
License
ISC
Repository
-
Last release
1 year ago

SmartWithFood JS SDK

NPM Version

SmartWithFood JS SDK is designed to integrate a widget on your website or SPA

Table of Contents

Installation

npm install @smartwithfood/js-sdk

Using yarn:

yarn add @smartwithfood/js-sdk

Alternatively, you can include it in your project from our CDN. Replace {version} with the desired package version.

<script src="https://unpkg.com/@smartwithfood/js-sdk@{version}/dist/index.min.js"></script>

Usage

ESModules

import Recipe2Basket, { Language } from '@smartwithfood/js-sdk';

Recipe2Basket.initialize({
  token: '<your-widget-token>',
  language: Language.NL,
});

Recipe2Basket.renderButtons({ ... });

Browser

<script src="https://unpkg.com/@smartwithfood/js-sdk@{version}/dist/index.min.js"></script>
<script>
  window.Recipe2Basket.initialize({
    token: '<your-widget-token>',
    language: 'fr',
  });

  Recipe2Basket.renderButtons({ ... });
</script>

API

function Recipe2Basket.initialize(options: InitializeOptions)

Recipe2Basket.initialize({
  token: '<your-widget-token>',
  language: Language.NL,
});

function Recipe2Basket.openModal(options: ModalOptions)

Open the widget modal.

Recipe2Basket.openModal({
  partnerId: '00000000-0000-0000-0000-000000000000',
  recipes: [
    {
      name: 'Recept',
      language: 'nl',
      media: 'https://example.com/image.png',
      ingredients: [
        "1 ei",
        "100 g aardappelen"
      ],
      yield: 4
    }
  ]
});

function Recipe2Basket.closeModal()

Recipe2Basket.closeModal();

function Recipe2Basket.renderButtons(options: ButtonOptions): CleanupFunction

Render a button which opens the widget modal when pressed. Returns a CleanupFunction which will remove the button when called.

Recipe2Basket.renderButtons({
  type: ButtonType.SINGLE_RECIPE,
  selectorOrElement: '.r2b-button-container',
  recipes: [
    {
      name: 'Recept',
      language: 'nl',
      media: 'https://example.com/image.png',
      ingredients: [
        "1 ei",
        "100 g aardappelen"
      ],
      yield: 4
    }
  ],
});

function Recipe2Basket.getBasketUri(options: ModalOptions): String

Returns the URI which can be used in an iframe or redirect to show the widget.

Recipe2Basket.getBasketUri({
  partnerId: '00000000-0000-0000-0000-000000000000',
  recipes: [
    {
      name: 'Recept',
      language: 'nl',
      media: 'https://example.com/image.png',
      ingredients: [
        "1 ei",
        "100 g aardappelen"
      ],
      yield: 4
    }
  ]
});

function Recipe2Basket.on(name: String, callback: Function)

Each event's Data object extends the GenericEventData object.

NameDataDescription
initedWidget interface is loaded and is fetching data
loadedWidget interface is ready
modalClosedWidget is closed
basketCompleted{ basketId: string }User has completed a basket

Sample code to handle events

r2b.on('inited', function() {
  console.log('Recipe2Basket is initialized');
})

function CleanupFunction

A cleanup function which when called will remove the previously made element and modal from the DOM. This is usefull when using frameworks like React. Here is some example code on how this could be used.

import React, { useLayout } from 'react';
import Recipe2Basket from '@smartwithfood/js-sdk';

Recipe2Basket.initialize({ ... });

export function MyComponent() {
  useEffect(() => {
    const buttonCleanup = Recipe2Basket.renderButon({
      selectorOrElement: '#add-recipe-button',
      ...
    });

    return () => {
      buttonCleanup();
    }
  });

  return <div id="add-recipe-button"></div>;
}

type Recipe

A Recipe is a RecipeObject OR a String. When a string is provided we assume this is a uuid from our Recipe Management service.

object InitializeOptions

PropertyDefaultTypeDescription
token*NoneStringYour widget identification token
language*NoneLanguageLanguage of the widget

object RecipeObject

PropertyType
nameString
languageLanguage
mediaString
yieldNumber
ingredientsString

object ModalOptions

PropertyDefaultTypeDescription
recipes*None[Recipe]Array of recipes to add to the shoppinglist
partnerId*NoneStringId of the warehouse we want to productize the recipes in
externalUrlwindow.location.hrefStringThe url where the recipes where selected
warehouseIdNoneStringId of the warehouse we want to productize the recipes in

object ButtonOptions

PropertyDefaultTypeDescription
typesingle_recipe[ButtonType]The type of button to render
selectorOrElementNoneString | ElementWhere the button should render
recipes*None[Recipe]Array of recipes to add to the shoppinglist

enum Language

PropertyValue
NLnl
FRfr
ENen

enum ButtonType

PropertyValue
SINGLE_RECIPEsingle_recipe

object GenericEventData

PropertyType
nameString
idString
2.0.2

1 year ago

2.0.0-alpha.19

1 year ago

2.0.0-alpha.18

1 year ago

2.0.0-alpha.17

1 year ago

2.0.0-alpha.16

1 year ago

2.0.0-alpha.15

1 year ago

2.0.1

1 year ago

2.0.0

1 year ago

2.0.0-alpha.32

1 year ago

2.0.0-alpha.31

1 year ago

2.0.0-alpha.30

1 year ago

2.0.0-alpha.22

1 year ago

2.0.0-alpha.21

1 year ago

2.0.0-alpha.20

1 year ago

2.0.0-alpha.29

1 year ago

2.0.0-alpha.28

1 year ago

2.0.0-alpha.26

1 year ago

2.0.0-alpha.25

1 year ago

2.0.0-alpha.24

1 year ago

2.0.0-alpha.23

1 year ago

2.0.0-alpha.7

2 years ago

2.0.0-alpha.8

2 years ago

2.0.0-alpha.9

2 years ago

2.0.0-alpha.11

1 year ago

2.0.0-alpha.3

2 years ago

2.0.0-alpha.10

1 year ago

2.0.0-alpha.4

2 years ago

2.0.0-alpha.5

2 years ago

2.0.0-alpha.6

2 years ago

2.0.0-alpha.2

2 years ago

2.0.0-alpha.14

1 year ago

2.0.0-alpha.13

1 year ago

2.0.0-alpha.12

1 year ago

1.5.24-beta.1

2 years ago

2.0.0-alpha.0

2 years ago

2.0.0-alpha.1

2 years ago

1.5.24-beta.0

2 years ago

1.5.22-beta.1

2 years ago

1.5.22-beta.0

2 years ago

1.5.23

2 years ago

1.5.22-alpha.3

2 years ago

1.5.22-alpha.4

2 years ago

1.5.21

2 years ago

1.5.20

3 years ago

1.5.19

4 years ago

1.5.18

4 years ago

1.5.14

4 years ago

1.5.16

4 years ago

1.5.15

4 years ago

1.5.17

4 years ago

1.5.12

4 years ago

1.5.11

4 years ago

1.5.13

4 years ago

1.5.10

4 years ago

1.5.9

4 years ago

1.5.8

4 years ago

1.5.7

4 years ago

1.5.6

4 years ago

1.5.5

4 years ago

1.5.4

4 years ago

1.5.3

4 years ago

1.5.2

4 years ago

1.5.1

4 years ago

1.5.0

4 years ago

1.4.0

4 years ago

1.3.0

4 years ago

1.2.0

4 years ago

1.1.0

4 years ago

1.0.0

4 years ago