From 4581d25178f587a72c31f9f06b86b8b05cc59c20 Mon Sep 17 00:00:00 2001 From: Zeshan Ahmed Date: Wed, 23 Oct 2024 13:47:16 +0100 Subject: [PATCH 1/4] Fix #161 user select in workflows removed selected values --- admin/src/WorkflowUI.js | 39 +++++++++++++++++++------ inc/class-rest-workflows-controller.php | 25 +++++++++++++++- 2 files changed, 54 insertions(+), 10 deletions(-) diff --git a/admin/src/WorkflowUI.js b/admin/src/WorkflowUI.js index c4cf0f3fb..a24be971f 100644 --- a/admin/src/WorkflowUI.js +++ b/admin/src/WorkflowUI.js @@ -292,7 +292,13 @@ class WorkflowUI extends Component { const recipientValue = recipient.value ? { value: recipientObject.multi ? recipient.value : recipient.value[0] } : {}; - return Object.assign( {}, recipientObject, recipientValue ); + let recipientUsers = {}; + + if (recipient.users) { + recipientUsers = { users: recipientObject.multi ? recipient.users : recipient.users[0] }; + } + + return Object.assign( {}, recipientObject, recipientValue, recipientUsers ); } ), destinations: data.destinations.map( destination => { const destObject = HM.Workflows.Destinations.find( dest => dest.id === destination.id ); @@ -564,7 +570,7 @@ class WorkflowUI extends Component { .then( response => response.json() ) .then( data => ({ options: data }) ) } - value={recipient.value} + value={(recipient.users || []).filter(user => recipient.value.includes(String(user.id)))} labelKey={recipient.endpoint.labelKey || 'name'} valueKey={recipient.endpoint.valueKey || 'id'} onChange={option => this.setState( { @@ -572,13 +578,28 @@ class WorkflowUI extends Component { if ( rec.id !== recipient.id ) { return rec; } - return Object.assign( {}, rec, { - value: rec.multi - ? option.map( opt => String( opt[ recipient.endpoint.valueKey || 'id' ] ) ) - : String( option[ recipient.endpoint.valueKey || 'id' ] ) - } ); - } ) - } )} + + const selectedValue = rec.multi + ? option.map(opt => String(opt[recipient.endpoint.valueKey || 'id'])) + : String(option[recipient.endpoint.valueKey || 'id']); + + // Handle single or multi-select options + const selectedUsers = rec.multi ? option : [option]; + + // Merge with the existing users + const mergedItems = [...(rec.users || []), ...(selectedUsers || [])].reduce((acc, current) => { + if (!acc.some(item => item.id === current.id)) { + acc.push(current); + } + return acc; + }, []); + + return Object.assign({}, rec, { + value: selectedValue, + users: mergedItems + }); + }) + })} />} diff --git a/inc/class-rest-workflows-controller.php b/inc/class-rest-workflows-controller.php index e711fb249..ff2561fcb 100644 --- a/inc/class-rest-workflows-controller.php +++ b/inc/class-rest-workflows-controller.php @@ -85,7 +85,30 @@ public function register_routes() { register_rest_field( 'hm_workflow', 'recipients', [ 'get_callback' => function ( $post ) { - return get_post_meta( $post['id'], 'recipients', true ) ?: []; + $recipients = get_post_meta( $post['id'], 'recipients', true ); + + // Loop through each recipient and set the 'users' field if field type is 'user' + foreach ( $recipients as &$recipient ) { + if ( isset( $recipient['id'] ) && $recipient['id'] === 'user' ) { + $user_ids = isset( $recipient['value'] ) ? $recipient['value'] : []; + + if ( ! empty( $user_ids ) && is_array( $user_ids ) ) { + $users = get_users([ + 'include' => $user_ids + ]); + + // Prepare the formatted response to include only 'id' and 'name' + $recipient['users'] = array_map( function( $user ) { + return [ + 'id' => $user->ID, + 'name' => $user->display_name, + ]; + }, $users ); + } + } + } + + return $recipients ?: []; }, 'update_callback' => function ( $value, $post ) { update_post_meta( $post->ID, 'recipients', $value ); From b82d44510566ceeea8c25a31a82aeb641c217893 Mon Sep 17 00:00:00 2001 From: Zeshan Ahmed Date: Tue, 29 Oct 2024 10:11:39 +0000 Subject: [PATCH 2/4] Update admin/src/WorkflowUI.js Co-authored-by: Robert O'Rourke <23417+roborourke@users.noreply.github.com> --- admin/src/WorkflowUI.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/admin/src/WorkflowUI.js b/admin/src/WorkflowUI.js index a24be971f..6b1f3ae41 100644 --- a/admin/src/WorkflowUI.js +++ b/admin/src/WorkflowUI.js @@ -294,7 +294,7 @@ class WorkflowUI extends Component { : {}; let recipientUsers = {}; - if (recipient.users) { + if ( recipient.users && recipient.users.length > 0 ) { recipientUsers = { users: recipientObject.multi ? recipient.users : recipient.users[0] }; } From 4df0a385073244f11afc498b0fc253c61ce167db Mon Sep 17 00:00:00 2001 From: Zeshan Ahmed Date: Tue, 29 Oct 2024 11:10:40 +0000 Subject: [PATCH 3/4] Fix formatting issues --- inc/class-rest-workflows-controller.php | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/inc/class-rest-workflows-controller.php b/inc/class-rest-workflows-controller.php index ff2561fcb..ea074d68f 100644 --- a/inc/class-rest-workflows-controller.php +++ b/inc/class-rest-workflows-controller.php @@ -93,12 +93,12 @@ public function register_routes() { $user_ids = isset( $recipient['value'] ) ? $recipient['value'] : []; if ( ! empty( $user_ids ) && is_array( $user_ids ) ) { - $users = get_users([ - 'include' => $user_ids - ]); + $users = get_users( [ + 'include' => $user_ids, + ] ); // Prepare the formatted response to include only 'id' and 'name' - $recipient['users'] = array_map( function( $user ) { + $recipient['users'] = array_map( function ( $user ) { return [ 'id' => $user->ID, 'name' => $user->display_name, From 811fca086204a4a31762e5e642cdfab2eca75c07 Mon Sep 17 00:00:00 2001 From: Zeshan Ahmed Date: Tue, 29 Oct 2024 11:11:26 +0000 Subject: [PATCH 4/4] Add support for single user value if users select is not set to multi --- admin/src/WorkflowUI.js | 25 ++++++++++++++++--------- 1 file changed, 16 insertions(+), 9 deletions(-) diff --git a/admin/src/WorkflowUI.js b/admin/src/WorkflowUI.js index 6b1f3ae41..1a76d202a 100644 --- a/admin/src/WorkflowUI.js +++ b/admin/src/WorkflowUI.js @@ -570,7 +570,11 @@ class WorkflowUI extends Component { .then( response => response.json() ) .then( data => ({ options: data }) ) } - value={(recipient.users || []).filter(user => recipient.value.includes(String(user.id)))} + value={ + recipient.multi + ? ( recipient.users || [] ).filter( user => recipient.value.includes( String( user.id ) ) ) + : recipient.users + } labelKey={recipient.endpoint.labelKey || 'name'} valueKey={recipient.endpoint.valueKey || 'id'} onChange={option => this.setState( { @@ -579,24 +583,27 @@ class WorkflowUI extends Component { return rec; } - const selectedValue = rec.multi - ? option.map(opt => String(opt[recipient.endpoint.valueKey || 'id'])) - : String(option[recipient.endpoint.valueKey || 'id']); + // Make sure to only set value if option is not null or empty. + const selectedValue = option ? + rec.multi + ? option.map(opt => String(opt[recipient.endpoint.valueKey || 'id'])) + : String( option[recipient.endpoint.valueKey || 'id'] ) + : ''; // Handle single or multi-select options const selectedUsers = rec.multi ? option : [option]; // Merge with the existing users - const mergedItems = [...(rec.users || []), ...(selectedUsers || [])].reduce((acc, current) => { - if (!acc.some(item => item.id === current.id)) { - acc.push(current); + const mergedItems = [...(rec.users || []), ...( selectedUsers || [])].reduce( ( acc, current ) => { + if ( ! acc.some(item => item.id === current.id ) ) { + acc.push( current ); } return acc; - }, []); + }, [] ); return Object.assign({}, rec, { value: selectedValue, - users: mergedItems + users: rec.multi ? mergedItems : mergedItems[0] }); }) })}