|
|
|
@ -1,6 +1,16 @@
|
|
|
|
|
package com.iqball.app.page
|
|
|
|
|
|
|
|
|
|
import android.util.Log
|
|
|
|
|
import androidx.compose.foundation.layout.Arrangement
|
|
|
|
|
import androidx.compose.foundation.layout.Column
|
|
|
|
|
import androidx.compose.foundation.layout.Spacer
|
|
|
|
|
import androidx.compose.foundation.layout.fillMaxSize
|
|
|
|
|
import androidx.compose.foundation.layout.fillMaxWidth
|
|
|
|
|
import androidx.compose.foundation.layout.height
|
|
|
|
|
import androidx.compose.foundation.layout.padding
|
|
|
|
|
import androidx.compose.material3.Button
|
|
|
|
|
import androidx.compose.material3.OutlinedTextField
|
|
|
|
|
import androidx.compose.material3.Surface
|
|
|
|
|
import androidx.compose.material3.Text
|
|
|
|
|
import androidx.compose.runtime.Composable
|
|
|
|
|
import androidx.compose.runtime.LaunchedEffect
|
|
|
|
@ -8,40 +18,90 @@ import androidx.compose.runtime.getValue
|
|
|
|
|
import androidx.compose.runtime.mutableStateOf
|
|
|
|
|
import androidx.compose.runtime.remember
|
|
|
|
|
import androidx.compose.runtime.setValue
|
|
|
|
|
import androidx.compose.ui.Alignment
|
|
|
|
|
import androidx.compose.ui.Modifier
|
|
|
|
|
import androidx.compose.ui.graphics.Color
|
|
|
|
|
import androidx.compose.ui.unit.dp
|
|
|
|
|
import androidx.compose.ui.unit.sp
|
|
|
|
|
import arrow.core.Either
|
|
|
|
|
import com.iqball.app.api.service.AuthService
|
|
|
|
|
import com.iqball.app.api.service.IQBallService
|
|
|
|
|
import com.iqball.app.session.Authentication
|
|
|
|
|
import kotlinx.coroutines.runBlocking
|
|
|
|
|
import kotlinx.datetime.LocalDateTime
|
|
|
|
|
import androidx.compose.ui.text.input.VisualTransformation
|
|
|
|
|
|
|
|
|
|
@Composable
|
|
|
|
|
fun RegisterPage(service: IQBallService) {
|
|
|
|
|
fun RegisterPage(service: AuthService, onLoginSuccess: (Authentication) -> Unit) {
|
|
|
|
|
var username by remember { mutableStateOf("") }
|
|
|
|
|
var email by remember { mutableStateOf("") }
|
|
|
|
|
var password by remember { mutableStateOf("") }
|
|
|
|
|
var errors by remember { mutableStateOf("") }
|
|
|
|
|
|
|
|
|
|
Surface(
|
|
|
|
|
color = Color.White,
|
|
|
|
|
modifier = Modifier.fillMaxSize()
|
|
|
|
|
|
|
|
|
|
var text by remember { mutableStateOf("No message !") }
|
|
|
|
|
|
|
|
|
|
LaunchedEffect(Unit) {
|
|
|
|
|
val result = service.login(AuthService.LoginRequest("maxime@mail.com", "123456"))
|
|
|
|
|
|
|
|
|
|
when (result) {
|
|
|
|
|
is Either.Left -> {
|
|
|
|
|
println("Error : " + result.value)
|
|
|
|
|
text = result.toString()
|
|
|
|
|
) {
|
|
|
|
|
Column(
|
|
|
|
|
modifier = Modifier
|
|
|
|
|
.padding(16.dp)
|
|
|
|
|
.fillMaxSize(),
|
|
|
|
|
verticalArrangement = Arrangement.Center,
|
|
|
|
|
horizontalAlignment = Alignment.CenterHorizontally
|
|
|
|
|
) {
|
|
|
|
|
Text(
|
|
|
|
|
text = "S'enregistrer",
|
|
|
|
|
fontSize = 28.sp,
|
|
|
|
|
color = Color.Black
|
|
|
|
|
)
|
|
|
|
|
Spacer(modifier = Modifier.height(16.dp))
|
|
|
|
|
errors?.let { message ->
|
|
|
|
|
Text(
|
|
|
|
|
text = message,
|
|
|
|
|
color = Color.Red,
|
|
|
|
|
fontSize = 14.sp,
|
|
|
|
|
modifier = Modifier.padding(vertical = 8.dp)
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
is Either.Right -> {
|
|
|
|
|
val token = result.value.token
|
|
|
|
|
val userDataResponse = service.getUserData(token)
|
|
|
|
|
Spacer(modifier = Modifier.height(16.dp))
|
|
|
|
|
OutlinedTextField(
|
|
|
|
|
value = username,
|
|
|
|
|
onValueChange = { username = it },
|
|
|
|
|
label = { Text("Nom d'utilisateur") },
|
|
|
|
|
modifier = Modifier.fillMaxWidth()
|
|
|
|
|
)
|
|
|
|
|
Spacer(modifier = Modifier.height(16.dp))
|
|
|
|
|
OutlinedTextField(
|
|
|
|
|
value = password,
|
|
|
|
|
onValueChange = { password = it },
|
|
|
|
|
label = { Text("Mot de passe") },
|
|
|
|
|
modifier = Modifier.fillMaxWidth()
|
|
|
|
|
)
|
|
|
|
|
Spacer(modifier = Modifier.height(16.dp))
|
|
|
|
|
OutlinedTextField(
|
|
|
|
|
value = email,
|
|
|
|
|
onValueChange = { email = it },
|
|
|
|
|
label = { Text("Email") },
|
|
|
|
|
modifier = Modifier.fillMaxWidth()
|
|
|
|
|
)
|
|
|
|
|
Button(onClick = {
|
|
|
|
|
runBlocking {
|
|
|
|
|
when (val response =
|
|
|
|
|
service.register(AuthService.RegisterRequest(username, email, password))) {
|
|
|
|
|
is Either.Left -> {
|
|
|
|
|
errors = response.value.toList().flatMap { entry -> entry.second.map { "${entry.first} : ${it}" } }.joinToString("\n")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
when (userDataResponse) {
|
|
|
|
|
is Either.Left -> println("Error User Data : " + userDataResponse.value)
|
|
|
|
|
is Either.Right -> println("Success User Data : " + userDataResponse.value)
|
|
|
|
|
is Either.Right -> {
|
|
|
|
|
onLoginSuccess(Authentication(response.value.token,LocalDateTime.parse(response.value.expirationDate)))
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
text = userDataResponse.toString()
|
|
|
|
|
}) {
|
|
|
|
|
Text(text = "Créer votre compte")
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
println(result)
|
|
|
|
|
Log.i("%", result.toString())
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Text(text = text)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|