Added refresh button for NodeList

This commit is contained in:
Andrey Terentev 2024-02-19 16:01:08 +07:00 committed by Andrey Terentev
parent d40222d048
commit 9650ecd358
3 changed files with 44 additions and 14 deletions

View File

@ -4,6 +4,7 @@ import com.vaadin.flow.component.Html;
import com.vaadin.flow.component.button.Button; import com.vaadin.flow.component.button.Button;
import com.vaadin.flow.component.button.ButtonVariant; import com.vaadin.flow.component.button.ButtonVariant;
import com.vaadin.flow.component.dialog.Dialog; import com.vaadin.flow.component.dialog.Dialog;
import com.vaadin.flow.component.grid.ColumnTextAlign;
import com.vaadin.flow.component.grid.Grid; import com.vaadin.flow.component.grid.Grid;
import com.vaadin.flow.component.html.H2; import com.vaadin.flow.component.html.H2;
import com.vaadin.flow.component.icon.Icon; import com.vaadin.flow.component.icon.Icon;
@ -13,28 +14,29 @@ import com.vaadin.flow.component.orderedlayout.VerticalLayout;
import com.vaadin.flow.component.textfield.TextField; import com.vaadin.flow.component.textfield.TextField;
import com.vaadin.flow.data.value.ValueChangeMode; import com.vaadin.flow.data.value.ValueChangeMode;
import lombok.Setter; import lombok.Setter;
import ru.dragonestia.picker.api.repository.NodeRepository;
import ru.dragonestia.picker.api.repository.response.type.RNode; import ru.dragonestia.picker.api.repository.response.type.RNode;
import java.util.List; import java.util.List;
import java.util.function.Consumer; import java.util.function.Consumer;
public class NodeList extends VerticalLayout { public class NodeList extends VerticalLayout implements RefreshableTable {
private final NodeRepository nodeRepository;
private final Grid<RNode> nodesGrid; private final Grid<RNode> nodesGrid;
private final TextField searchField; private final TextField searchField;
private List<RNode> cachedNodes; private List<RNode> cachedNodes;
@Setter private Consumer<String> removeMethod; @Setter private Consumer<String> removeMethod;
public NodeList(List<RNode> nodes) { public NodeList(NodeRepository nodeRepository) {
super(); super();
this.nodeRepository = nodeRepository;
cachedNodes = nodes;
add(new H2("Nodes")); add(new H2("Nodes"));
add(searchField = createSearchField()); add(searchField = createSearchField());
add(nodesGrid = createGrid()); add(nodesGrid = createGrid());
update(nodes); refresh();
} }
private TextField createSearchField() { private TextField createSearchField() {
@ -60,7 +62,8 @@ public class NodeList extends VerticalLayout {
grid.addColumn(RNode::getId).setHeader("Identifier").setSortable(true); grid.addColumn(RNode::getId).setHeader("Identifier").setSortable(true);
grid.addColumn(node -> node.getMode().getName()).setHeader("Mode").setSortable(true); grid.addColumn(node -> node.getMode().getName()).setHeader("Mode").setSortable(true);
grid.addComponentColumn(this::createManageButtons).setFrozenToEnd(true); grid.addComponentColumn(this::createManageButtons).setFrozenToEnd(true)
.setTextAlign(ColumnTextAlign.END).setHeader(createRefreshButton());
grid.setMultiSort(true, Grid.MultiSortPriority.APPEND); grid.setMultiSort(true, Grid.MultiSortPriority.APPEND);
return grid; return grid;
@ -124,14 +127,15 @@ public class NodeList extends VerticalLayout {
dialog.open(); dialog.open();
} }
public void update(List<RNode> nodes) {
cachedNodes = nodes;
applySearch(searchField.getValue());
}
private void removeNode(RNode node) { private void removeNode(RNode node) {
if (removeMethod != null) { if (removeMethod != null) {
removeMethod.accept(node.getId()); removeMethod.accept(node.getId());
} }
} }
@Override
public void refresh() {
cachedNodes = nodeRepository.all(NodeRepository.ALL_DETAILS);
applySearch(searchField.getValue());
}
} }

View File

@ -0,0 +1,26 @@
package ru.dragonestia.picker.cp.component;
import com.vaadin.flow.component.button.Button;
import com.vaadin.flow.component.button.ButtonVariant;
import com.vaadin.flow.component.icon.Icon;
import com.vaadin.flow.component.icon.VaadinIcon;
import com.vaadin.flow.component.notification.Notification;
public interface RefreshableTable {
default Button createRefreshButton() {
var button = new Button("Refresh", new Icon(VaadinIcon.REFRESH));
button.addThemeVariants(ButtonVariant.LUMO_CONTRAST);
button.addClickListener(event -> {
refresh();
var notification = new Notification("Refreshed!");
notification.setDuration(1000);
notification.setPosition(Notification.Position.BOTTOM_END);
notification.open();
});
return button;
}
void refresh();
}

View File

@ -33,7 +33,7 @@ public class NodesPage extends VerticalLayout {
add(nodeList = createNodeListElement()); add(nodeList = createNodeListElement());
nodeList.setRemoveMethod(nodeIdentifier -> { nodeList.setRemoveMethod(nodeIdentifier -> {
nodeRepository.remove(nodeIdentifier); nodeRepository.remove(nodeIdentifier);
nodeList.update(nodeRepository.all(NodeRepository.ALL_DETAILS)); nodeList.refresh();
}); });
} }
@ -45,12 +45,12 @@ public class NodesPage extends VerticalLayout {
} catch (ApiException ex) { } catch (ApiException ex) {
return new RegisterNode.Response(true, ex.getMessage()); return new RegisterNode.Response(true, ex.getMessage());
} finally { } finally {
nodeList.update(nodeRepository.all(NodeRepository.ALL_DETAILS)); nodeList.refresh();
} }
}); });
} }
protected NodeList createNodeListElement() { protected NodeList createNodeListElement() {
return new NodeList(nodeRepository.all(NodeRepository.ALL_DETAILS)); return new NodeList(nodeRepository);
} }
} }