import { Github, Loader2, Save } from "lucide-react";
import { Button } from "../ui/button";
import { Card } from "../ui/card";
import { Input } from "../ui/input";
import { Label } from "../ui/label";
import { Link } from "react-router";
import { getCredentialsStatus, testCredentials } from "~/lib/api";
import { useState } from "react";
import { toast } from "sonner";
import MealRegistration from "./registration";
export default function BJRepas() {
const credentials = getSavedCredentials();
if (credentials) {
return ;
}
const [username, setUsername] = useState("");
const [password, setPassword] = useState("");
let intervalId: NodeJS.Timeout | null = null;
const [isLoading, setLoading] = useState(false);
const handleSaveCredentials = async () => {
try {
if (!username || !password) {
toast.error(
"Veuillez renseigner vos identifiants BJColle avant de continuer."
);
return;
}
await testCredentials(username, password);
setLoading(true);
// Try every second to check if the credentials are valid
intervalId = setInterval(async () => {
try {
const res = await getCredentialsStatus();
// Credentials are valid
if (res.authenticated === "SUCCESS") {
saveCredentials(username, password);
toast.success("Vos identifiants ont été enregistrés avec succès.");
setLoading(false);
if (intervalId) clearInterval(intervalId);
// Reload the page to reflect the changes
window.location.reload();
}
// Credentials are invalid
if (res.authenticated === "ERROR") {
toast.error(
"Identifiants invalides. Veuillez vérifier votre nom d'utilisateur et mot de passe."
);
setUsername("");
setPassword("");
setLoading(false);
if (intervalId) clearInterval(intervalId);
}
} catch (error) {
console.error("Error testing credentials:", error);
}
}, 1000);
} catch (error) {
console.error("Error testing credentials:", error);
toast.error(
"Une erreur est survenue lors de la validation des identifiants. Veuillez réessayer plus tard."
);
}
};
return (
Inscription aux repas
Pour vous inscrire aux repas (via BJ Repas), veuillez renseigner vos
identifiants BJColle ci-dessous.
Vos identifiants sont enregistrés localement sur votre appareil et ne
sont jamais stockés sur nos serveurs.
Pour en savoir plus sur la sécurité et la confidentialité, consultez
le code source du projet.{" "}
);
}
const saveCredentials = (username: string, password: string) => {
localStorage.setItem("bj_username", username);
localStorage.setItem("bj_password", password);
};
export const getSavedCredentials = () => {
const username = localStorage.getItem("bj_username");
const password = localStorage.getItem("bj_password");
if (username && password) {
return { username, password };
}
return null;
};