All files / app/locale LocaleProvider.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 62 63 64146x 146x 146x   146x 146x                   146x 71x   71x     71x   130x                 146x 70x   70x 170x 170x           146x             146x 198x 198x   198x           146x   146x  
import { createCurrencyService, CheckoutService, StoreConfig } from '@bigcommerce/checkout-sdk';
import { memoizeOne } from '@bigcommerce/memoize';
import React, { Component, ReactNode } from 'react';
 
import getLanguageService from './getLanguageService';
import LocaleContext from './LocaleContext';
 
export interface LocaleProviderProps {
    checkoutService: CheckoutService;
}
 
export interface LocaleProviderState {
    config?: StoreConfig;
}
 
class LocaleProvider extends Component<LocaleProviderProps> {
    state: Readonly<LocaleProviderState> = {};
 
    private languageService = getLanguageService();
    private unsubscribe?: () => void;
 
    private getContextValue = memoizeOne((config?: StoreConfig) => {
 
        return {
            currency: config ? createCurrencyService(config) : undefined,
            date: config ? {
                inputFormat: config.inputDateFormat,
            } : undefined,
            language: this.languageService,
        };
    });
 
    componentDidMount(): void {
        const { checkoutService } = this.props;
 
        this.unsubscribe = checkoutService.subscribe(
            ({ data }) => {
                this.setState({ config: data.getConfig() });
            },
            ({ data }) => data.getConfig()
        );
    }
 
    componentWillUnmount(): void {
        if (this.unsubscribe) {
            this.unsubscribe();
            this.unsubscribe = undefined;
        }
    }
 
    render(): ReactNode {
        const { children } = this.props;
        const { config } = this.state;
 
        return (
            <LocaleContext.Provider value={ this.getContextValue(config) }>
                { children }
            </LocaleContext.Provider>
        );
    }
}
 
export default LocaleProvider;