- Fix: Configuration Docker pour éviter l'écrasement de node_modules - Fix: Script build-apk.sh installe correctement les dépendances dev - Fix: Adaptation du code pour export statique (Suspense, useSearchParams) - Fix: Correction type Accordion
111 lines
3.4 KiB
TypeScript
111 lines
3.4 KiB
TypeScript
"use client";
|
|
|
|
import { useState, useEffect, useMemo } from "react";
|
|
import { config } from "@/lib/config";
|
|
|
|
export interface ClientData {
|
|
bungalowNumber: string;
|
|
wifiName: string;
|
|
wifiPassword: string;
|
|
gerantMessage: string;
|
|
}
|
|
|
|
const STORAGE_KEY = "clientData";
|
|
|
|
function loadFromStorage(): ClientData | null {
|
|
if (typeof window === "undefined") return null;
|
|
|
|
try {
|
|
const stored = localStorage.getItem(STORAGE_KEY);
|
|
if (stored) {
|
|
return JSON.parse(stored);
|
|
}
|
|
} catch (error) {
|
|
console.error("Erreur lors du chargement depuis localStorage:", error);
|
|
}
|
|
return null;
|
|
}
|
|
|
|
export function useClientData() {
|
|
// Utiliser window.location pour l'export statique (compatible)
|
|
const [token, setToken] = useState<string | null>(null);
|
|
// Charger immédiatement depuis localStorage pour éviter le délai
|
|
const [clientData, setClientData] = useState<ClientData | null>(() => loadFromStorage());
|
|
const [loading, setLoading] = useState(true);
|
|
|
|
useEffect(() => {
|
|
// Récupérer le token depuis l'URL (compatible export statique)
|
|
if (typeof window !== "undefined") {
|
|
const urlParams = new URLSearchParams(window.location.search);
|
|
setToken(urlParams.get("token"));
|
|
}
|
|
}, []);
|
|
|
|
useEffect(() => {
|
|
if (token === null && typeof window !== "undefined") {
|
|
// Attendre que le token soit récupéré
|
|
const urlParams = new URLSearchParams(window.location.search);
|
|
setToken(urlParams.get("token"));
|
|
return;
|
|
}
|
|
|
|
const loadClientData = async () => {
|
|
// 1. Charger d'abord depuis localStorage pour un affichage immédiat
|
|
const storedData = loadFromStorage();
|
|
if (storedData) {
|
|
setClientData(storedData);
|
|
setLoading(false);
|
|
}
|
|
|
|
// 2. Vérifier s'il y a un token dans l'URL
|
|
|
|
if (token) {
|
|
try {
|
|
// 3. Charger les données depuis l'API pour mettre à jour
|
|
const response = await fetch(`/api/client/${token}`);
|
|
if (response.ok) {
|
|
const data = await response.json();
|
|
const clientInfo: ClientData = {
|
|
bungalowNumber: data.bungalowNumber,
|
|
wifiName: data.wifiName,
|
|
wifiPassword: data.wifiPassword,
|
|
gerantMessage: data.gerantMessage,
|
|
};
|
|
|
|
// 4. Sauvegarder dans localStorage
|
|
if (typeof window !== "undefined") {
|
|
localStorage.setItem(STORAGE_KEY, JSON.stringify(clientInfo));
|
|
}
|
|
setClientData(clientInfo);
|
|
} else {
|
|
// Token invalide, garder les données sauvegardées si disponibles
|
|
if (!storedData) {
|
|
setClientData(null);
|
|
}
|
|
}
|
|
} catch (error) {
|
|
console.error("Erreur lors du chargement des données client:", error);
|
|
// En cas d'erreur, garder les données sauvegardées si disponibles
|
|
if (!storedData) {
|
|
setClientData(null);
|
|
}
|
|
}
|
|
}
|
|
|
|
setLoading(false);
|
|
};
|
|
|
|
loadClientData();
|
|
}, [token]);
|
|
|
|
// Retourner les données client ou les valeurs par défaut
|
|
return useMemo(() => ({
|
|
bungalowNumber: clientData?.bungalowNumber || config.bungalowNumber,
|
|
wifiName: clientData?.wifiName || config.wifiName,
|
|
wifiPassword: clientData?.wifiPassword || config.wifiPassword,
|
|
gerantMessage: clientData?.gerantMessage || config.gerantMessage,
|
|
loading,
|
|
}), [clientData, loading]);
|
|
}
|
|
|