connect login and room pages via logged in state

moving websocket into being managed by the room component.
if the partent would want access to "user signal" it can ask via
observer.
that would mean bidirectionality, and i already hear screeching of my
intuitions from react, but maybe that's ok and in react i would still
scope the websocket to the room page, plus callbacks
This commit is contained in:
efim
2023-04-26 22:28:22 +04:00
parent 90e886c62d
commit 9db42cb522
5 changed files with 82 additions and 76 deletions

View File

@@ -48,28 +48,35 @@ object JoinRoomComponent {
val (responsesStream, responseReceived) = EventStream.withCallback[FetchResponse[String]]
val submitButton = button(
"Join room",
onClick
.mapTo {
(roomNameVar.now(), roomPassVar.now(), nicknameVar.now(), nicknamePass.now())
}
.flatMap { case (roomName, roomPass, nickname, nicknamePass) =>
Fetch
.post(
"/api/login",
body = Requests.LogIn(
roomName,
nickname,
roomPass,
nicknamePass
)
)
.text
} --> responseReceived
)
def render(): Element = {
def render(loggedIn: Observer[Boolean]): Element = {
val submitButton = button(
"Join room",
onClick
.mapTo {
(roomNameVar.now(), roomPassVar.now(), nicknameVar.now(), nicknamePass.now())
}
.flatMap { case (roomName, roomPass, nickname, nicknamePass) =>
Fetch
.post(
"/api/login",
body = Requests.LogIn(
roomName,
nickname,
roomPass,
nicknamePass
)
)
.text.map { response =>
if (response.ok) {
loggedIn.onNext(true)
response
} else response
}
} --> responseReceived
)
div(
className := "flex flex-col h-full justify-center",
"Logging in:",
@@ -86,19 +93,21 @@ object JoinRoomComponent {
cls := "flex flex-col space-y-4 p-4 max-h-96 overflow-auto bg-gray-900",
children.command <-- responsesStream.recoverToTry.map {
case Success(response) =>
CollectionCommand.Append(
div(
{
CollectionCommand.Append(
div(
cls := "flex space-x-2 items-center",
code(cls := "text-green-500", "Status: "),
code(cls := "text-green-300", s"${response.status} ${response.statusText}")
),
div(
cls := "text-green-400 text-xs",
code(response.data)
div(
cls := "flex space-x-2 items-center",
code(cls := "text-green-500", "Status: "),
code(cls := "text-green-300", s"${response.status} ${response.statusText}")
),
div(
cls := "text-green-400 text-xs",
code(response.data)
)
)
)
)
}
case Failure(exception) =>
CollectionCommand.Append(
div(