Skip to content

Commit

Permalink
Merge branch 'enhancement/award-inventory-block' into develop
Browse files Browse the repository at this point in the history
  • Loading branch information
admturner committed Apr 26, 2022
2 parents b222ad9 + 03798f2 commit a7d182f
Show file tree
Hide file tree
Showing 5 changed files with 170 additions and 1 deletion.
45 changes: 45 additions & 0 deletions inc/classes/class-award-post-type.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ public function action_register_post_types(): void {
$template = array(
array( 'hrswp/er-award-description' ),
array( 'hrswp/er-award-meta-year' ),
array( 'hrswp/er-award-inventory' ),
);

$award_args = array(
Expand Down Expand Up @@ -119,6 +120,50 @@ public function action_register_award_meta() {
},
)
);

register_meta(
'post',
'hrswp_er_awards_quantity',
array(
'object_subtype' => 'hrswp_er_awards',
'type' => 'integer',
'default' => 5000,
'show_in_rest' => true,
'single' => true,
'sanitize_callback' => function( $value ) {
$value = (int) $value;
if ( empty( $value ) ) {
$value = 5000;
}
return abs( $value );
},
'auth_callback' => function() {
return current_user_can( 'edit_posts' );
},
)
);

register_meta(
'post',
'hrswp_er_awards_reserve',
array(
'object_subtype' => 'hrswp_er_awards',
'type' => 'integer',
'default' => 1,
'show_in_rest' => true,
'single' => true,
'sanitize_callback' => function( $value ) {
$value = (int) $value;
if ( empty( $value ) ) {
$value = 1;
}
return abs( $value );
},
'auth_callback' => function() {
return current_user_can( 'edit_posts' );
},
)
);
}

/**
Expand Down
21 changes: 21 additions & 0 deletions src/blocks/er-award-inventory/block.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"$schema": "https://schemas.wp.org/trunk/block.json",
"apiVersion": 2,
"name": "hrswp/er-award-inventory",
"title": "ER Award Inventory",
"category": "widgets",
"description": "Inventory tracking for a length-of-service award.",
"keywords": [ "text" ],
"textdomain": "default",
"attributes": {
"isQuantityEditable": {
"type": "boolean",
"default": true
}
},
"supports": {
"html": false,
"inserter": false
},
"editorScript": "hrswp-employee-recognition"
}
79 changes: 79 additions & 0 deletions src/blocks/er-award-inventory/edit.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
/**
* WordPress dependencies
*/
import { __ } from '@wordpress/i18n';
import {
PanelBody,
ToggleControl,
RangeControl,
} from '@wordpress/components';
import { useSelect } from '@wordpress/data';
import { useEntityProp } from '@wordpress/core-data';
import { InspectorControls, useBlockProps } from "@wordpress/block-editor";
import { box, pin } from "@wordpress/icons";

function ERAwardMetaInventoryEdit( { attributes, setAttributes } ) {
const { isQuantityEditable } = attributes;
const blockProps = useBlockProps();
const postType = useSelect( ( select ) =>
select( 'core/editor' ).getCurrentPostType(),
[] );

const [ meta, setMeta ] = useEntityProp( 'postType', postType, 'meta' );
const quantityMetaFieldValue = meta.hrswp_er_awards_quantity;
const reserveMetaFieldValue = meta.hrswp_er_awards_reserve;

const updateQuantityMetaValue = ( newValue ) => {
setMeta( { ...meta, hrswp_er_awards_quantity: Number( newValue ) } );
};
const updateReserveMetaValue = ( newValue ) => {
setMeta( { ...meta, hrswp_er_awards_reserve: Number( newValue ) } );
}

return (
<>
<InspectorControls>
<PanelBody title={ __( 'Inventory Management' ) }>
<ToggleControl
label={ __( 'Allow editing inventory' ) }
checked={ !! isQuantityEditable }
onChange={ () =>
setAttributes( {
isQuantityEditable: ! isQuantityEditable,
} )
}
/>
</PanelBody>
</InspectorControls>
<div { ...blockProps }>
<RangeControl
label={ __( 'ER Award Quantity' ) }
help={ __( 'The number of awards in inventory.' ) }
beforeIcon={ box }
value={ quantityMetaFieldValue }
onChange={ updateQuantityMetaValue }
min={ 0 }
max={ 9999 }
step={ 1 }
withInputField={ true }
disabled={ ! isQuantityEditable }
/>
<RangeControl
label={ __( 'ER Award Reserve' ) }
help={ __( 'The inventory amount at which an admin is notified.' ) }
beforeIcon={ pin }
value={ reserveMetaFieldValue }
onChange={ updateReserveMetaValue }
min={ 0 }
max={ 9999 }
step={ 1 }
withInputField={ true }
disabled={ ! isQuantityEditable }
separatorType={ 'topFullWidth' }
/>
</div>
</>
);
}

export default ERAwardMetaInventoryEdit;
19 changes: 19 additions & 0 deletions src/blocks/er-award-inventory/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/**
* WordPress dependencies
*/
import { store as icon } from '@wordpress/icons';

/**
* Internal dependencies
*/
import edit from './edit';
import metadata from './block.json';

const { name } = metadata;

export { metadata, name };

export const settings = {
icon,
edit,
};
7 changes: 6 additions & 1 deletion src/blocks/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,13 @@ import { registerBlockType } from '@wordpress/blocks';
*/
import * as erAwardDescription from './er-award-description';
import * as erAwardMetaYear from './er-award-meta-year';
import * as erAwardInventory from './er-award-inventory';

const erAwardBlocks = [ erAwardDescription, erAwardMetaYear ];
const erAwardBlocks = [
erAwardDescription,
erAwardMetaYear,
erAwardInventory,
];

const registerBlock = ( block ) => {
if ( ! block ) {
Expand Down

0 comments on commit a7d182f

Please sign in to comment.