2018-12-06 22:08:40 +00:00
|
|
|
/** @format */
|
|
|
|
/**
|
|
|
|
* External dependencies
|
|
|
|
*/
|
2019-07-16 08:56:24 +00:00
|
|
|
import { addQueryArgs } from '@wordpress/url';
|
2018-12-06 22:08:40 +00:00
|
|
|
import apiFetch from '@wordpress/api-fetch';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Internal dependencies
|
|
|
|
*/
|
|
|
|
import { isResourcePrefix, getResourceIdentifier, getResourceName } from '../utils';
|
|
|
|
import { NAMESPACE } from '../constants';
|
|
|
|
|
|
|
|
function read( resourceNames, fetch = apiFetch ) {
|
|
|
|
return [ ...readNotes( resourceNames, fetch ), ...readNoteQueries( resourceNames, fetch ) ];
|
|
|
|
}
|
|
|
|
|
2019-03-12 13:13:20 +00:00
|
|
|
function update( resourceNames, data, fetch = apiFetch ) {
|
2019-05-24 20:22:46 +00:00
|
|
|
return [
|
|
|
|
...updateNote( resourceNames, data, fetch ),
|
|
|
|
...triggerAction( resourceNames, data, fetch ),
|
|
|
|
];
|
2019-03-12 13:13:20 +00:00
|
|
|
}
|
|
|
|
|
2018-12-06 22:08:40 +00:00
|
|
|
function readNoteQueries( resourceNames, fetch ) {
|
|
|
|
const filteredNames = resourceNames.filter( name => isResourcePrefix( name, 'note-query' ) );
|
|
|
|
|
|
|
|
return filteredNames.map( async resourceName => {
|
|
|
|
const query = getResourceIdentifier( resourceName );
|
2019-07-16 08:56:24 +00:00
|
|
|
const url = addQueryArgs( `${ NAMESPACE }/admin/notes`, query );
|
2018-12-06 22:08:40 +00:00
|
|
|
|
|
|
|
try {
|
|
|
|
const response = await fetch( {
|
|
|
|
parse: false,
|
|
|
|
path: url,
|
|
|
|
} );
|
|
|
|
|
|
|
|
const notes = await response.json();
|
|
|
|
const totalCount = parseInt( response.headers.get( 'x-wp-total' ) );
|
|
|
|
const ids = notes.map( note => note.id );
|
|
|
|
const noteResources = notes.reduce( ( resources, note ) => {
|
|
|
|
resources[ getResourceName( 'note', note.id ) ] = { data: note };
|
|
|
|
return resources;
|
|
|
|
}, {} );
|
|
|
|
|
|
|
|
return {
|
|
|
|
[ resourceName ]: {
|
|
|
|
data: ids,
|
|
|
|
totalCount,
|
|
|
|
},
|
|
|
|
...noteResources,
|
|
|
|
};
|
|
|
|
} catch ( error ) {
|
|
|
|
return { [ resourceName ]: { error } };
|
|
|
|
}
|
|
|
|
} );
|
|
|
|
}
|
|
|
|
|
|
|
|
function readNotes( resourceNames, fetch ) {
|
|
|
|
const filteredNames = resourceNames.filter( name => isResourcePrefix( name, 'note' ) );
|
|
|
|
return filteredNames.map( resourceName => readNote( resourceName, fetch ) );
|
|
|
|
}
|
|
|
|
|
|
|
|
function readNote( resourceName, fetch ) {
|
|
|
|
const id = getResourceIdentifier( resourceName );
|
|
|
|
const url = `${ NAMESPACE }/admin/notes/${ id }`;
|
|
|
|
|
|
|
|
return fetch( { path: url } )
|
|
|
|
.then( note => {
|
|
|
|
return { [ resourceName ]: { data: note } };
|
|
|
|
} )
|
|
|
|
.catch( error => {
|
|
|
|
return { [ resourceName ]: { error } };
|
|
|
|
} );
|
|
|
|
}
|
|
|
|
|
2019-03-12 13:13:20 +00:00
|
|
|
function updateNote( resourceNames, data, fetch ) {
|
|
|
|
const resourceName = 'note';
|
|
|
|
if ( resourceNames.includes( resourceName ) ) {
|
|
|
|
const { noteId, ...noteFields } = data[ resourceName ];
|
|
|
|
const url = `${ NAMESPACE }/admin/notes/${ noteId }`;
|
|
|
|
return [
|
|
|
|
fetch( { path: url, method: 'PUT', data: noteFields } )
|
|
|
|
.then( note => {
|
|
|
|
return { [ resourceName + ':' + noteId ]: { data: note } };
|
|
|
|
} )
|
|
|
|
.catch( error => {
|
|
|
|
return { [ resourceName + ':' + noteId ]: { error } };
|
|
|
|
} ),
|
|
|
|
];
|
|
|
|
}
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
|
2019-05-24 20:22:46 +00:00
|
|
|
function triggerAction( resourceNames, data, fetch ) {
|
|
|
|
const resourceName = 'note-action';
|
|
|
|
if ( resourceNames.includes( resourceName ) ) {
|
|
|
|
const { noteId, actionId } = data[ resourceName ];
|
|
|
|
const url = `${ NAMESPACE }/admin/notes/${ noteId }/action/${ actionId }`;
|
|
|
|
return [
|
|
|
|
fetch( { path: url, method: 'POST' } )
|
|
|
|
.then( note => {
|
|
|
|
return { [ 'note:' + noteId ]: { data: note } };
|
|
|
|
} )
|
|
|
|
.catch( error => {
|
|
|
|
return { [ 'note:' + noteId ]: { error } };
|
|
|
|
} ),
|
|
|
|
];
|
|
|
|
}
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
|
2018-12-06 22:08:40 +00:00
|
|
|
export default {
|
|
|
|
read,
|
2019-03-12 13:13:20 +00:00
|
|
|
update,
|
2019-05-24 20:22:46 +00:00
|
|
|
triggerAction,
|
2018-12-06 22:08:40 +00:00
|
|
|
};
|