Skip to content

Commit

Permalink
ProfilePage: Tabs and Telegram Stuff (#998)
Browse files Browse the repository at this point in the history
Signed-off-by: Aleksandr Muravja <[email protected]>
  • Loading branch information
Aleksandr Muravja committed Jan 21, 2023
1 parent b77b1a5 commit 9dc9c1c
Show file tree
Hide file tree
Showing 7 changed files with 435 additions and 235 deletions.
7 changes: 7 additions & 0 deletions src/main/java/pm/axe/internal/HasTabInit.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package pm.axe.internal;

import pm.axe.db.models.User;

public interface HasTabInit {
void tabInit(User user);
}
235 changes: 0 additions & 235 deletions src/main/java/pm/axe/ui/pages/user/ProfilePage.java

This file was deleted.

121 changes: 121 additions & 0 deletions src/main/java/pm/axe/ui/pages/user/profile/ProfilePage.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
package pm.axe.ui.pages.user.profile;

import com.vaadin.flow.component.Component;
import com.vaadin.flow.component.dependency.CssImport;
import com.vaadin.flow.component.html.Div;
import com.vaadin.flow.component.html.H2;
import com.vaadin.flow.component.html.Span;
import com.vaadin.flow.component.icon.VaadinIcon;
import com.vaadin.flow.component.tabs.Tab;
import com.vaadin.flow.component.tabs.TabVariant;
import com.vaadin.flow.component.tabs.Tabs;
import com.vaadin.flow.component.tabs.TabsVariant;
import com.vaadin.flow.router.BeforeEnterEvent;
import com.vaadin.flow.router.BeforeEnterObserver;
import com.vaadin.flow.router.PageTitle;
import com.vaadin.flow.router.Route;
import com.vaadin.flow.spring.annotation.SpringComponent;
import com.vaadin.flow.spring.annotation.UIScope;
import lombok.RequiredArgsConstructor;
import pm.axe.Endpoint;
import pm.axe.db.models.User;
import pm.axe.internal.HasTabInit;
import pm.axe.session.AxeSession;
import pm.axe.ui.MainView;
import pm.axe.ui.layouts.AxeCompactLayout;
import pm.axe.ui.pages.user.LoginPage;
import pm.axe.ui.pages.user.profile.tabs.DangerZoneTab;
import pm.axe.ui.pages.user.profile.tabs.ProfileTab;
import pm.axe.ui.pages.user.profile.tabs.SecurityTab;
import pm.axe.ui.pages.user.profile.tabs.SettingsTab;

import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;

@SpringComponent
@UIScope
@RequiredArgsConstructor
@CssImport(value = "./css/profile_page.css")
@Route(value = Endpoint.UI.PROFILE_PAGE, layout = MainView.class)
@PageTitle("My Profile - Axe.pm")
public class ProfilePage extends AxeCompactLayout implements BeforeEnterObserver {
private final Tab profileTab = new Tab(VaadinIcon.USER.create(), new Span("Profile"));
private final Tab securityTab = new Tab(VaadinIcon.SHIELD.create(), new Span("Security"));
private final Tab settingsTab = new Tab(VaadinIcon.COG.create(), new Span("Settings"));
private final Tab dangerZoneTab = new Tab(VaadinIcon.FIRE.create(), new Span("Danger Zone"));
private final ProfileTab profileTabContent;
private final SecurityTab securityTabContent;
private final SettingsTab settingsTabContent;
private final DangerZoneTab dangerZoneTabContent;
private boolean pageAlreadyInitialized = false;
private User user;

private final Div content = new Div();
private final Map<Tab, Component> contentMap = new LinkedHashMap<>();

@Override
public void beforeEnter(BeforeEnterEvent event) {
if (event.isRefreshEvent()) return;
boundUserIfAny();
if (Objects.isNull(user)) {
event.forwardTo(LoginPage.class);
return;
}

if (!pageAlreadyInitialized) {
initPage();
pageAlreadyInitialized = true;
}
}

private void initPage() {
removeAll();
//content map
contentMap.clear();
contentMap.put(profileTab, profileTabContent);
contentMap.put(securityTab, securityTabContent);
contentMap.put(settingsTab, settingsTabContent);
contentMap.put(dangerZoneTab, dangerZoneTabContent);

//title
H2 title = new H2("My Profile");
title.setClassName("profile-title");

//tabs
//set icon on top on text
contentMap.forEach((tab, content) -> tab.addThemeVariants(TabVariant.LUMO_ICON_ON_TOP));
//init content
contentMap.forEach((tab, content) -> {
if (content instanceof HasTabInit) {
((HasTabInit) content).tabInit(user);
}
});
Tabs tabs = new Tabs();
tabs.addSelectedChangeListener(event -> setContent(event.getSelectedTab()));
contentMap.forEach((tab, content) -> tabs.add(tab));
tabs.addThemeVariants(TabsVariant.LUMO_EQUAL_WIDTH_TABS);
tabs.setWidthFull();

add(title, tabs, content);
}

private void boundUserIfAny() {
Optional<AxeSession> axeSession = AxeSession.getCurrent();
if (axeSession.isPresent()) {
if (axeSession.get().hasUser()) {
user = axeSession.get().getUser();
}
}
}

private void setContent(final Tab tab) {
content.removeAll();
if (tab == null) {
return;
}
Component tabContent = contentMap.get(tab);
content.add(tabContent);
}
}
20 changes: 20 additions & 0 deletions src/main/java/pm/axe/ui/pages/user/profile/tabs/DangerZoneTab.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package pm.axe.ui.pages.user.profile.tabs;

import com.vaadin.flow.component.html.Span;
import com.vaadin.flow.component.orderedlayout.VerticalLayout;
import com.vaadin.flow.spring.annotation.SpringComponent;
import com.vaadin.flow.spring.annotation.UIScope;
import lombok.RequiredArgsConstructor;
import pm.axe.db.models.User;
import pm.axe.internal.HasTabInit;

@RequiredArgsConstructor
@SpringComponent
@UIScope
public class DangerZoneTab extends VerticalLayout implements HasTabInit {

@Override
public void tabInit(final User user) {
add(new Span("This is Danger Zone"));
}
}
Loading

0 comments on commit 9dc9c1c

Please sign in to comment.