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

Custom Asset Field Reordering #17873

Open
wants to merge 5 commits into
base: main
Choose a base branch
from

Conversation

cconard96
Copy link
Contributor

@cconard96 cconard96 commented Sep 17, 2024

  • I have read the CONTRIBUTING document.
  • I have performed a self-review of my code.
  • I have added tests that prove my fix is effective or that my feature works.
  • This change requires a documentation update.

Taking over from #16975
Currently focusing on reordering default fields until the custom fields PR is merged, and then the Fields tab UI will be adjusted and support for the custom fields added.

@cconard96 cconard96 force-pushed the feature/custom_asset_field_order branch 2 times, most recently from 2cb5785 to 024575a Compare September 25, 2024 10:11
@orthagh
Copy link
Contributor

orthagh commented Sep 26, 2024

Now #17462 has been merged, here are a few notes on that feature as it's the continuation;

Technically

  • fields attributes (mandatory, full width, etc) should be expanded to native fields
  • so we may store these informations in fields_display
  • I suggest so to remove these attributes from glpi_assets_customfields

Regarding UX/UI

  • we may have an edit icon for editing field attributes
  • native and custom fields should be set/displayed in the same grid (mainly to let people reorder them)
  • I STRONGLY suggest using modals in the current UI (for addition and edition). People may add a lot of custom fields and the need to scroll breaks the usage.
  • We shouldn't have a "Save" button globally. Maybe it's just a display bug, if not, all actions should auto-save.
  • "fake inputs" on the grid should display the default value if any
  • The addition process (and primary button) should be common for native and custom fields, maybe a toggle at the top lets you choose the proper form.

That's all for the moment, I may have other minor suggestions later (after seeing a first iteration)

@cconard96 cconard96 self-assigned this Sep 26, 2024
@cconard96 cconard96 force-pushed the feature/custom_asset_field_order branch from 9b8edd1 to 40ba2a6 Compare September 30, 2024 18:10
@cconard96 cconard96 force-pushed the feature/custom_asset_field_order branch from 3c9f33d to 42e5059 Compare October 8, 2024 12:10
@cconard96
Copy link
Contributor Author

cconard96 commented Oct 8, 2024

A checklist based on previous review comment:

  • Allow changing field options for core fields. Since core fields aren't really defined well, the options will be stored in the fields_display column for core fields. For custom fields, it is easier to keep storing the options the way they already are.
  • Edit icon to change field options, change custom field properties, and delete custom fields
  • All field previews reflect the correct type (string, number, dropdown) regardless of if they are core or custom.
  • Ability to delete custom field definitions in new UI
  • Reflect full width option in UI
  • Reflect mandatory option in UI. Need to manually add the mandatory indicator to the preview when needed.
  • Reflect readonly option in UI. Since all previews are disabled, need some other way to indicate a readonly field.
  • Show default value in UI
  • Process to add fields to the order preview is the same for core and custom fields

Also had to redo the field display as flexbox instead of grid for sorting to work properly when fields could be regular or full width.

@cconard96
Copy link
Contributor Author

Tests need updated, but I think this is ready for a review of the initial implementation.

Copy link
Contributor

@AdrienClairembault AdrienClairembault left a comment

Choose a reason for hiding this comment

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

Could you also add some tests ?

New logic should be validated as much as possible on PHPunit side, with a few cypress tests for the things that can also be done on the UI.

ajax/asset/assetdefinition.php Outdated Show resolved Hide resolved
ajax/asset/assetdefinition.php Outdated Show resolved Hide resolved
ajax/asset/assetdefinition.php Outdated Show resolved Hide resolved
ajax/asset/assetdefinition.php Outdated Show resolved Hide resolved
ajax/asset/assetdefinition.php Outdated Show resolved Hide resolved
ajax/asset/assetdefinition.php Outdated Show resolved Hide resolved
Copy link
Contributor

Choose a reason for hiding this comment

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

Symfony controllers have been available for a while now, IMO we shouldn't create any new legacy front files.

ajax/asset/assetdefinition.php Outdated Show resolved Hide resolved
templates/generic_show_form.html.twig Outdated Show resolved Hide resolved
@orthagh
Copy link
Contributor

orthagh commented Oct 14, 2024

A quick functional review for today's checkout on my side:

  • we can't set a default value for classic fields
  • The same for the "disabled" attribute but, for this one, I'm not sure it could be useful
  • The dropdowns in classic fields (models, manufacturers, etc) can be tagged as "multiple values", but it works only for groups_id/groups_id_tech.
  • We lack an indication a custom dropdown has been set as multiple (like done for other attributes as "mandatory", "read-only", etc)
  • There is a strange bug also with this attribute, when I set it on groups_id, save, set on groups_id tech, save, the first fields lose the attribute in the editor. Anyway, I don't think there is a need to have this toggle on classic fields. The group fields will have the behavior, when displaying an asset, no matter what is set up in the editor.
  • Could you explain the top helper phrase "Fields not added below will be appended at the end of the form rather than be hidden". I'm not sure to understand what it does.
  • I'm not satisfied with the current UX for adding classic fields and custom ones, but I don't have any suggestions (yet) I'll think about it.

@cconard96
Copy link
Contributor Author

* we can't set a default value for classic fields

Didn't realize that was required. This PR was originally just to reorder fields in the UI. I didn't see a spec anywhere for anything extra like making core fields mandatory/readonly/full width/etc. Also all this extra stuff is just in the web UI display. Nothing enforces it on the server side so the API doesn't respect it.

* The same for the "disabled" attribute but, for this one, I'm not sure it could be useful

Doubt it is useful

* The dropdowns in classic fields (models, manufacturers, etc) can be tagged as "multiple values", but it works only for groups_id/groups_id_tech.

Not intended to even have this option. Core fields have no structure so I faked it by reusing the custom field stuff.

* We lack an indication a custom dropdown has been set as multiple (like done for other attributes as "mandatory", "read-only", etc)

We kind of do. You just need to know that select2 controls look like a regular text field when they allow multiple options.

* There is a strange bug also with this attribute, when I set it on groups_id, save, set on groups_id tech, save, the first fields lose the attribute in the editor. **Anyway, I don't think there is a need to have this toggle on classic fields**. The group fields will have the behavior, when displaying an asset, no matter what is set up in the editor.

Not supposed to be an option.

* Could you explain the top helper phrase "Fields not added below will be appended at the end of the form rather than be hidden". I'm not sure to understand what it does.

You cannot hide fields using this UI currently. Look at the generic_show_form template and you see how this is all handled. It takes a custom defined order if one is provided and then appends the missing fields based on the default order. So, if we add new core fields that don't exist in the field order config for a custom asset, they will still appear in the form but be at the end of the form.

I did add handling for a fields_excluded twig parameter to exclude fields completely from the UI but it wasn't hooked up to this new UI. I missed that the spec did actually specify this feature.

The original spec was basically just:

  • Can reorder fields in UI
  • Can hide fields
  • Core fields cannot be changed at all

@cconard96 cconard96 force-pushed the feature/custom_asset_field_order branch 2 times, most recently from 6c0c6a2 to 27d3488 Compare October 14, 2024 15:21
@orthagh
Copy link
Contributor

orthagh commented Oct 21, 2024

When hiding everything except "name", a few fields remain on a custom asset: "UUID" and "update source".
Note, that it's not particularly useful from a functional pov, but you can't remove everything.
It fails with the following error:

[2024-10-21 08:57:30] glpiphplog.CRITICAL:   *** Uncaught Exception TypeError: array_merge(): Argument #1 must be of type array, null given in .../ajax/asset/assetdefinition.php at line 92

@cconard96
Copy link
Contributor Author

When hiding everything except "name", a few fields remain on a custom asset: "UUID" and "update source".

I can't recreate. I also cannot recreate the situation where all fields can be removed. AFAIK it would be impossible from the web UI because all fields_display fields would be removed and therefore nothing is sent to the server. The missing field isn't treated as a deletion.

@orthagh
Copy link
Contributor

orthagh commented Nov 4, 2024

A few things to discuss or fix. Sorry for the long wall of text, this took me more time than I tough.

Removing all fields

I can't recreate. I also cannot recreate the situation where all fields can be removed.

Below is a recording of my instance. I created a new type to be sure. Note I may have a broken state on my database, I'll check later.
With the last commit, I don't have any more errors, but the save is not taken into account.
Recording 2024-11-04 at 10 28 32(1)

Remaining fields

Anyway, I kept only the field "name". When I create an instance of this new asset definition, there are still a few fields displayed that are not managed: "is active", "UUID", "Update source"
image

Edit modal

Attributes of a native field are not retrieved in the edit modal. Ex I checked all properties here (without saving globally)
reopening the edit modal, fields are not checked:
image

Purge of a custom fields

The field remains in the grid after being purged permanently. A refresh fixes the display issue.

Strange behavior of dropdown

When adding a native field (previously hidden), the selected option in the " field " dropdown isn't removed. When you click the "Add" button a second time, it doesn't change anything in the "Preview" section, but the field is finally removed from the dropdown.
Recording 2024-11-04 at 09 29 49

Strange behavior of drag&drop

I finally found an issue with Drag&drop behavior I didn't understand previously but bothering me: When selecting an item and starting to drag it, the placeholder appears, but the initial item remains in DOM. This results in double space usage in the grid and doesn't reflect the final result when you drop the item.
Recording 2024-11-04 at 10 21 19

Drag&drop icon alignment:

align vertically the icon (maybe for this recurring one, we could find a more global fix)
image

Adding a custom field

A new added custom field should directly be added to the grid. We shouldn't have to add it manually.

custom field modal

  • add a title
  • remove horizontal lines
  • remove the grey footer

image

discussing UX

Below issues/suggestions, I don't have a strong opinion, and it's mainly open to discussion.

The main issue is the distinction for the final user between native and custom fields. If you are not familiar with the concept, this is very hard to understand what is going on.

Maybe we can add a helper explaining the concept at the top.

Regarding design, I always found the controls for adding a field, placed on top, don't permit a fluid understanding.
1st suggestion: integrating one unified button into the grid of sortable fields:
image

alternative: separate the add form of the grid with an hr

Another suggestion: rename the labels to identify clearly the action

  • "insert" (an existing field)
  • "Create a custom field"

In the same idea, all the people I presented the page didn't identify well the "Hide" action and what it achieves (returning the field in the list of available)

@cconard96
Copy link
Contributor Author

cconard96 commented Nov 6, 2024

  • Allow removing all fields
  • Fix issue with inventory-related fields showing when they aren't in the field display
  • Fix field options when reopening edit modal
  • Field dropdown selection not cleared after field added
  • Remove fields from view when purged from modal form
  • Fix original element when dragging and dropping still being shown which throws off alignment of the remaining fields
  • Vertically align sort handle with the field labels
  • Custom fields should be added directly to the view after creation
  • Custom field modal needs title and extra UI parts (hr and footer) should be removed

Not previously requested:

  • Convert to Vue for cleaner implementation and potentially faster E2E testing using Component Tests (component tests not done yet)

@cconard96
Copy link
Contributor Author

All points addressed except the dragged field still being in the UI. I haven't found a way to target the original element in CSS without also targeting the drag image element.

@orthagh
Copy link
Contributor

orthagh commented Nov 7, 2024

I'll try to make a test in 1h or 2

Copy link
Contributor

@orthagh orthagh left a comment

Choose a reason for hiding this comment

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

Regarding the functional side, we arrived at an ok state.
The UX for adding native or custom fields needs reflection and work, but this can be achieved in a later PR.

Copy link
Member

@cedric-anne cedric-anne left a comment

Choose a reason for hiding this comment

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

  1. IMHO, the glpi_assets_customfielddefinitions.field_options field should not contain anymore options related to the form layout. Indeed, with the current data model, the layout options are not all stored in the same place, and it will probably lead to some confusion.

  2. When a custom field definition is removed, it is not automatically removed from the glpi_assets_assetdefinitions.fields_display field. It should be done inside the cleanDBonPurge method.

ajax/asset/assetdefinition.php Outdated Show resolved Hide resolved
ajax/asset/assetdefinition.php Outdated Show resolved Hide resolved
ajax/asset/assetdefinition.php Outdated Show resolved Hide resolved
ajax/asset/assetdefinition.php Outdated Show resolved Hide resolved
ajax/asset/assetdefinition.php Outdated Show resolved Hide resolved
ajax/asset/assetdefinition.php Outdated Show resolved Hide resolved
ajax/asset/assetdefinition.php Outdated Show resolved Hide resolved
ajax/asset/assetdefinition.php Outdated Show resolved Hide resolved
templates/generic_show_form.html.twig Outdated Show resolved Hide resolved
@cedric-anne cedric-anne added this to the 11.0.0 milestone Nov 13, 2024
@cconard96 cconard96 force-pushed the feature/custom_asset_field_order branch from 819ef84 to 116ba38 Compare November 15, 2024 11:53
@orthagh
Copy link
Contributor

orthagh commented Nov 26, 2024

What do we need to finish this pr (and #18145 ) ?

@cconard96
Copy link
Contributor Author

Translations PR needs tests added now that it should be functionally and technically complete.

This PR should clean the field display values when a custom field is deleted. Then, I assume it is finally ready for tests to be added.

@cconard96 cconard96 force-pushed the feature/custom_asset_field_order branch from f1c0f40 to 71138da Compare November 26, 2024 21:23
@cconard96 cconard96 force-pushed the feature/custom_asset_field_order branch from 71138da to 6057921 Compare November 27, 2024 00:38
@orthagh
Copy link
Contributor

orthagh commented Nov 27, 2024

Sonarcloud report: remaining alert seems the same and should probably ignored.

The current failing e2e test is due to the modal losing focus on the input while Cypress is typing the label.
A rough solution, waiting for the event shown/hidden to add an interceptable attribute by cypress:3

diff --git a/templates/components/form/viewsubitem.html.twig b/templates/components/form/viewsubitem.html.twig
index 40c2bdc79b..3f302044d6 100644
--- a/templates/components/form/viewsubitem.html.twig
+++ b/templates/components/form/viewsubitem.html.twig
@@ -56,6 +56,12 @@
 <script>
     function viewAddEditSubItem{{ rand }}(id) {
         const modal_el = $('#{{ subitem_container_id ~ '_modal' }}');
+        modal_el[0].addEventListener('shown.bs.modal', (event) => {
+            event.target.setAttribute('data-cy-ready', 'true')
+        });
+        modal_el[0].addEventListener('hidden.bs.modal', (event) => {
+            event.target.removeAttribute('data-cy-ready')
+        });
         $('#{{ subitem_container_id }}').load('/ajax/viewsubitem.php',{
             type: "{{ type|e('js') }}",
             parenttype: "{{ parenttype|e('js') }}",
diff --git a/tests/cypress/e2e/Asset/custom_fields.cy.js b/tests/cypress/e2e/Asset/custom_fields.cy.js
index ade0902bc2..b97e7d065a 100644
--- a/tests/cypress/e2e/Asset/custom_fields.cy.js
+++ b/tests/cypress/e2e/Asset/custom_fields.cy.js
@@ -125,7 +125,7 @@ describe("Custom Assets - Custom Fields", () => {
         function createField(label, type, options = new Map()) {
             cy.findByRole('button', {name: 'Create new field'}).click();
             cy.waitForNetworkIdle('/ajax/viewsubitem.php', 100);
-            cy.findByRole('button', {name: 'Create new field'}).siblings('.modal').should('be.visible').within(() => {
+            cy.findByRole('button', {name: 'Create new field'}).siblings('.modal[data-cy-ready=true]').should('be.visible').within(() => {
                 cy.findByLabelText('Label').type(label);
                 cy.findByLabelText('Type').select(type, {force: true});

They may be a better solution, but we should avoid adding a wait(...) for this kind of issue.

@cconard96 cconard96 force-pushed the feature/custom_asset_field_order branch from c6bfcb9 to 1730b58 Compare November 27, 2024 10:36
Copy link
Member

@cedric-anne cedric-anne left a comment

Choose a reason for hiding this comment

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

It seems OK from the functional point of view. I have not done a detailed technical review because I am having difficulty assimilating all the changes as a whole.

templates/generic_show_form.html.twig Outdated Show resolved Hide resolved
templates/generic_show_form.html.twig Outdated Show resolved Hide resolved
@cedric-anne
Copy link
Member

E2E tests seems to fail.

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

Successfully merging this pull request may close these issues.

4 participants