Update notes store to include queries

This commit is contained in:
Allen Snook 2018-10-16 12:46:00 -07:00
parent 8622ee7fc4
commit ad06cee25d
5 changed files with 101 additions and 30 deletions

View File

@ -20,7 +20,7 @@ import { Section } from '@woocommerce/components';
class InboxPanel extends Component {
render() {
const { loading, notes } = this.props;
const { isRequesting, notes } = this.props;
const getButtonsFromActions = actions => {
if ( ! actions ) {
@ -40,7 +40,7 @@ class InboxPanel extends Component {
<Fragment>
<ActivityHeader title={ __( 'Inbox', 'wc-admin' ) } />
<Section>
{ loading ? (
{ isRequesting ? (
<ActivityCardPlaceholder
className="woocommerce-inbox-activity-card"
hasAction
@ -70,9 +70,18 @@ class InboxPanel extends Component {
export default compose(
withSelect( select => {
const { getNotes } = select( 'wc-admin' );
const notes = getNotes();
const loading = select( 'core/data' ).isResolving( 'wc-admin', 'getNotes' );
return { loading, notes };
const { getNotes, isGetNotesError, isGetNotesRequesting } = select( 'wc-admin' );
const inboxQuery = {
page: 1,
per_page: 25,
};
const notes = getNotes( inboxQuery );
const isError = isGetNotesError( inboxQuery );
const isRequesting = isGetNotesRequesting( inboxQuery );
console.log( notes );
return { notes, isError, isRequesting };
} )
)( InboxPanel );

View File

@ -1,10 +1,17 @@
/** @format */
export default {
setNotes( notes ) {
setNotes( notes, query ) {
return {
type: 'SET_NOTES',
notes,
query: query || {},
};
},
setNotesError( query ) {
return {
type: 'SET_NOTES_ERROR',
query: query || {},
};
},
};

View File

@ -1,22 +1,30 @@
/** @format */
const DEFAULT_STATE = {
notes: {},
ids: [],
};
/**
* External dependencies
*/
import { merge } from 'lodash';
/**
* Internal dependencies
*/
import { ERROR } from 'store/constants';
import { getJsonString } from 'store/utils';
const DEFAULT_STATE = {};
export default function notesReducer( state = DEFAULT_STATE, action ) {
const queryKey = getJsonString( action.query );
switch ( action.type ) {
case 'SET_NOTES':
const { notes } = action;
const notesMap = notes.reduce( ( map, note ) => {
map[ note.id ] = note;
return map;
}, {} );
return {
...state,
notes: Object.assign( {}, state.notes, notesMap ),
};
return merge( {}, state, {
[ queryKey ]: action.notes,
} );
case 'SET_NOTES_ERROR':
return merge( {}, state, {
[ queryKey ]: ERROR,
} );
}
return state;

View File

@ -1,21 +1,24 @@
/** @format */
/**
* External dependencies
*/
import { dispatch } from '@wordpress/data';
import apiFetch from '@wordpress/api-fetch';
/**
* Internal dependencies
*/
import { stringifyQuery } from 'lib/nav-utils';
import { NAMESPACE } from 'store/constants';
export default {
async getNotes() {
async getNotes( state, query ) {
try {
const notes = await apiFetch( { path: '/wc/v3/admin/notes' } );
dispatch( 'wc-admin' ).setNotes( notes );
const notes = await apiFetch( { path: NAMESPACE + 'admin/notes' + stringifyQuery( query ) } );
dispatch( 'wc-admin' ).setNotes( notes, query );
} catch ( error ) {
if ( error && error.responseJSON ) {
alert( error.responseJSON.message );
} else {
alert( error );
}
dispatch( 'wc-admin' ).setNotesError( query );
}
},
};

View File

@ -1,7 +1,51 @@
/** @format */
/**
* External dependencies
*/
import { get } from 'lodash';
import { select } from '@wordpress/data';
/**
* Internal dependencies
*/
import { getJsonString } from 'store/utils';
import { ERROR } from 'store/constants';
/**
* Returns notes for a specific query.
*
* @param {Object} state Current state
* @param {Object} query Note query parameters
* @return {Array} Notes
*/
function getNotes( state, query = {} ) {
console.log( 'in getNotes, query = ', query );
console.log( 'in getNotes, state = ', state );
return get( state, [ 'notes' ], getJsonString( query ), [] );
}
export default {
getNotes( state ) {
return state.notes.notes;
getNotes,
/**
* Returns true if a query is pending.
*
* @param {Object} state Current state
* @return {Boolean} True if the `getNotes` request is pending, false otherwise
*/
isGetNotesRequesting( state, ...args ) {
return select( 'core/data' ).isResolving( 'wc-admin', 'getNotes', args );
},
/**
* Returns true if a get notes request has returned an error.
*
* @param {Object} state Current state
* @param {Object} query Query parameters
* @return {Boolean} True if the `getNotes` request has failed, false otherwise
*/
isGetNotesError( state, query ) {
return ERROR === getNotes( state, query );
},
};