Skip to content

Commit

Permalink
Refactor configs for currency formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
Gnito committed Apr 4, 2019
1 parent 03ca53f commit 9770c31
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 16 deletions.
22 changes: 6 additions & 16 deletions src/config.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import * as custom from './marketplace-custom-config.js';
import defaultLocationSearches from './default-location-searches';
import { stripePublishableKey, stripeSupportedCountries } from './stripe-config';
import { currencyConfiguration } from './currency-config';

const env = process.env.REACT_APP_ENV;
const dev = process.env.REACT_APP_ENV === 'development';
Expand Down Expand Up @@ -59,6 +60,10 @@ const sdkTransitVerbose = process.env.REACT_APP_SHARETRIBE_SDK_TRANSIT_VERBOSE =

const currency = process.env.REACT_APP_SHARETRIBE_MARKETPLACE_CURRENCY;

// Currency formatting options.
// See: https://github.com/yahoo/react-intl/wiki/API#formatnumber
const currencyConfig = currencyConfiguration(currency);

// Listing minimum price in currency sub units, e.g. cents.
// 0 means no restriction to the price
const listingMinimumPriceSubUnits = 0;
Expand All @@ -69,21 +74,6 @@ const sentryDsn = process.env.REACT_APP_SENTRY_DSN;
// If webapp is using SSL (i.e. it's behind 'https' protocol)
const usingSSL = process.env.REACT_APP_SHARETRIBE_USING_SSL === 'true';

// Currency formatting options.
// See: https://github.com/yahoo/react-intl/wiki/API#formatnumber
//
// TODO: Remove this and hide formating within the util/currency module
const currencyConfig = {
style: 'currency',
currency,
currencyDisplay: 'symbol',
useGrouping: true,
// Note: you should change fraction digits to 0,
// if the currency is not using subunits (like JPY).
minimumFractionDigits: 2,
maximumFractionDigits: 2,
};

// Address information is used in SEO schema for Organization (http://schema.org/PostalAddress)
const addressCountry = 'FI';
const addressRegion = 'Helsinki';
Expand Down Expand Up @@ -205,8 +195,8 @@ const config = {
},
sortSearchByDistance,
currency,
listingMinimumPriceSubUnits,
currencyConfig,
listingMinimumPriceSubUnits,
stripe: {
publishableKey: stripePublishableKey,
supportedCountries: stripeSupportedCountries,
Expand Down
27 changes: 27 additions & 0 deletions src/currency-config.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,30 @@ export const subUnitDivisors = {
SGD: 100,
USD: 100,
};

// Currency formatting options.
// See: https://github.com/yahoo/react-intl/wiki/API#formatnumber
export const currencyConfiguration = currency => {
if (!subUnitDivisors[currency]) {
throw new Error(`Configuration missing for currency: ${currency}.`);
}

return subUnitDivisors[currency] === 1
? {
style: 'currency',
currency,
currencyDisplay: 'symbol',
useGrouping: true,
// If the currency is not using subunits (like JPY), remove fractions.
minimumFractionDigits: 0,
maximumFractionDigits: 0,
}
: {
style: 'currency',
currency,
currencyDisplay: 'symbol',
useGrouping: true,
minimumFractionDigits: 2,
maximumFractionDigits: 2,
};
};

0 comments on commit 9770c31

Please sign in to comment.