import re import flet as ft from flet.core.page import RouteChangeEvent from pages.profile import profile from requests.board_games import get_sections, get_games from requests.auth import login as login_req, registration def root(page: ft.Page): def update_games(section): current_section.value = section games = get_games(None if section == "Все" else section) grid.controls.clear() for game in games: grid.controls.append( ft.Column([ ft.Text(str(game)), ft.Row([ ft.IconButton(icon=ft.icons.SHOPPING_CART), ft.IconButton(icon=ft.icons.FAVORITE), ]), ]) ) page.update() sections = [*get_sections(), "Все"] sections_buttons = [ft.Button(section, on_click=lambda e, s=section: update_games(s)) for section in sections] games = get_games() current_section = ft.Ref[str]() grid = ft.GridView( expand=1, runs_count=5, max_extent=200, child_aspect_ratio=1.0, spacing=5, run_spacing=5, ) for game in games: grid.controls.append( ft.Column([ ft.Text(str(game)), ft.Row([ ft.IconButton( icon=ft.Icons.SHOPPING_CART ), ft.IconButton( icon=ft.Icons.FAVORITE ) ]) ]) ) def go_to_login(): page.route = "/login" page.update() def go_to_register(): page.route = "/register" page.update() def go_to_profile(): page.route = "/profile" page.update() def logout(): page.client_storage.remove("user_id") page.clean() root(page) page.update() user_id = page.client_storage.get("user_id") page.add( ft.Row( [ ft.Text( "Весёлые кубики", color="#59799f", size=24, weight=ft.FontWeight.BOLD, ), ft.Row([ ft.Button( text="Вход", on_click=lambda e: go_to_login(), ), ft.Button( text="Регистрация", on_click=lambda e: go_to_register(), ), ]) if user_id is None else ft.Row([ ft.Button( "Профиль", on_click=lambda e: go_to_profile() ), ft.Button( text="Выйти", on_click=lambda e: logout(), ) ]) ], alignment=ft.MainAxisAlignment.SPACE_BETWEEN, # Adjust alignment if needed ), ft.Column( [ft.Row(sections_buttons, alignment=ft.MainAxisAlignment.CENTER)], alignment=ft.MainAxisAlignment.START ), grid ) def login(page): def close_banner(e): page.close(banner) action_button_style = ft.ButtonStyle(color=ft.Colors.BLUE) banner = ft.Banner( bgcolor=ft.Colors.AMBER_100, leading=ft.Icon(ft.Icons.WARNING_AMBER_ROUNDED, color=ft.Colors.AMBER, size=40), content=ft.Text( value="Ведены неверные данные пользователя", color=ft.Colors.BLACK, ), actions=[ ft.TextButton(text="Ок", style=action_button_style, on_click=close_banner), ], ) # Login button handler def login_clicked(e): email = email_input.value password = password_input.value user_id = login_req(email, password) if user_id is None: page.open(banner) else: page.client_storage.set("user_id", user_id) back_clicked(None) # Back button handler def back_clicked(e): page.go("/") page.update() # Email input field email_input = ft.TextField( label="Email", hint_text="Enter your email", width=300, ) # Password input field password_input = ft.TextField( label="Password", hint_text="Enter your password", password=True, can_reveal_password=True, width=300, ) # Login and Back buttons login_button = ft.ElevatedButton("Войти", on_click=login_clicked) back_button = ft.TextButton("Назад", on_click=back_clicked) page.horizontal_alignment=ft.CrossAxisAlignment.CENTER # Add components to the page page.add( ft.Column( [ ft.Text("Вход", size=24, weight=ft.FontWeight.BOLD), email_input, password_input, login_button, back_button ], alignment=ft.MainAxisAlignment.CENTER, horizontal_alignment=ft.CrossAxisAlignment.CENTER, ), ) def register(page): page.horizontal_alignment=ft.CrossAxisAlignment.CENTER def back_clicked(e): page.go("/") page.update() # Поля для ввода данных first_name_input = ft.TextField(label="Имя", hint_text="Введите ваше имя") last_name_input = ft.TextField(label="Фамилия", hint_text="Введите вашу фамилию") middle_name_input = ft.TextField(label="Отчество", hint_text="Введите ваше отчество (опционально)") age_input = ft.TextField(label="Возраст", hint_text="Введите ваш возраст", keyboard_type=ft.KeyboardType.NUMBER) phone_input = ft.TextField(label="Телефон", hint_text="Введите ваш телефон", keyboard_type=ft.KeyboardType.PHONE) email_input = ft.TextField(label="Электронная почта", hint_text="Введите вашу электронную почту", keyboard_type=ft.KeyboardType.EMAIL) password_input = ft.TextField(label="Пароль", hint_text="Введите ваш пароль", password=True, can_reveal_password=True) # Обработчик кнопки регистрации def register_clicked(e): first_name = first_name_input.value.strip() last_name = last_name_input.value.strip() middle_name = middle_name_input.value.strip() age = int(age_input.value.strip()) phone = phone_input.value.strip() email = email_input.value.strip() password = password_input.value user_id = registration(first_name, last_name, middle_name, age, phone, email, password) page.client_storage.set("user_id", user_id) page.go("/") # Кнопка для регистрации register_button = ft.ElevatedButton("Зарегистрироваться", on_click=register_clicked) # Добавление элементов на страницу back_button = ft.TextButton("Назад", on_click=back_clicked) page.add( ft.Column( [ ft.Text("Регистрация", size=24, weight=ft.FontWeight.BOLD), first_name_input, last_name_input, middle_name_input, age_input, phone_input, email_input, password_input, register_button, back_button ], alignment=ft.MainAxisAlignment.START, horizontal_alignment=ft.CrossAxisAlignment.CENTER, spacing=10, width=600 ) ) def main(page: ft.Page): def route_change(e): route = e.route page.clean() if route == "/login": login(page) elif route == "/register": register(page) elif route == "/profile": profile(page) else: root(page) page.update() page.title = "Весёлые кубики" page.on_route_change = route_change route_change(RouteChangeEvent(page.route)) ft.app(main)