Skip to content

Instantly share code, notes, and snippets.

@FlorianRappl
Created November 7, 2019 20:57
Show Gist options
  • Save FlorianRappl/55e7c443463ec5c09d49cbd2a3cb521f to your computer and use it in GitHub Desktop.
Save FlorianRappl/55e7c443463ec5c09d49cbd2a3cb521f to your computer and use it in GitHub Desktop.
import * as React from "react";
import { PiletApi } from "app-shell";
import { BuyButton } from "./BuyButton";
import { BasketInfo } from "./BasketInfo";
interface BasketInfoExtension {}
interface BuyButtonExtension {
item: string;
}
export function setup(app: PiletApi) {
const connectBasket = app.createState({
state: {
items: []
},
actions: {
addToCart(dispatch, item: string) {
dispatch(state => ({
...state,
items: [...state.items, item]
}));
}
}
});
app.registerExtension<BuyButtonExtension>(
"buy-button",
connectBasket(({ actions, params }) => (
<BuyButton addToCart={actions.addToCart} item={params.item} />
))
);
app.registerExtension<BasketInfoExtension>(
"basket-info",
connectBasket(({ state }) => <BasketInfo count={state.items.length} />)
);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment