Skip to content

Commit

Permalink
State: Use getHttpData() for geolocation data
Browse files Browse the repository at this point in the history
In #22549 we added the `getHttpData()` abstraction
for simple network data needs.

In this patch we're moving the `geo` state subtree over to this
new abstraction and removing the old code it previously required.

This approach is build on the data-layer ideas and extends it
to store data outside of Redux.
  • Loading branch information
dmsnell committed May 6, 2018
1 parent 03204ab commit cf06bcb
Show file tree
Hide file tree
Showing 16 changed files with 50 additions and 581 deletions.
18 changes: 0 additions & 18 deletions client/components/data/query-geo/README.md

This file was deleted.

36 changes: 0 additions & 36 deletions client/components/data/query-geo/index.jsx

This file was deleted.

10 changes: 4 additions & 6 deletions client/components/language-picker/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,9 @@ import { find, isString, noop } from 'lodash';
/**
* Internal dependencies
*/
import { getGeoCountryShort } from 'state/geo/selectors';
import QueryGeo from 'components/data/query-geo';
import LanguagePickerModal from './modal';
import QueryLanguageNames from 'components/data/query-language-names';
import { requestGeoLocation } from 'state/data-layer/http-data/getters';
import { getLanguageCodeLabels } from './utils';

export class LanguagePicker extends PureComponent {
Expand Down Expand Up @@ -174,13 +173,12 @@ export class LanguagePicker extends PureComponent {
</div>
</div>
{ this.renderModal( language.langSlug ) }
<QueryGeo />
<QueryLanguageNames />
</div>
);
}
}

export default connect( state => ( {
countryCode: getGeoCountryShort( state ),
} ) )( localize( LanguagePicker ) );
export default connect( () => ( { countryCode: requestGeoLocation().data } ) )(
localize( LanguagePicker )
);
4 changes: 2 additions & 2 deletions client/lib/abtest/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ Also, the user's variation is saved in local storage. You can see this in Chrome

Here's another example with country targeting:
```jsx
const userCountryCode = getGeoCountryShort( state );
const userCountryCode = requestGeoLocation().data;
let buttonWording;

if ( abtest( 'freeTrialButtonWordingForIndia', userCountryCode ) === 'startFreeTrial' ) {
Expand Down Expand Up @@ -161,7 +161,7 @@ You would need to update [config/default.json](https://github.com/Automattic/wp-
"knownABTestKeys": [
"freeTrialButtonWording"
]

"overrideABTests": [
[ "freeTrialButtonWording_201502160", "startFreeTrial" ]
]
Expand Down
2 changes: 0 additions & 2 deletions client/my-sites/checkout/checkout/checkout.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ import { managePurchase } from 'me/purchases/paths';
import SubscriptionLengthPicker from 'blocks/subscription-length-picker';
import QueryContactDetailsCache from 'components/data/query-contact-details-cache';
import QueryStoredCards from 'components/data/query-stored-cards';
import QueryGeo from 'components/data/query-geo';
import QuerySitePlans from 'components/data/query-site-plans';
import QueryPlans from 'components/data/query-plans';
import SecurePaymentForm from './secure-payment-form';
Expand Down Expand Up @@ -610,7 +609,6 @@ export class Checkout extends React.Component {
<QueryProducts />
<QueryContactDetailsCache />
<QueryStoredCards />
<QueryGeo />

{ this.content() }
</div>
Expand Down
38 changes: 38 additions & 0 deletions client/state/data-layer/http-data/getters.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/** @format */
/**
* Internal dependencies
*/
import makeJsonSchemaParser from 'lib/make-json-schema-parser';
import { http as rawHttp } from 'state/http/actions';
import { requestHttpData } from 'state/data-layer/http-data/common';

export const requestGeoLocation = () =>
requestHttpData(
'geo',
rawHttp( {
method: 'GET',
url: 'https://public-api.wordpress.com/geo/',
} ),
{
fromApi: makeJsonSchemaParser(
{
type: 'object',
properties: {
body: {
type: [ 'object', 'null' ],
properties: {
latitude: { type: 'string' },
longitude: { type: 'string' },
country_short: { type: 'string' },
country_long: { type: 'string' },
region: { type: 'string' },
city: { type: 'string' },
},
},
},
},
// we only use the short code currently
( { body: { country_short } } ) => [ [ 'geo', country_short ] ]
),
}
);
6 changes: 4 additions & 2 deletions client/state/data-layer/http-data/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -105,8 +105,10 @@ const parseResponse = ( data, fromApi ) => {
};

const onSuccess = ( action, apiData ) => {
const fromApi = 'function' === typeof action.fromApi && action.fromApi();
const [ error, data ] = fromApi ? parseResponse( apiData, fromApi ) : [ undefined, [] ];
const [ error, data ] =
'function' === typeof action.fromApi
? parseResponse( apiData, action.fromApi )
: [ undefined, [] ];

if ( undefined !== error ) {
return onError( action, error );
Expand Down
57 changes: 0 additions & 57 deletions client/state/geo/actions.js

This file was deleted.

49 changes: 0 additions & 49 deletions client/state/geo/reducer.js

This file was deleted.

12 changes: 0 additions & 12 deletions client/state/geo/schema.js

This file was deleted.

47 changes: 0 additions & 47 deletions client/state/geo/selectors.js

This file was deleted.

Loading

0 comments on commit cf06bcb

Please sign in to comment.