forked from Automattic/wp-calypso
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add actions, reducers & selectors to get available shipping methods f…
…rom merchant host endpoint (Automattic#14610)
- Loading branch information
Showing
8 changed files
with
265 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
42 changes: 42 additions & 0 deletions
42
client/extensions/woocommerce/state/sites/shipping-methods/actions.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
/** | ||
* Internal dependencies | ||
*/ | ||
import request from '../request'; | ||
import { setError } from 'woocommerce/state/sites/status/wc-api/actions'; | ||
import { | ||
WOOCOMMERCE_SHIPPING_METHODS_REQUEST, | ||
WOOCOMMERCE_SHIPPING_METHODS_REQUEST_SUCCESS, | ||
} from 'woocommerce/state/action-types'; | ||
import { | ||
areShippingMethodsLoaded, | ||
areShippingMethodsLoading, | ||
} from './selectors'; | ||
|
||
export const fetchShippingMethodsSuccess = ( siteId, data ) => { | ||
return { | ||
type: WOOCOMMERCE_SHIPPING_METHODS_REQUEST_SUCCESS, | ||
siteId, | ||
data, | ||
}; | ||
}; | ||
|
||
export const fetchShippingMethods = ( siteId ) => ( dispatch, getState ) => { | ||
if ( areShippingMethodsLoaded( getState(), siteId ) || areShippingMethodsLoading( getState(), siteId ) ) { | ||
return; | ||
} | ||
|
||
const getAction = { | ||
type: WOOCOMMERCE_SHIPPING_METHODS_REQUEST, | ||
siteId, | ||
}; | ||
|
||
dispatch( getAction ); | ||
|
||
return request( siteId ).get( 'shipping_methods' ) | ||
.then( ( data ) => { | ||
dispatch( fetchShippingMethodsSuccess( siteId, data ) ); | ||
} ) | ||
.catch( err => { | ||
dispatch( setError( siteId, getAction, err ) ); | ||
} ); | ||
}; |
21 changes: 21 additions & 0 deletions
21
client/extensions/woocommerce/state/sites/shipping-methods/reducer.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
/** | ||
* Internal dependencies | ||
*/ | ||
import { createReducer } from 'state/utils'; | ||
import { | ||
WOOCOMMERCE_SHIPPING_METHODS_REQUEST, | ||
WOOCOMMERCE_SHIPPING_METHODS_REQUEST_SUCCESS, | ||
} from 'woocommerce/state/action-types'; | ||
import { LOADING } from 'woocommerce/state/constants'; | ||
|
||
// TODO: Handle error | ||
|
||
export default createReducer( null, { | ||
[ WOOCOMMERCE_SHIPPING_METHODS_REQUEST ]: () => { | ||
return LOADING; | ||
}, | ||
|
||
[ WOOCOMMERCE_SHIPPING_METHODS_REQUEST_SUCCESS ]: ( state, { data } ) => { | ||
return data; | ||
}, | ||
} ); |
38 changes: 38 additions & 0 deletions
38
client/extensions/woocommerce/state/sites/shipping-methods/selectors.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
/** | ||
* External dependencies | ||
*/ | ||
import { get, isArray } from 'lodash'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import { getSelectedSiteId } from 'state/ui/selectors'; | ||
import { LOADING } from 'woocommerce/state/constants'; | ||
|
||
/** | ||
* @param {Object} state Whole Redux state tree | ||
* @param {Number} [siteId] Site ID to check. If not provided, the Site ID selected in the UI will be used | ||
* @return {Object} The list of shipping methods, as retrieved from the server. It can also be the string "LOADING" | ||
* if the methods are currently being fetched, or a "falsy" value if that haven't been fetched at all. | ||
*/ | ||
export const getShippingMethods = ( state, siteId = getSelectedSiteId( state ) ) => { | ||
return get( state, [ 'extensions', 'woocommerce', 'sites', siteId, 'shippingMethods' ] ); | ||
}; | ||
|
||
/** | ||
* @param {Object} state Whole Redux state tree | ||
* @param {Number} [siteId] Site ID to check. If not provided, the Site ID selected in the UI will be used | ||
* @return {boolean} Whether the shipping methods list has been successfully loaded from the server | ||
*/ | ||
export const areShippingMethodsLoaded = ( state, siteId = getSelectedSiteId( state ) ) => { | ||
return isArray( getShippingMethods( state, siteId ) ); | ||
}; | ||
|
||
/** | ||
* @param {Object} state Whole Redux state tree | ||
* @param {Number} [siteId] Site ID to check. If not provided, the Site ID selected in the UI will be used | ||
* @return {boolean} Whether the shipping methods list is currently being retrieved from the server | ||
*/ | ||
export const areShippingMethodsLoading = ( state, siteId = getSelectedSiteId( state ) ) => { | ||
return LOADING === getShippingMethods( state, siteId ); | ||
}; |
40 changes: 40 additions & 0 deletions
40
client/extensions/woocommerce/state/sites/shipping-methods/test/reducer.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
/** | ||
* External dependencies | ||
*/ | ||
import { expect } from 'chai'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import reducer from '../../reducer'; | ||
import { LOADING } from 'woocommerce/state/constants'; | ||
import { | ||
WOOCOMMERCE_SHIPPING_METHODS_REQUEST, | ||
} from 'woocommerce/state/action-types'; | ||
import { fetchShippingMethodsSuccess } from '../actions'; | ||
|
||
describe( 'fetch shipping methods', () => { | ||
it( 'should mark the shipping methods tree as "loading"', () => { | ||
const siteId = 123; | ||
const state = {}; | ||
|
||
const newSiteData = reducer( state, { type: WOOCOMMERCE_SHIPPING_METHODS_REQUEST, siteId } ); | ||
expect( newSiteData[ siteId ].shippingMethods ).to.eql( LOADING ); | ||
} ); | ||
} ); | ||
|
||
describe( 'fetch shipping methods - success', () => { | ||
it( 'should store data from the action', () => { | ||
const siteId = 123; | ||
const state = {}; | ||
|
||
const methods = [ | ||
{ id: 'free_shipping', title: 'Free Shipping' }, | ||
{ id: 'local_pickup', title: 'Local Pickup' }, | ||
]; | ||
const newState = reducer( state, fetchShippingMethodsSuccess( siteId, methods ) ); | ||
expect( newState[ siteId ] ).to.exist; | ||
expect( newState[ siteId ].shippingMethods ).to.deep.equal( methods ); | ||
} ); | ||
} ); | ||
|
119 changes: 119 additions & 0 deletions
119
client/extensions/woocommerce/state/sites/shipping-methods/test/selectors.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,119 @@ | ||
/** | ||
* External dependencies | ||
*/ | ||
import { expect } from 'chai'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import { getShippingMethods, areShippingMethodsLoaded, areShippingMethodsLoading } from '../selectors'; | ||
import { LOADING } from 'woocommerce/state/constants'; | ||
|
||
describe( 'selectors', () => { | ||
describe( 'shipping methods loading state', () => { | ||
it( 'when woocommerce state is not available.', () => { | ||
const state = { | ||
extensions: { | ||
woocommerce: {}, | ||
}, | ||
}; | ||
|
||
expect( getShippingMethods( state, 123 ) ).to.be.falsey; | ||
expect( areShippingMethodsLoaded( state, 123 ) ).to.be.false; | ||
expect( areShippingMethodsLoading( state, 123 ) ).to.be.false; | ||
} ); | ||
|
||
it( 'when methods are loaded.', () => { | ||
const state = { | ||
extensions: { | ||
woocommerce: { | ||
sites: { | ||
123: { | ||
shippingMethods: [], | ||
}, | ||
}, | ||
}, | ||
}, | ||
}; | ||
|
||
expect( getShippingMethods( state, 123 ) ).to.deep.equal( [] ); | ||
expect( areShippingMethodsLoaded( state, 123 ) ).to.be.true; | ||
expect( areShippingMethodsLoading( state, 123 ) ).to.be.false; | ||
} ); | ||
|
||
it( 'when methods are currently being fetched.', () => { | ||
const state = { | ||
extensions: { | ||
woocommerce: { | ||
sites: { | ||
123: { | ||
shippingMethods: LOADING, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}; | ||
|
||
expect( getShippingMethods( state, 123 ) ).to.equal( LOADING ); | ||
expect( areShippingMethodsLoaded( state, 123 ) ).to.be.false; | ||
expect( areShippingMethodsLoading( state, 123 ) ).to.be.true; | ||
} ); | ||
|
||
it( 'when methods are loaded only for a different site.', () => { | ||
const state = { | ||
extensions: { | ||
woocommerce: { | ||
sites: { | ||
123: { | ||
shippingMethods: [], | ||
}, | ||
}, | ||
}, | ||
}, | ||
}; | ||
|
||
expect( getShippingMethods( state, 456 ) ).to.be.falsey; | ||
expect( areShippingMethodsLoaded( state, 456 ) ).to.be.false; | ||
expect( areShippingMethodsLoading( state, 456 ) ).to.be.false; | ||
} ); | ||
|
||
it( 'should get the siteId from the UI tree if not provided.', () => { | ||
const stateLoaded = { | ||
extensions: { | ||
woocommerce: { | ||
sites: { | ||
123: { | ||
shippingMethods: [], | ||
}, | ||
}, | ||
}, | ||
}, | ||
ui: { | ||
selectedSiteId: 123, | ||
}, | ||
}; | ||
const stateLoading = { | ||
extensions: { | ||
woocommerce: { | ||
sites: { | ||
123: { | ||
shippingMethods: LOADING, | ||
}, | ||
}, | ||
}, | ||
}, | ||
ui: { | ||
selectedSiteId: 123, | ||
}, | ||
}; | ||
|
||
expect( getShippingMethods( stateLoaded ) ).to.deep.equal( [] ); | ||
expect( areShippingMethodsLoaded( stateLoaded ) ).to.be.true; | ||
expect( areShippingMethodsLoading( stateLoaded ) ).to.be.false; | ||
|
||
expect( getShippingMethods( stateLoading ) ).to.equal( LOADING ); | ||
expect( areShippingMethodsLoaded( stateLoading ) ).to.be.false; | ||
expect( areShippingMethodsLoading( stateLoading ) ).to.be.true; | ||
} ); | ||
} ); | ||
} ); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters