Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use APIv4-based Autocomplete widget throughout SearchKit, Afform & API Explorer #24974

Merged
merged 2 commits into from
Nov 14, 2022

Conversation

colemanw
Copy link
Member

Overview

This switches our 3 APIv4-based UIs to consistently use the new Autocomplete widget instead of the old v3-based entityRef

Before

v3 widget on v4 screens.

After

v4 only

Comments

You won't notice much difference in the UI as the widgets look the same. There are some differences in how the results are formatted as some v3 apis have customizations to give more verbose output. I'm working on recreating that in APIv4...

@civibot
Copy link

civibot bot commented Nov 14, 2022

(Standard links)

@civibot civibot bot added the master label Nov 14, 2022
@artfulrobot
Copy link
Contributor

artfulrobot commented Nov 14, 2022

FYI: I have started a code read through review but not posted as I have some reservations on introducing new code using old javacsript libraries which have long since been supported by native JS. https://chat.civicrm.org/civicrm/pl/mhnp3ryc4ifrx8iybccwqhf3kw

In terms of r-run, using the patch and api4 explorer:

  • ✔ I was able to select an activity type (optionvalue) for an Activity.get Where clause.
  • ✖ I was NOT able to select a contact (for Activity.create target_contact_id) using the User Contact ID. *I think this would be a regression?
  • ✔ I was able to specify 'current logged in contact' for contact source and target IDs in Activity.create.

I'm sure there's other places and tests, but I've run out of time.

@mattwire
Copy link
Contributor

@colemanw see possible regression from @artfulrobot

Copy link
Contributor

@artfulrobot artfulrobot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's lots of this that could be reduced and simplified using ES6.

staticItems = getStaticOptions(select2Options.static),
multiple = !!select2Options.multiple;

$el.crmSelect2(_.extend({
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider standard Object.assign call instead? See discussion

if (!idsNeeded.length) {
callback(multiple ? existing : existing[0]);
} else {
var params = $.extend({}, apiParams || {}, {ids: idsNeeded});
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider standard Object.assign call instead? See discussion

Or at least comment as to why jQuery's implementation (and not Lodash's or vanilla's) is chosen here.

@@ -31,7 +30,7 @@ class AfformAutocompleteSubscriber extends AutoService implements EventSubscribe
*/
public static function getSubscribedEvents() {
return [
'civi.api.prepare' => ['onApiPrepare', Events::W_MIDDLE],
'civi.api.prepare' => ['onApiPrepare', -20],
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we have a comment to explain the hard coded integer? W_MIDDLE at least gives a clue.

Comment on lines +536 to +538
return _.transform(staticItems || [], function(staticItems, option) {
staticItems.push(_.isString(option) ? staticPresets[option] : option);
});
Copy link
Contributor

@artfulrobot artfulrobot Nov 14, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I find this hard to reason about.

staticPresets is a hard coded object.

We're _.transform-ing staticItems which, from a quick scan seems to be an Object, into a new copy of staticItems (or an array? - change of type?). It's confusing because: _.transform() which only iterates string keyed properties anyway, so why the _.isString() call which would always return true?

I guess it's saying: if one of the staticPresets keys is specified as a string in the staticItems array, replace it with the object above.

Is this the same - for this use case - as simply:

Suggested change
return _.transform(staticItems || [], function(staticItems, option) {
staticItems.push(_.isString(option) ? staticPresets[option] : option);
});
return staticItems.map(option => staticPresets[option] || option)

I think this is a lot easier to read/understand.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's actually iterating an array. The array contains either strings or objects. If it contains a string it's replaced with the matching object from staticPresets.
Maybe I should add a comment...

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@colemanw Yes, and my suggestion results in the exact same behaviour (I tested).

@colemanw
Copy link
Member Author

I'm still actively working on improving the output of the new widgets for parity (or better) with the old widgets. I've got another PR for that I'll post soon, and I'll include search-by-id as a default.

@mattwire
Copy link
Contributor

Merging per discussion https://chat.civicrm.org/civicrm/pl/ch49u7k57b8emxrptmyzircb6c because @colemanw will provide follow-up PRs to address issues.

@mattwire mattwire merged commit 21cd793 into civicrm:master Nov 14, 2022
@mattwire
Copy link
Contributor

@colemanw Please follow up with a PR to add comments where @artfulrobot suggests

@eileenmcnaughton eileenmcnaughton deleted the api4Autocompletes branch November 14, 2022 21:15
@colemanw
Copy link
Member Author

@mattwire @artfulrobot here is the followup, it would be great to get it reviewed soon :)

@artfulrobot
Copy link
Contributor

@colemanw did you mean to include a link?

@colemanw
Copy link
Member Author

@artfulrobot I was counting on you to telepathically know I was referring to #24976

@artfulrobot
Copy link
Contributor

@colemanw ah, I was getting the 2 and the 4 but it was a bit blurry after that 😆

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants