All files / app/checkout CheckoutProvider.tsx

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61  79x 79x   79x                   79x         476x 479x             476x   476x         470x 470x   470x 530x       79x 7x 7x 7x       79x 1011x 1011x   1011x           79x  
import { CheckoutSelectors, CheckoutService } from '@bigcommerce/checkout-sdk';
import { memoizeOne } from '@bigcommerce/memoize';
import React, { Component, ReactNode } from 'react';
 
import CheckoutContext from './CheckoutContext';
 
export interface CheckoutProviderProps {
    checkoutService: CheckoutService;
}
 
export interface CheckoutProviderState {
    checkoutState: CheckoutSelectors;
}
 
export default class CheckoutProvider extends Component<CheckoutProviderProps, CheckoutProviderState> {
    state: Readonly<CheckoutProviderState>;
 
    private unsubscribe?: () => void;
 
    private getContextValue = memoizeOne((checkoutService, checkoutState) => {
        return {
            checkoutService,
            checkoutState,
        };
    });
 
    constructor(props: Readonly<CheckoutProviderProps>) {
        super(props);
 
        this.state = {
            checkoutState: props.checkoutService.getState(),
        };
    }
 
    componentDidMount(): void {
        const { checkoutService } = this.props;
 
        this.unsubscribe = checkoutService.subscribe(checkoutState =>
            this.setState({ checkoutState })
        );
    }
 
    componentWillUnmount(): void {
        Eif (this.unsubscribe) {
            this.unsubscribe();
            this.unsubscribe = undefined;
        }
    }
 
    render(): ReactNode {
        const { checkoutService, children } = this.props;
        const { checkoutState } = this.state;
 
        return (
            <CheckoutContext.Provider value={ this.getContextValue(checkoutService, checkoutState) }>
                { children }
            </CheckoutContext.Provider>
        );
    }
}