We also provide the @sidedish/react package that you can use to easily embed your store. To use it, simply run:
Copy
npm install @sidedish/react
The npm package is super lightweight. To use it, simply provide store url and optional sessionId.
Copy
import { StorePage } from '@sidedish/react';const StoreEmbed = () => {const STORE_PAGE_URL = 'YOUR_PAGE_URL'; // Consider saving this in your environment variablesreturn <StorePage url={STORE_PAGE_URL} />;};// And the component where you want to embed:<StoreEmbed />;
If you are creating a safe session to the store then use this code:
Copy
import { StorePage } from "@sidedish/react";// You get the sessionId from somewhere elseconst StoreEmbed = (sessionId:string) => {const STORE_PAGE_URL = 'YOUR_PAGE_URL'; // Consider saving this in your environment variablesreturn <StorePage url={STORE_PAGE_URL} sessionId={sessionId} />};// And the component where you want to embed:<StoreEmbed sessionId={sessionId} />;
To avoid tracking events and analytics made by your internal employees, you can pass true in the internal property of the Store component.
Another way to do this is to use the internal property of passed user object in the unsafeParams or the safe session.
Copy
import { StorePage } from "@sidedish/react"; const STORE_PAGE_URL = 'YOUR_PAGE_URL'; // Consider saving this in your environment variables const StoreEmbed = () => { const isInternalUser = useIsInternalUser(); // your own logic return <StorePage url={STORE_PAGE_URL} internal={isInternalUser} /> }; // And the component where you want to embed: <StoreEmbed />;
We support passing any of the acceptable parameters in an unsafe way if you use them just for display purposes and don’t use them for actual effects anyway.
Passing parameters is relatively straight forward, and depends on your implementation:
In the React component you could include the unsafe params as JSX prop- unsafeParams.
In the iframe method you would simply include the params in the url in a querystring manner but preceding with $ to guarantee uniqueness-
URL?$accountId=SOME_ID&$userId=ANOTHER_ID. This is only required in the iframe method and not in the react method. Please use JSON.stringify() the values.
We support plenty of actions inside a store, including custom callbacks.
1
Set origin validation in admin
Go to app.sidedish.com/ and navigate to the desired store settings page. Set up a origin validation hostname and set that up to your domain.
2
Set a callback for a button somewhere in your store
In the layout of the store, define a “Javascript Callback” and set its identifier to a unique string of your liking, that would be the ACTION_IDENTIFIER, so you could recognize specific events of that kind. Since a store can have multiple callbacks, the id is used to recognize a specific action. You could however use the same id on multiple places in the store to call for the same callback.
3
Create your function
If you used the React method that is even simpler as you could just pass a function callback as the property onCallback:
Copy
import { useRef } from 'react';import { CallbackEvent } from '@sidedish/react';const StoreEmbed = () => { const storeRef = useRef(null); const onCallback = (e: CallbackEvent) => { if (e.actionIdentifier === 'ACTION_IDENTIFIER'){ // Do whatever you want. doSomething(e.payload); // 1) If the callback has caused a change in the unsafe params, you can just update the passed `unsafeParams` and the component // will take care of posting an update event for you. This is the same as doing: // storeRef.current.updateUnsafeParams({}) but easier to just update `unsafeParams` // 2) If there was a change in data that is included in the safe session, you can ask the store to reload the session: if (storeRef.current){ storeRef.current.reloadSession(); } } } const STORE_PAGE_URL = 'YOUR_PAGE_URL'; // Consider saving this in your environment variables return <StorePage url={STORE_PAGE_URL} onCallback={onCallback} ref={storeRef} />;};
If you used the React method that is even simpler as you could just pass a function callback as the property onCallback:
Copy
import { useRef } from 'react';import { CallbackEvent } from '@sidedish/react';const StoreEmbed = () => { const storeRef = useRef(null); const onCallback = (e: CallbackEvent) => { if (e.actionIdentifier === 'ACTION_IDENTIFIER'){ // Do whatever you want. doSomething(e.payload); // 1) If the callback has caused a change in the unsafe params, you can just update the passed `unsafeParams` and the component // will take care of posting an update event for you. This is the same as doing: // storeRef.current.updateUnsafeParams({}) but easier to just update `unsafeParams` // 2) If there was a change in data that is included in the safe session, you can ask the store to reload the session: if (storeRef.current){ storeRef.current.reloadSession(); } } } const STORE_PAGE_URL = 'YOUR_PAGE_URL'; // Consider saving this in your environment variables return <StorePage url={STORE_PAGE_URL} onCallback={onCallback} ref={storeRef} />;};
If you used the iframe method, just listen to message events on your window:
Copy
window.addEventListener('message', (event) => { // You should ensure the message is from the expected origin, as plenty of other sources of messages could occur if (event.origin !== "STORE_PAGE_URL") { //Replace with your store url return; } if (event.data.action==='ACTION_IDENTIFIER'){ // Do whatever you want. doSomething(even.data); // If the callback has caused an effect that demands a change in the unsafe params, don't update the url of the iframe, as that // will cause the store to rerender. To avoid that, use an accepted message: event.source.postMesage({ type: 'UNSAFE_PARAMS_CHANGE', params: unsafeParams }); // If there was a change in data that is included in the safe session, you can ask the store to reload the session: event.source.postMesage({ type: 'SESSION_RELOAD' }); }});
If you used the iframe method, just listen to message events on your window:
Copy
window.addEventListener('message', (event) => { // You should ensure the message is from the expected origin, as plenty of other sources of messages could occur if (event.origin !== "STORE_PAGE_URL") return; //Replace with your store url if (event.data.type==='STORE_PATH_CHANGE'){ const path = event.data.path; const STORE_PAGE_PATH = '/my-marketplace'; window.history.pushState({}, '', `${STORE_PAGE_PATH}${path}`); }});