refactor login box

This commit is contained in:
David Rodenkirchen 2024-08-28 14:58:46 +02:00
parent 2be572ea90
commit 3a20f6c976
4 changed files with 170 additions and 166 deletions

View File

@ -2,8 +2,7 @@ from rio import *
from src.ez_lan_manager import ConfigurationService
from src.ez_lan_manager.components.DesktopNavigationButton import DesktopNavigationButton
from src.ez_lan_manager.components.LoginBox import LoginBox
from src.ez_lan_manager.components.UserInfoBox import UserInfoBox
from src.ez_lan_manager.components.UserInfoAndLoginBox import UserInfoAndLoginBox
from src.ez_lan_manager.types.SessionStorage import SessionStorage
class DesktopNavigation(Component):
@ -14,13 +13,12 @@ class DesktopNavigation(Component):
await self.force_refresh()
def build(self) -> Component:
box = LoginBox() if self.session[SessionStorage].user_id is None else UserInfoBox()
lan_info = self.session[ConfigurationService].get_lan_info()
return Card(
Column(
Text(lan_info.name, align_x=0.5, margin_top=0.3, style=TextStyle(fill=self.session.theme.hud_color, font_size=2.5)),
Text(f"Edition {lan_info.iteration}", align_x=0.5, style=TextStyle(fill=self.session.theme.hud_color, font_size=1.2), margin_top=0.3, margin_bottom=2),
box,
UserInfoAndLoginBox(refresh_cb=self.refresh_cb),
DesktopNavigationButton("News", "./news"),
Spacer(min_height=1),
DesktopNavigationButton(f"Über {lan_info.name} {lan_info.iteration}", "./overview"),

View File

@ -1,85 +0,0 @@
from typing import Callable
from rio import Component, Column, Text, Row, Rectangle, Button, TextStyle, Color, Spacer, TextInput
from src.ez_lan_manager import UserService
from src.ez_lan_manager.types.SessionStorage import SessionStorage
class LoginBox(Component):
TEXT_STYLE = TextStyle(fill=Color.from_hex("02dac5"), font_size=0.9)
async def _on_login_pressed(self) -> None:
self.login_button.is_loading = True
user_name = self.user_name_input.text.lower()
if self.session[UserService].is_login_valid(user_name, self.password_input.text):
self.user_name_input.is_valid = True
self.password_input.is_valid = True
self.login_button.is_loading = False
await self.session[SessionStorage].set_user_id(self.session[UserService].get_user(user_name).user_id)
else:
self.user_name_input.is_valid = False
self.password_input.is_valid = False
self.login_button.is_loading = False
def build(self) -> Component:
self.user_name_input = TextInput(
text="",
label="Benutzername",
accessibility_label = "Benutzername",
min_height=0.5,
on_confirm=lambda _: self._on_login_pressed()
)
self.password_input = TextInput(
text="",
label="Passwort",
accessibility_label="Passwort",
is_secret=True,
on_confirm=lambda _: self._on_login_pressed()
)
self.login_button = Button(
Text("LOGIN", style=self.TEXT_STYLE, justify="center"),
shape="rectangle",
style="minor",
color="secondary",
margin_bottom=0.4,
on_press=self._on_login_pressed
)
self.register_button = Button(
Text("REG", style=self.TEXT_STYLE, justify="center"),
shape="rectangle",
style="minor",
color="secondary",
on_press=lambda: self.session.navigate_to("./register")
)
self.forgot_password_button = Button(
Text("LST PWD",style=self.TEXT_STYLE, justify="center"),
shape="rectangle",
style="minor",
color="secondary",
on_press=lambda: self.session.navigate_to("./forgot-password")
)
return Rectangle(
content=Column(
self.user_name_input,
self.password_input,
Column(
Row(
self.login_button
),
Row(
self.register_button,
Spacer(),
self.forgot_password_button,
proportions=(49, 2, 49)
)
),
spacing=0.4
),
fill=Color.TRANSPARENT,
min_height=8,
min_width=12,
align_x=0.5,
margin_top=0.3,
margin_bottom=2
)

View File

@ -0,0 +1,168 @@
from random import choice
from typing import Callable
from rio import Component, Column, Text, Row, Rectangle, Button, TextStyle, Color, Spacer, TextInput, Link
from src.ez_lan_manager import UserService
from src.ez_lan_manager.components.UserInfoBoxButton import UserInfoBoxButton
from src.ez_lan_manager.services.AccountingService import AccountingService
from src.ez_lan_manager.services.TicketingService import TicketingService
from src.ez_lan_manager.services.SeatingService import SeatingService
from src.ez_lan_manager.types.SessionStorage import SessionStorage
class StatusButton(Component):
STYLE = TextStyle(fill=Color.from_hex("121212"), font_size=0.5)
label: str
target_url: str
enabled: bool
def build(self) -> Component:
return Link(
content=Button(
content=Text(self.label, style=self.STYLE, justify="center"),
shape="rectangle",
style="major",
color="success" if self.enabled else "danger",
grow_x=True,
margin_left=0.6,
margin_right=0.6,
margin_top=0.6
),
target_url=self.target_url,
align_y=0.5,
grow_y=False
)
class UserInfoAndLoginBox(Component):
refresh_cb: Callable
TEXT_STYLE = TextStyle(fill=Color.from_hex("02dac5"), font_size=0.9)
show_login: bool = True
@staticmethod
def get_greeting() -> str:
return choice(["Grüße", "Hallo", "Willkommen", "Moin", "Ahoi"])
async def logout(self) -> None:
self.show_login = True
await self.session[SessionStorage].clear()
await self.force_refresh()
await self.refresh_cb()
async def _on_login_pressed(self) -> None:
self.login_button.is_loading = True
await self.login_button.force_refresh()
user_name = self.user_name_input.text.lower()
if self.session[UserService].is_login_valid(user_name, self.password_input.text):
self.user_name_input.is_valid = True
self.password_input.is_valid = True
self.login_button.is_loading = False
await self.session[SessionStorage].set_user_id(self.session[UserService].get_user(user_name).user_id)
self.show_login = False
await self.refresh_cb()
else:
self.user_name_input.is_valid = False
self.password_input.is_valid = False
self.login_button.is_loading = False
def build(self) -> Component:
self.user_name_input = TextInput(
text="",
label="Benutzername",
accessibility_label="Benutzername",
min_height=0.5,
on_confirm=lambda _: self._on_login_pressed()
)
self.password_input = TextInput(
text="",
label="Passwort",
accessibility_label="Passwort",
is_secret=True,
on_confirm=lambda _: self._on_login_pressed()
)
self.login_button = Button(
Text("LOGIN", style=self.TEXT_STYLE, justify="center"),
shape="rectangle",
style="minor",
color="secondary",
margin_bottom=0.4,
on_press=self._on_login_pressed
)
self.register_button = Button(
Text("REG", style=self.TEXT_STYLE, justify="center"),
shape="rectangle",
style="minor",
color="secondary",
on_press=lambda: self.session.navigate_to("./register")
)
self.forgot_password_button = Button(
Text("LST PWD", style=self.TEXT_STYLE, justify="center"),
shape="rectangle",
style="minor",
color="secondary",
on_press=lambda: self.session.navigate_to("./forgot-password")
)
if self.show_login:
return Rectangle(
content=Column(
self.user_name_input,
self.password_input,
Column(
Row(
self.login_button
),
Row(
self.register_button,
Spacer(),
self.forgot_password_button,
proportions=(49, 2, 49)
)
),
spacing=0.4
),
fill=Color.TRANSPARENT,
min_height=8,
min_width=12,
align_x=0.5,
margin_top=0.3,
margin_bottom=2
)
else:
user = self.session[UserService].get_user(self.session[SessionStorage].user_id)
if user is None:
print("ERROR")
a_s = self.session[AccountingService]
return Rectangle(
content=Column(
Text(f"{self.get_greeting()},", style=TextStyle(fill=Color.from_hex("02dac5"), font_size=0.9), justify="center"),
Text(f"{user.user_name}", style=TextStyle(fill=Color.from_hex("02dac5"), font_size=1.2), justify="center"),
Row(
StatusButton(label="TICKET", target_url="./buy_ticket",
enabled=self.session[TicketingService].get_user_ticket(user.user_id) is not None),
StatusButton(label="SITZPLATZ", target_url="./seating",
enabled=self.session[SeatingService].get_user_seat(user.user_id) is not None),
proportions=(50, 50),
grow_y=False
),
UserInfoBoxButton("Profil bearbeiten", "./edit-profile"),
UserInfoBoxButton(f"Guthaben: {a_s.make_euro_string_from_int(a_s.get_balance(user.user_id))}", "./account"),
Button(
content=Text("Ausloggen", style=TextStyle(fill=Color.from_hex("02dac5"), font_size=0.6)),
shape="rectangle",
style="minor",
color="secondary",
grow_x=True,
margin_left=0.6,
margin_right=0.6,
margin_top=0.6,
on_press=self.logout
)
),
fill=Color.TRANSPARENT,
min_height=8,
min_width=12,
align_x=0.5,
margin_top=0.3,
margin_bottom=2
)

View File

@ -1,77 +0,0 @@
from random import choice
from rio import Component, Column, Text, Row, Rectangle, Button, TextStyle, Color, Link
from src.ez_lan_manager import UserService, AccountingService, TicketingService, SeatingService
from src.ez_lan_manager.components.UserInfoBoxButton import UserInfoBoxButton
from src.ez_lan_manager.types.SessionStorage import SessionStorage
class StatusButton(Component):
STYLE = TextStyle(fill=Color.from_hex("121212"), font_size=0.5)
label: str
target_url: str
enabled: bool
def build(self) -> Component:
return Link(
content=Button(
content=Text(self.label, style=self.STYLE, justify="center"),
shape="rectangle",
style="major",
color="success" if self.enabled else "danger",
grow_x=True,
margin_left=0.6,
margin_right=0.6,
margin_top=0.6
),
target_url=self.target_url,
align_y=0.5,
grow_y=False
)
class UserInfoBox(Component):
@staticmethod
def get_greeting() -> str:
return choice(["Grüße", "Hallo", "Willkommen", "Moin", "Ahoi"])
async def logout(self) -> None:
await self.session[SessionStorage].clear()
await self.force_refresh()
def build(self) -> Component:
user = self.session[UserService].get_user(self.session[SessionStorage].user_id)
if user is None: # Noone logged in
return Text("")
a_s = self.session[AccountingService]
return Rectangle(
content=Column(
Text(f"{self.get_greeting()},", style=TextStyle(fill=Color.from_hex("02dac5"), font_size=0.9), justify="center"),
Text(f"{user.user_name}", style=TextStyle(fill=Color.from_hex("02dac5"), font_size=1.2), justify="center"),
Row(
StatusButton(label="TICKET", target_url="./buy_ticket", enabled=self.session[TicketingService].get_user_ticket(user.user_id) is not None),
StatusButton(label="SITZPLATZ", target_url="./seating", enabled=self.session[SeatingService].get_user_seat(user.user_id) is not None),
proportions=(50, 50),
grow_y=False
),
UserInfoBoxButton("Profil bearbeiten", "./edit-profile"),
UserInfoBoxButton(f"Guthaben: {a_s.make_euro_string_from_int(a_s.get_balance(user.user_id))}", "./account"),
Button(
content=Text("Ausloggen", style=TextStyle(fill=Color.from_hex("02dac5"), font_size=0.6)),
shape="rectangle",
style="minor",
color="secondary",
grow_x=True,
margin_left=0.6,
margin_right=0.6,
margin_top=0.6,
on_press=self.logout
)
),
fill=Color.TRANSPARENT,
min_height=8,
min_width=12,
align_x=0.5,
margin_top=0.3,
margin_bottom=2
)