diff --git a/src/front/Components/DesignSystem/DragAndDrop/index.tsx b/src/front/Components/DesignSystem/DragAndDrop/index.tsx index bc2a8021..a73ab98f 100644 --- a/src/front/Components/DesignSystem/DragAndDrop/index.tsx +++ b/src/front/Components/DesignSystem/DragAndDrop/index.tsx @@ -77,6 +77,10 @@ const mimeTypesAccepted: { [key: string]: IMimeTypes } = { extension: "txt", size: 104857600, // 100MB }, + "application/json": { + extension: "json", + size: 104857600, // 100MB + }, }; type IDocumentFileBase = { diff --git a/src/front/Components/Layouts/Login/StepImportProfile/index.tsx b/src/front/Components/Layouts/Login/StepImportProfile/index.tsx index 44ddfeb0..35d1e545 100644 --- a/src/front/Components/Layouts/Login/StepImportProfile/index.tsx +++ b/src/front/Components/Layouts/Login/StepImportProfile/index.tsx @@ -13,7 +13,6 @@ type IProps = { export default function StepImportProfile(props: IProps) { const { onSubmit, onBack, validationErrors } = props; - const [importedFile, setImportedFile] = useState(null); const [profileData, setProfileData] = useState(null); const [error, setError] = useState(""); @@ -22,44 +21,32 @@ export default function StepImportProfile(props: IProps) { return data && typeof data === "object" && data.version && - data.userData && - typeof data.userData === "object"; + data.user_data && + typeof data.user_data === "object"; }; - const handleFileUpload = useCallback(async (files: File[]) => { + const handleFileUpload = useCallback((files: File[]) => { const file = files[0]; if (!file) return; - // Validate file type - if (file.type !== "application/json") { - setError("Veuillez sélectionner un fichier JSON valide."); - return; - } - - // Validate file size (max 1MB) - if (file.size > 1024 * 1024) { - setError("Le fichier est trop volumineux. Taille maximum : 1MB."); - return; - } - - setImportedFile(file); setError(""); // Read and parse JSON - try { - const text = await file.text(); - const data = JSON.parse(text); - - // Validate profile structure - if (!validateProfileStructure(data)) { - setError("Le fichier ne contient pas un profil valide."); - return; - } + file.text() + .then((text) => { + const data = JSON.parse(text); + + // Validate profile structure + if (!validateProfileStructure(data)) { + setError('Le fichier ne contient pas un profil valide.'); + return; + } - setProfileData(data); - } catch (err) { - setError("Erreur lors de la lecture du fichier JSON."); - } + setProfileData(data); + }) + .catch((e) => { + setError(`Erreur lors de la lecture du fichier JSON: ${e}`); + }); }, []); const handleSubmit = useCallback(() => { @@ -100,15 +87,12 @@ export default function StepImportProfile(props: IProps) { Profil détecté : - - {profileData.userData?.email || "Email non disponible"} - Version : {profileData.version} - {profileData.exportedAt && ( + {profileData.exported_at && ( - Exporté le : {new Date(profileData.exportedAt).toLocaleDateString('fr-FR')} + Exporté le : {new Date(profileData.exported_at).toLocaleDateString('fr-FR')} )} diff --git a/src/front/Components/Layouts/Login/index.tsx b/src/front/Components/Layouts/Login/index.tsx index 8226512b..6005e476 100644 --- a/src/front/Components/Layouts/Login/index.tsx +++ b/src/front/Components/Layouts/Login/index.tsx @@ -22,6 +22,8 @@ import UserStore from "@Front/Stores/UserStore"; import AuthModal from "src/sdk/AuthModal"; import CustomerService from "src/common/Api/LeCoffreApi/sdk/CustomerService"; import StepImportProfile from "./StepImportProfile"; +import MessageBus from "src/sdk/MessageBus"; +import Iframe from "src/sdk/Iframe"; export enum LoginStep { EMAIL, @@ -46,6 +48,7 @@ export default function Login() { const [validationErrors, setValidationErrors] = useState([]); const [isAuthModalOpen, setIsAuthModalOpen] = useState(false); const [isLoading, setIsLoading] = useState(false); + const [showIframeForImport, setShowIframeForImport] = useState(false); // const openErrorModal = useCallback(() => { // setIsErrorModalOpen(true); @@ -248,26 +251,22 @@ export default function Login() { try { setIsLoading(true); setValidationErrors([]); + + // Show iframe for import operation (but hidden from view) + setShowIframeForImport(true); - // Call API to validate and import profile - // Note: You'll need to implement this method in your Auth service - // const response = await Auth.getInstance().importProfile(profileData); + // Initialize message listener for import operation + MessageBus.getInstance().initMessageListener(); - // For now, we'll simulate a successful import - // In a real implementation, you would: - // 1. Send the profile data to your backend - // 2. Validate the profile on the server - // 3. Return authentication tokens - // 4. Connect the user - - // Simulate API call - await new Promise(resolve => setTimeout(resolve, 1000)); - - // For demo purposes, we'll just redirect to the dashboard - // In reality, you'd use the response from the API - // CustomerStore.instance.connect(response.accessToken, response.refreshToken); - - // Redirect to dashboard + // Wait for the iframe to be ready + await MessageBus.getInstance().isReady(); + + await MessageBus.getInstance().importBackup(profileData); + + // Clean up message listener after import + MessageBus.getInstance().destroyMessageListener(); + setShowIframeForImport(false); + router.push(Module.getInstance().get().modules.pages.Folder.pages.Select.props.path); } catch (error: any) { setValidationErrors([ @@ -280,6 +279,7 @@ export default function Login() { ]); } finally { setIsLoading(false); + setShowIframeForImport(false); } }, [router]); @@ -337,6 +337,7 @@ export default function Login() { /> )} + {showIframeForImport &&