Updated Command to allow client_response. First implementation or UserProfile control

This commit is contained in:
2025-11-11 12:07:00 +01:00
parent c641f3fd63
commit cba4f2aab4
11 changed files with 166 additions and 26 deletions

View File

@@ -1,15 +1,48 @@
from fasthtml.components import *
from myfasthtml.controls.BaseCommands import BaseCommands
from myfasthtml.controls.helpers import Ids, mk
from myfasthtml.core.instances import SingleInstance
from myfasthtml.core.commands import Command
from myfasthtml.core.instances import SingleInstance, InstancesManager
from myfasthtml.core.utils import retrieve_user_info
from myfasthtml.icons.material import dark_mode_filled, person_outline_sharp
from myfasthtml.icons.material_p1 import light_mode_filled, alternate_email_filled
class UserProfileState:
def __init__(self, owner):
self._owner = owner
self._session = owner.get_session()
self.theme = "light"
def load(self):
user_info = retrieve_user_info(self._session)
user_settings = user_info.get("user_settings", {})
for k, v in user_settings.items():
if hasattr(self, k):
setattr(self, k, v)
def save(self):
user_settings = {k: v for k, v in self.__dict__.items() if not k.startswith("_")}
auth_proxy = InstancesManager.get_auth_proxy()
auth_proxy.save_user_info(self._session["access_token"], {"user_settings": user_settings})
class Commands(BaseCommands):
def update_dark_mode(self):
return Command("UpdateDarkMode", "Set the dark mode", self._owner.update_dark_mode).htmx(target=None)
class UserProfile(SingleInstance):
def __init__(self, session):
super().__init__(session, Ids.UserProfile)
self._state = UserProfileState(self)
self._commands = Commands(self)
def update_dark_mode(self, client_response):
self._state.theme = client_response.get("theme", "light")
self._state.save()
def render(self):
user_info = retrieve_user_info(self._session)
@@ -29,14 +62,15 @@ class UserProfile(SingleInstance):
cls="dropdown dropdown-end"
)
@staticmethod
def mk_dark_mode():
def mk_dark_mode(self):
return Label(
Input(type="checkbox",
name='theme',
aria_label='Dark',
value='dark',
cls='theme-controller'),
mk.mk(Input(type="checkbox",
name='theme',
aria_label='Dark',
value="dark",
checked='true' if self._state.theme == 'dark' else None,
cls='theme-controller'),
command=self._commands.update_dark_mode()),
light_mode_filled,
dark_mode_filled,
cls="toggle text-base-content"