|
| 1 | +from typing import TYPE_CHECKING |
| 2 | + |
| 3 | +from django.urls import reverse |
| 4 | +from dominate.tags import ( |
| 5 | + a, |
| 6 | + button, |
| 7 | + div, |
| 8 | + fieldset, |
| 9 | + form, |
| 10 | + input_, |
| 11 | + label, |
| 12 | + legend, |
| 13 | + li, |
| 14 | + p, |
| 15 | + section, |
| 16 | + ul, |
| 17 | +) |
| 18 | + |
| 19 | +from apps.webui.components import common_styles |
| 20 | +from apps.webui.components.forms_common import csrf_hidden_input |
| 21 | +from apps.webui.components.layout import page |
| 22 | + |
| 23 | +from ... import lichess_api |
| 24 | +from ...models import LichessCorrespondenceGameDaysChoice |
| 25 | +from ..misc_ui import detach_lichess_account_form |
| 26 | +from ..svg_icons import ICON_SVG_CREATE, ICON_SVG_LOG_IN |
| 27 | + |
| 28 | +if TYPE_CHECKING: |
| 29 | + from django.http import HttpRequest |
| 30 | + |
| 31 | + from ...models import LichessAccessToken |
| 32 | + |
| 33 | + |
| 34 | +def lichess_no_account_linked_page( |
| 35 | + *, |
| 36 | + request: "HttpRequest", |
| 37 | +) -> str: |
| 38 | + return page( |
| 39 | + section( |
| 40 | + form( |
| 41 | + csrf_hidden_input(request), |
| 42 | + p("Click here to log in to Lichess"), |
| 43 | + button( |
| 44 | + "Log in via Lichess", |
| 45 | + " ", |
| 46 | + ICON_SVG_LOG_IN, |
| 47 | + type="submit", |
| 48 | + cls=common_styles.BUTTON_CLASSES, |
| 49 | + ), |
| 50 | + action=reverse("lichess_bridge:oauth2_start_flow"), |
| 51 | + method="POST", |
| 52 | + ), |
| 53 | + cls="text-slate-50", |
| 54 | + ), |
| 55 | + request=request, |
| 56 | + title="Lichess - no account linked", |
| 57 | + ) |
| 58 | + |
| 59 | + |
| 60 | +async def lichess_account_linked_homepage( |
| 61 | + *, |
| 62 | + request: "HttpRequest", |
| 63 | + access_token: "LichessAccessToken", |
| 64 | +) -> str: |
| 65 | + me = await lichess_api.get_my_account(access_token=access_token) |
| 66 | + ongoing_games = await lichess_api.get_my_ongoing_games(access_token=access_token) |
| 67 | + |
| 68 | + return page( |
| 69 | + div( |
| 70 | + section( |
| 71 | + f"Hello {me.username}!", |
| 72 | + ul( |
| 73 | + *[li(game.gameId) for game in ongoing_games], |
| 74 | + ), |
| 75 | + p( |
| 76 | + a( |
| 77 | + "Create a new game", |
| 78 | + href=reverse("lichess_bridge:create_correspondence_game"), |
| 79 | + cls=common_styles.BUTTON_CLASSES, |
| 80 | + ), |
| 81 | + ), |
| 82 | + cls="text-slate-50", |
| 83 | + ), |
| 84 | + div( |
| 85 | + detach_lichess_account_form(request), |
| 86 | + cls="mt-4", |
| 87 | + ), |
| 88 | + cls="w-full mx-auto bg-slate-900 min-h-48 " |
| 89 | + "md:max-w-3xl xl:max-w-7xl xl:border xl:rounded-md xl:border-neutral-800", |
| 90 | + ), |
| 91 | + request=request, |
| 92 | + title="Lichess - account linked", |
| 93 | + ) |
| 94 | + |
| 95 | + |
| 96 | +def lichess_correspondence_game_creation_page( |
| 97 | + request: "HttpRequest", form_errors: dict |
| 98 | +) -> str: |
| 99 | + return page( |
| 100 | + div( |
| 101 | + section( |
| 102 | + form( |
| 103 | + csrf_hidden_input(request), |
| 104 | + div( |
| 105 | + fieldset( |
| 106 | + legend("Days per turn."), |
| 107 | + ( |
| 108 | + p(form_errors["days_per_turn"], cls="text-red-600 ") |
| 109 | + if "days_per_turn" in form_errors |
| 110 | + else "" |
| 111 | + ), |
| 112 | + div( |
| 113 | + *[ |
| 114 | + div( |
| 115 | + input_( |
| 116 | + id=f"days-per-turn-{value}", |
| 117 | + type="radio", |
| 118 | + name="days_per_turn", |
| 119 | + value=value, |
| 120 | + checked=( |
| 121 | + value |
| 122 | + == LichessCorrespondenceGameDaysChoice.THREE_DAYS.value # type: ignore[attr-defined] |
| 123 | + ), |
| 124 | + ), |
| 125 | + label( |
| 126 | + display, html_for=f"days-per-turn-{value}" |
| 127 | + ), |
| 128 | + ) |
| 129 | + for value, display in LichessCorrespondenceGameDaysChoice.choices |
| 130 | + ], |
| 131 | + cls="flex gap-3", |
| 132 | + ), |
| 133 | + cls="block text-sm font-bold mb-2", |
| 134 | + ), |
| 135 | + input_( |
| 136 | + id="days-per-turn", |
| 137 | + cls="shadow appearance-none border rounded w-full py-2 px-3 text-gray-700 leading-tight focus:outline-none focus:shadow-outline", |
| 138 | + ), |
| 139 | + cls="mb-4", |
| 140 | + ), |
| 141 | + button( |
| 142 | + "Create", |
| 143 | + " ", |
| 144 | + ICON_SVG_CREATE, |
| 145 | + type="submit", |
| 146 | + cls=common_styles.BUTTON_CLASSES, |
| 147 | + ), |
| 148 | + action=reverse("lichess_bridge:create_correspondence_game"), |
| 149 | + method="POST", |
| 150 | + ), |
| 151 | + cls="text-slate-50", |
| 152 | + ), |
| 153 | + div( |
| 154 | + detach_lichess_account_form(request), |
| 155 | + cls="mt-4", |
| 156 | + ), |
| 157 | + cls="w-full mx-auto bg-slate-900 min-h-48 " |
| 158 | + "md:max-w-3xl xl:max-w-7xl xl:border xl:rounded-md xl:border-neutral-800", |
| 159 | + ), |
| 160 | + request=request, |
| 161 | + title="Lichess - new correspondence game", |
| 162 | + ) |
0 commit comments