Skip to content

Instantly share code, notes, and snippets.

@silesky
Created July 21, 2017 19:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save silesky/bfd1cfca8f21c125a8e1d27063a472d4 to your computer and use it in GitHub Desktop.
Save silesky/bfd1cfca8f21c125a8e1d27063a472d4 to your computer and use it in GitHub Desktop.
/* eslint camelcase: 0 */
import React from 'react';
import compose from 'recompose/compose';
import lifecycle from 'recompose/lifecycle';
import {
getCountryNameByCode,
getCountryCodeByName,
validCountry,
} from 'utils';
import { injectMaxMindScript, getGeoIpCountry } from 'geoip';
import 'whatwg-fetch';
export const getOtherDonateUrl = lang => {
let url = 'https://my.rotary.org/donate';
switch (lang) {
case 'en':
url = 'https://my.rotary.org/en/check-draft-or-wire-payments';
break;
case 'de':
url = 'https://my.rotary.org/de/check-draft-or-wire-payments';
break;
case 'fr':
url = 'https://my.rotary.org/fr/check-draft-or-wire-payments';
break;
case 'it':
url = 'https://my.rotary.org/it/check-draft-or-wire-payments';
break;
case 'es':
url = 'https://my.rotary.org/es/check-draft-or-wire-payments';
break;
default:
}
return url;
};
export const getCountrySpecificNote = country => {
switch (country) {
case 'in':
return (
<span
dangerouslySetInnerHTML={{
__html: Drupal.t(
'If you wish to donate in Rupees, <a href="@url">go here</a>.',
{ '@url': 'https://my.rotary.org/exit-redirect/give-pp' },
),
}}
/>
);
default:
return null;
}
};
const withLoadUserData = lifecycle({
componentDidMount() {
const {
config,
setUser,
setUserData,
setCountry,
setCountryOverridable,
countryOverridable,
} = this.props;
(async () => {
// Attempt to determine users country via IP address
const scriptRes = await injectMaxMindScript();
const userCountryData = await getGeoIpCountry(scriptRes);
const setCountryFromDataRes = ({ country: { iso_code } }) => {
if (!iso_code) throw Error('userCountry data not found');
const countryName = getCountryNameByCode(iso_code);
if (countryOverridable && countryName) {
setCountry(countryName);
}
};
setCountryFromDataRes(userCountryData);
try {
const userRes = await global.fetch(`${config.api_url}/en/widgets/donate/user-info`, {
credentials: 'include',
});
if (!userRes.ok) throw userRes;
const user = await userRes.json();
// Set the default country based on the users primary address, if it's
// a valid country, and the user hasn't already picked another country
if (countryOverridable && user.address && validCountry(getCountryCodeByName(user.address.country))) {
// The users address has a high degree of certainty, disable any
// further automatic setting of it
setCountryOverridable(false);
setCountry(user.address.country);
}
setUser(user);
setUserData(user);
} catch (err) {
// Intentionally do nothing on request failure.
}
})();
},
});
export default compose(withLoadUserData);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment