vaadin / flow-components

Java counterpart of Vaadin Web Components
81 stars 63 forks source link

Grid: misleading JS error messages upon incorrect user data (duplicates) #3795

Open enver-haase opened 1 year ago

enver-haase commented 1 year ago

Description

Try this code.

package com.vaadin.qa.views.helloworld;

import com.vaadin.flow.component.orderedlayout.VerticalLayout;
import com.vaadin.flow.component.treegrid.TreeGrid;
import com.vaadin.flow.data.provider.hierarchy.AbstractBackEndHierarchicalDataProvider;
import com.vaadin.flow.data.provider.hierarchy.HierarchicalQuery;
import com.vaadin.flow.function.ValueProvider;
import com.vaadin.flow.router.PageTitle;
import com.vaadin.flow.router.Route;
import com.vaadin.flow.router.RouteAlias;

import java.util.*;
import java.util.stream.Stream;

@PageTitle("Hello World")
@Route(value = "hello")
@RouteAlias(value = "")
public class HelloWorldView extends VerticalLayout {

    public static class TreeDataProvider<TYPE> extends AbstractBackEndHierarchicalDataProvider<TYPE, Void> {

        private final ValueProvider<TYPE, Collection<TYPE>> childItemProvider;

        public TreeDataProvider(ValueProvider<TYPE, Collection<TYPE>> childItemProvider) {
            this.childItemProvider = childItemProvider;
        }

        @Override
        public int getChildCount(HierarchicalQuery<TYPE, Void> query) {
            return Math.toIntExact(fetchChildrenFromBackEnd(query).count());
        }

        @Override
        public boolean hasChildren(TYPE item) {
            return getChildCount(new HierarchicalQuery<>(null, item)) > 0;
        }

        @Override
        protected Stream<TYPE> fetchChildrenFromBackEnd(HierarchicalQuery<TYPE, Void> query) {
            return childItemProvider.apply(query.getParent()).stream()/*.skip(query.getOffset()).limit(query.getLimit())*/;
        }
    }
    public HelloWorldView() {
        setSizeFull();

        TreeGrid<String> treeGrid = new TreeGrid<>();
        treeGrid.setWidthFull();

        final ArrayList<String> rootEnverNodes = new ArrayList<>();

        final ValueProvider<String, Collection<String>> childItemProvider = enverNode -> {
            if (enverNode == null){ // root items
                return rootEnverNodes;
            } else {
                return new ArrayList<>(); // assume no children
            }
        };

        TreeDataProvider<String> treeDataProvider = new TreeDataProvider<>(childItemProvider);

        for (int i=0; i < 600; i++){
            String enverNode = Integer.toString(i);
            rootEnverNodes.add(enverNode);
        }

        treeGrid.setDataProvider(treeDataProvider);
        ValueProvider<String, String> textProvider = (String::toString);
        treeGrid.addHierarchyColumn(textProvider);

        add(treeGrid);
    }

}

When scrolling, the data provider is trying to deliver the second page, but -it is buggy!- the same page is delivered again.

I get: There seems to be an error in Vaadin Grid: Cannot read properties of undefined (reading 'filter') Please submit an issue to https://github.com/vaadin/flow-components/issues/new/choose (Firefox)

or it shows There seems to be an error in Vaadin Grid: items is undefined Please submit an issue to https://github.com/vaadin/flow-components/issues/new/choose (Chrome)

Expected outcome

I expect the Grid or Flow warn me about duplicate items. I expect there to be no error and no white / empty rows.

A mistake in using the framework should surely not trigger a warning like 'Cannot read properties of undefined (reading 'filter') Please submit an issue' or 'items is undefined Please submit an issue'. there should be a way to detect that there's a duplicate item in the data set either the identical item was added (or returned by the data provider) or it was otherwise 'a.equals(b)' for two items a and b. If such a check is costly at run-time then maybe have a documented debug option.

Certainly empty rows mask away the underlying problem. Interestingly, Vaadin 14.3.1 would show the same items again as mentioned above - so here this is more of a hint to the programmer what mistake they must have made. Maybe instead of wrong warnings a mechanism could be created instead that hints the programmer what he did wrong, as white lines (unknown to Vaadin outsiders) are most often the outcome of duplicate items.

Minimal reproducible example

See above.

Steps to reproduce

See above.

Environment

Vaadin version(s): 23.2.2 OS: not applicable

Browsers

Issue is not browser related

enver-haase commented 1 year ago

This was discovered when dealing with #3713 .

enver-haase commented 2 months ago

Ping...?