Désactivation du bouton de connexion pendant une connexion

This commit is contained in:
Emmy D'Anello 2024-12-10 08:19:54 +01:00
parent 48845c70c2
commit 4da75e310e
Signed by: ynerant
GPG Key ID: 3A75C55819C8CF85
2 changed files with 19 additions and 9 deletions

View File

@ -7,7 +7,7 @@ import { Appbar, Button, Dialog, Portal, Surface, Text, TextInput } from "react-
export default function Login() { export default function Login() {
const router = useRouter() const router = useRouter()
const [isLoggedIn, setIsLoggedIn] = useState(SecureStore.getItem("apiToken") !== null) const [isLoggedIn, setIsLoggedIn] = useState(SecureStore.getItem("apiToken") !== null)
console.log(SecureStore.getItem("apiToken")) const [loggingIn, setLoggingIn] = useState(false)
const [name, setName] = useState(SecureStore.getItem('apiName') ?? "") const [name, setName] = useState(SecureStore.getItem('apiName') ?? "")
const [password, setPassword] = useState("") const [password, setPassword] = useState("")
const [errorDialogVisible, setErrorDialogVisible] = useState(false) const [errorDialogVisible, setErrorDialogVisible] = useState(false)
@ -20,6 +20,9 @@ export default function Login() {
const hideErrorDialog = () => setErrorDialogVisible(false) const hideErrorDialog = () => setErrorDialogVisible(false)
async function login() { async function login() {
if (loggingIn)
return
setLoggingIn(true)
const resp = await fetch("http://192.168.1.198:3000/auth/login/", { const resp = await fetch("http://192.168.1.198:3000/auth/login/", {
method: "POST", method: "POST",
headers: { "Content-Type": "application/json" }, headers: { "Content-Type": "application/json" },
@ -30,6 +33,7 @@ export default function Login() {
setErrorDialogVisible(true) setErrorDialogVisible(true)
setErrorTitle("Erreur") setErrorTitle("Erreur")
setErrorText("Une erreur inconnue est survenue lors de la connexion. Veuillez réessayer plus tard. " + err) setErrorText("Une erreur inconnue est survenue lors de la connexion. Veuillez réessayer plus tard. " + err)
setLoggingIn(false)
}) })
if (!resp) if (!resp)
return return
@ -37,8 +41,10 @@ export default function Login() {
setErrorDialogVisible(true) setErrorDialogVisible(true)
setErrorTitle(resp.error) setErrorTitle(resp.error)
setErrorText(resp.message) setErrorText(resp.message)
setLoggingIn(false)
return return
} }
setLoggingIn(false)
SecureStore.setItem("apiName", name) SecureStore.setItem("apiName", name)
if (Platform.OS !== "web") { if (Platform.OS !== "web") {
// Le stockage navigateur n'est pas sûr, on évite de stocker un mot de passe à l'intérieur // Le stockage navigateur n'est pas sûr, on évite de stocker un mot de passe à l'intérieur
@ -80,7 +86,13 @@ export default function Login() {
onSubmitEditing={login} onSubmitEditing={login}
secureTextEntry={true} secureTextEntry={true}
style={{ margin: 8 }} /> style={{ margin: 8 }} />
<Button onPress={login} mode="contained" icon="login" style={{ margin: 8 }}> <Button
key={loggingIn ? "disabledLoginButton" : "loginButton"}
onPress={login}
mode={"contained"}
icon="login"
disabled={loggingIn}
style={{ margin: 8 }}>
Se connecter Se connecter
</Button> </Button>
<Portal> <Portal>

View File

@ -4,15 +4,13 @@ import { Platform } from 'react-native'
import { useEffect } from 'react' import { useEffect } from 'react'
const BACKGROUND_FETCH_TASK = "background-fetch" const BACKGROUND_FETCH_TASK = "background-fetch"
const BACKGROUND_FETCH_INTERVAL = 20 const BACKGROUND_FETCH_INTERVAL = 60
async function backgroundUpdate() { async function backgroundUpdate() {
async () => { const now = Date.now()
const now = Date.now() console.log(`Got background fetch call at date: ${new Date(now).toISOString()}`)
console.log(`Got background fetch call at date: ${new Date(now).toISOString()}`) // Be sure to return the successful result type!
// Be sure to return the successful result type! return BackgroundFetch.BackgroundFetchResult.NewData
return BackgroundFetch.BackgroundFetchResult.NewData
}
} }
TaskManager.defineTask(BACKGROUND_FETCH_TASK, backgroundUpdate) TaskManager.defineTask(BACKGROUND_FETCH_TASK, backgroundUpdate)