From 5855ea41006cd981a8edcffef1c6ff9733b75ba8 Mon Sep 17 00:00:00 2001 From: Arthur Wambst Date: Tue, 21 Oct 2025 14:22:42 +0200 Subject: [PATCH] fix return csv bulk user --- src/pages/admin/BulkCreateUser.tsx | 49 ++++++++++++++++-------------- 1 file changed, 27 insertions(+), 22 deletions(-) diff --git a/src/pages/admin/BulkCreateUser.tsx b/src/pages/admin/BulkCreateUser.tsx index 7eccb60..23d27fd 100644 --- a/src/pages/admin/BulkCreateUser.tsx +++ b/src/pages/admin/BulkCreateUser.tsx @@ -51,31 +51,36 @@ function BulkUsers() { const handleSubmit = async () => { try { - const response = await axios.post( - "/api/users", - { + const response = await axios.post("/api/users", { users: userData.filter((user: CSVParseType) => user.name !== ""), jiId: parseInt(practical), - }, - { - responseType: 'blob', // AJOUTER CETTE LIGNE - } - ); + }); - if (response.status === 200) { - // REMPLACER l'alert par ce code : - const url = window.URL.createObjectURL(new Blob([response.data])); - const link = document.createElement('a'); - link.href = url; - link.setAttribute('download', `created_users_${new Date().toISOString().split('T')[0]}.csv`); - document.body.appendChild(link); - link.click(); - link.remove(); - window.URL.revokeObjectURL(url); - - alert('Users succesfully created ! CSV downloaded.'); - navigate('/admin/users'); - } + if (response.status === 200) { + // REMPLACER l'alert par ce code : + let str = ""; + for (let i = 0; i < response.data.successMails.length; i++) { + const mail = response.data.successMails[i]; + const password = response.data.successPasswd[i]; + str += `${mail},${password}\n`; + } + const url = window.URL.createObjectURL( + new Blob([str], { type: "text/csv" }), + ); + const link = document.createElement("a"); + link.href = url; + link.setAttribute( + "download", + `created_users_${new Date().toISOString().split("T")[0]}.csv`, + ); + document.body.appendChild(link); + link.click(); + link.remove(); + window.URL.revokeObjectURL(url); + + alert("Users succesfully created ! CSV downloaded."); + navigate("/admin/users"); + } if (response.status === 202) { const url = window.URL.createObjectURL(new Blob([response.data])); const link = document.createElement('a');