-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
ProfilePage: Tabs and Telegram Stuff (#998)
Signed-off-by: Aleksandr Muravja <[email protected]>
- Loading branch information
Aleksandr Muravja
committed
Jan 21, 2023
1 parent
b77b1a5
commit 9dc9c1c
Showing
7 changed files
with
435 additions
and
235 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
This file was deleted.
Oops, something went wrong.
121 changes: 121 additions & 0 deletions
121
src/main/java/pm/axe/ui/pages/user/profile/ProfilePage.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
20
src/main/java/pm/axe/ui/pages/user/profile/tabs/DangerZoneTab.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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")); | ||
} | ||
} |
Oops, something went wrong.