Skip to content

Commit

Permalink
Prepare apiFetch data / data.args to convert booleans to 0/1
Browse files Browse the repository at this point in the history
  • Loading branch information
sc0ttkclark committed Sep 11, 2022
1 parent 66a0442 commit 665c7bd
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 3 deletions.
2 changes: 1 addition & 1 deletion ui/js/dfv/pods-dfv.min.asset.json
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{"dependencies":["lodash","moment","react","react-dom","wp-api-fetch","wp-autop","wp-components","wp-compose","wp-data","wp-element","wp-hooks","wp-i18n","wp-keycodes","wp-plugins","wp-polyfill","wp-primitives","wp-url"],"version":"f503439596a2361fb582"}
{"dependencies":["lodash","moment","react","react-dom","wp-api-fetch","wp-autop","wp-components","wp-compose","wp-data","wp-element","wp-hooks","wp-i18n","wp-keycodes","wp-plugins","wp-polyfill","wp-primitives","wp-url"],"version":"1198d87cf891ac047b46"}
2 changes: 1 addition & 1 deletion ui/js/dfv/pods-dfv.min.js

Large diffs are not rendered by default.

30 changes: 30 additions & 0 deletions ui/js/dfv/src/helpers/prepareApiFetchData.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/**
* Prepares the apiFetch data so that they can deal with the way it expects data types to be.
*
* @returns object
*/
const prepareApiFetchData = ( data ) => {
// Map true/false to 1/0 in data because the API will not send through true/false values to the request.
Object.entries( data ).forEach( ( [ key, value ] ) => {
if ( 'boolean' !== typeof value ) {
return;
}

data[ key ] = value ? 1 : 0;
} );

if ( 'undefined' !== typeof data.args ) {
// Map true/false to 1/0 in data because the API will not send through true/false values to the request.
Object.entries( data.args ).forEach( ( [ key, value ] ) => {
if ( 'boolean' !== typeof value ) {
return;
}

data.args[ key ] = value ? 1 : 0;
} );
}

return data;
};

export default prepareApiFetchData;
3 changes: 2 additions & 1 deletion ui/js/dfv/src/store/api-middleware.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import apiFetch from '@wordpress/api-fetch';

import { CURRENT_POD_ACTIONS } from 'dfv/src/store/constants';
import prepareApiFetchData from "dfv/src/helpers/prepareApiFetchData";

const {
API_REQUEST,
Expand Down Expand Up @@ -32,7 +33,7 @@ const apiMiddleware = ( { dispatch } ) => ( next ) => async ( action ) => {
path: url,
method,
parse: true,
data,
data: prepareApiFetchData( data ),
}
);

Expand Down

0 comments on commit 665c7bd

Please sign in to comment.