import { Automat, fetchJson } from 'react-automat'; import { article } from '../api/index'; import { requireObject, requireDefined } from '../utils/apiShape'; /** * Article automat store. * Registered as window.artikel via the name option. * State shape: { [sku: string]: article | null } * * Usage in PureComponent: * ```jsx * import articleAutomat, { createArticleSelector } from '../../store/articleAutomat'; * * class ArticleView extends PureComponent { * constructor(props) { * super(props); * const sku = props.sku; * const currentArticle = articleAutomat.state[sku] ?? null; * this.state = { article: currentArticle }; * * if (!currentArticle) { * articleAutomat.actions.loadArticle(sku).catch(console.error); * } * } * * componentDidMount() { * this.subscribeToArticle(); * } * * componentDidUpdate(prevProps) { * if (prevProps.sku !== this.props.sku) { * const currentArticle = articleAutomat.state[this.props.sku] ?? null; * this.setState({ article: currentArticle }); * * if (!currentArticle) { * articleAutomat.actions.loadArticle(this.props.sku).catch(console.error); * } * * this.subscribeToArticle(); * } * } * * subscribeToArticle() { * if (this.unsub) this.unsub(); * const selector = createArticleSelector(this.props.sku); * this.unsub = articleAutomat.subscribe(this, (state) => ({ * article: selector(state), * })); * } * * componentWillUnmount() { * if (this.unsub) { * this.unsub(); * this.unsub = null; * } * } * * render() { * const { article } = this.state; * if (!article) return null; * return