Skip to content

Commit

Permalink
Move workflow manager to component
Browse files Browse the repository at this point in the history
  • Loading branch information
alecgeatches committed Jul 5, 2024
1 parent e2909f8 commit 9a6fda1
Show file tree
Hide file tree
Showing 5 changed files with 111 additions and 106 deletions.
Original file line number Diff line number Diff line change
@@ -1 +1 @@
<?php return array('dependencies' => array('react', 'react-dom', 'wp-dom-ready', 'wp-element', 'wp-i18n', 'wp-primitives'), 'version' => 'a0162d0b6134374f1c60');
<?php return array('dependencies' => array('react', 'react-dom', 'wp-dom-ready', 'wp-element', 'wp-i18n', 'wp-primitives'), 'version' => 'f5137c662b4fcebfd284');
2 changes: 1 addition & 1 deletion dist/modules/custom-status/custom-status-configure.js

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ export default function DraggableCustomStatus( { customStatus, index, provided,
<div className="name">{ customStatus.name }</div>

<div className="drag-handle">
<Icon icon={ dragHandle } size={ 24 } />
<Icon icon={ dragHandle } size={ 20 } />
</div>
</div>
</>
Expand Down
100 changes: 100 additions & 0 deletions modules/custom-status/lib/components/workflow-manager.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
import { DragDropContext, Droppable, Draggable } from '@hello-pangea/dnd';
import { useState, useRef } from '@wordpress/element';
import { __ } from '@wordpress/i18n';

import DraggableCustomStatus from './draggable-custom-status';
import WorkflowArrow, { useRefDimensions } from './workflow-arrow';

export default function WorkflowManager( { customStatuses } ) {
const [ items, setItems ] = useState( customStatuses );

const statusContainerRef = useRef( null );
const [ statusContanerWidth, statusContainerHeight ] = useRefDimensions( statusContainerRef );

const onDragEnd = result => {
// Dropped outside the list
if ( ! result.destination ) {
return;
}

const reorderedItems = reorder( items, result.source.index, result.destination.index );
setItems( reorderedItems );
updateCustomStatusOrder( reorderedItems );
};

return (
<div className="workflow-manager">
<WorkflowArrow
start={ __( 'Create' ) }
end={ __( 'Publish' ) }
referenceDimensions={ { width: statusContanerWidth, height: statusContainerHeight } }
/>

<DragDropContext onDragEnd={ onDragEnd }>
<Droppable droppableId="droppable">
{ ( provided, snapshot ) => (
<div
className="status-container"
{ ...provided.droppableProps }
ref={ el => {
statusContainerRef.current = el;
provided.innerRef( el );
} }
style={ getListStyle( snapshot.isDraggingOver ) }
>
{ items.map( ( item, index ) => (
<Draggable key={ item.term_id } draggableId={ `${ item.term_id }` } index={ index }>
{ ( provided, snapshot ) => (
<DraggableCustomStatus
customStatus={ item }
index={ index }
provided={ provided }
snapshot={ snapshot }
/>
) }
</Draggable>
) ) }
{ provided.placeholder }
</div>
) }
</Droppable>
</DragDropContext>
</div>
);
}

const reorder = ( list, startIndex, endIndex ) => {
const result = Array.from( list );
const [ removed ] = result.splice( startIndex, 1 );
result.splice( endIndex, 0, removed );

return result;
};

const getListStyle = isDraggingOver => ( {
background: isDraggingOver ? 'lightblue' : 'white',
} );

function updateCustomStatusOrder( reorderedItems ) {
// Prepare the POST
const params = {
action: 'update_status_positions',
status_positions: reorderedItems.map( item => item.term_id ),
custom_status_sortable_nonce: VW_CUSTOM_STATUS_CONFIGURE.reorder_nonce,
};
// Inform WordPress of our updated positions
jQuery.post( VW_CUSTOM_STATUS_CONFIGURE.ajax_url, params, function ( retval ) {
// jQuery( '.edit-flow-admin .edit-flow-message' ).remove();
// If there's a success message, print it. Otherwise we assume we received an error message
if ( retval.status == 'success' ) {
var message =
'<span class="edit-flow-updated-message edit-flow-message">' + retval.message + '</span>';
} else {
var message =
'<span class="edit-flow-error-message edit-flow-message">' + retval.message + '</span>';
}
// jQuery( '.edit-flow-admin h2' ).append( message );
// // Set a timeout to eventually remove it
// setTimeout( edit_flow_hide_message, 8000 );
} );
}
111 changes: 8 additions & 103 deletions modules/custom-status/lib/custom-status-configure.js
Original file line number Diff line number Diff line change
@@ -1,114 +1,23 @@
import './configure.scss';

import { DragDropContext, Droppable, Draggable } from '@hello-pangea/dnd';
import domReady from '@wordpress/dom-ready';
import { createRoot, useState, useRef } from '@wordpress/element';
import { __ } from '@wordpress/i18n';

import DraggableCustomStatus from './components/draggable-custom-status';
import WorkflowArrow, { useRefDimensions } from './components/workflow-arrow';

function WorkflowManager() {
const [ items, setItems ] = useState( VW_CUSTOM_STATUS_CONFIGURE.custom_statuses );

const statusContainerRef = useRef( null );
const [ statusContanerWidth, statusContainerHeight ] = useRefDimensions( statusContainerRef );

const onDragEnd = result => {
// Dropped outside the list
if ( ! result.destination ) {
return;
}

const reorderedItems = reorder( items, result.source.index, result.destination.index );
setItems( reorderedItems );
updateCustomStatusOrder( reorderedItems );
};

return (
<div className="workflow-manager">
<WorkflowArrow
start={ __( 'Create' ) }
end={ __( 'Publish' ) }
referenceDimensions={ { width: statusContanerWidth, height: statusContainerHeight } }
/>

<DragDropContext onDragEnd={ onDragEnd }>
<Droppable droppableId="droppable">
{ ( provided, snapshot ) => (
<div
className="status-container"
{ ...provided.droppableProps }
ref={ el => {
statusContainerRef.current = el;
provided.innerRef( el );
} }
style={ getListStyle( snapshot.isDraggingOver ) }
>
{ items.map( ( item, index ) => (
<Draggable key={ item.term_id } draggableId={ `${ item.term_id }` } index={ index }>
{ ( provided, snapshot ) => (
<DraggableCustomStatus
customStatus={ item }
index={ index }
provided={ provided }
snapshot={ snapshot }
/>
) }
</Draggable>
) ) }
{ provided.placeholder }
</div>
) }
</Droppable>
</DragDropContext>
</div>
);
}
import { createRoot } from '@wordpress/element';

import WorkflowManager from './components/workflow-manager';

domReady( () => {
const workflowManagerRoot = document.getElementById( 'workflow-manager-root' );

if ( workflowManagerRoot ) {
const root = createRoot( workflowManagerRoot );
root.render( <WorkflowManager /> );
root.render(
<WorkflowManager customStatuses={ VW_CUSTOM_STATUS_CONFIGURE.custom_statuses } />
);
}
} );

const reorder = ( list, startIndex, endIndex ) => {
const result = Array.from( list );
const [ removed ] = result.splice( startIndex, 1 );
result.splice( endIndex, 0, removed );

return result;
};

const getListStyle = isDraggingOver => ( {
background: isDraggingOver ? 'lightblue' : 'white',
} );

function updateCustomStatusOrder( reorderedItems ) {
// Prepare the POST
const params = {
action: 'update_status_positions',
status_positions: reorderedItems.map( item => item.term_id ),
custom_status_sortable_nonce: VW_CUSTOM_STATUS_CONFIGURE.reorder_nonce,
};
// Inform WordPress of our updated positions
jQuery.post( VW_CUSTOM_STATUS_CONFIGURE.ajax_url, params, function ( retval ) {
// jQuery( '.edit-flow-admin .edit-flow-message' ).remove();
// If there's a success message, print it. Otherwise we assume we received an error message
if ( retval.status == 'success' ) {
var message =
'<span class="edit-flow-updated-message edit-flow-message">' + retval.message + '</span>';
} else {
var message =
'<span class="edit-flow-error-message edit-flow-message">' + retval.message + '</span>';
}
// jQuery( '.edit-flow-admin h2' ).append( message );
// // Set a timeout to eventually remove it
// setTimeout( edit_flow_hide_message, 8000 );
} );
if ( module.hot ) {
module.hot.accept();
}

// ( function ( $ ) {
Expand Down Expand Up @@ -270,7 +179,3 @@ function updateCustomStatusOrder( reorderedItems ) {
// }
// } );
// } );

if ( module.hot ) {
module.hot.accept();
}

0 comments on commit 9a6fda1

Please sign in to comment.