From 6b0c9f8b4089479116a1539c3121f82373f4ea6e Mon Sep 17 00:00:00 2001 From: Arnaud Ambroselli <31724752+arnaudambro@users.noreply.github.com> Date: Tue, 10 Oct 2023 06:17:58 +0200 Subject: [PATCH] fix(dashboard): reset all atoms on organisation change (#1717) * fix(dashboard): reset all atoms on organisation change * add new test --- dashboard/src/components/DataLoader.js | 14 ++++++++++++++ dashboard/src/scenes/auth/signin.js | 6 +++--- ...spec.js => global_cache-when-logout.spec.js} | 17 +++++++++++++++++ 3 files changed, 34 insertions(+), 3 deletions(-) rename e2e/{global_cache-still-here-when-logout.spec.js => global_cache-when-logout.spec.js} (82%) diff --git a/dashboard/src/components/DataLoader.js b/dashboard/src/components/DataLoader.js index 7f0f22009..bc32879fb 100644 --- a/dashboard/src/components/DataLoader.js +++ b/dashboard/src/components/DataLoader.js @@ -449,6 +449,20 @@ export function useDataLoader(options = { refreshOnMount: false }) { async function resetCache() { await clearCache(); setLastLoad(0); + setPersons([]); + setGroups([]); + setReports([]); + setPassages([]); + setRencontres([]); + setActions([]); + setTerritories([]); + setPlaces([]); + setRelsPersonPlace([]); + setTerritoryObservations([]); + setComments([]); + setConsultations([]); + setTreatments([]); + setMedicalFiles([]); } return { diff --git a/dashboard/src/scenes/auth/signin.js b/dashboard/src/scenes/auth/signin.js index 372738c15..e0c95270c 100644 --- a/dashboard/src/scenes/auth/signin.js +++ b/dashboard/src/scenes/auth/signin.js @@ -105,15 +105,15 @@ const SignIn = () => { const { user, token, ok } = authViaCookie ? await API.get({ path: '/user/signin-token' }) : await API.post({ path: '/user/signin', body }); if (!ok) return setIsSubmitting(false); const { organisation } = user; + if (organisation._id !== window.localStorage.getItem('mano-organisationId')) { + await resetCache(); + } if (!!organisation.encryptionEnabled && !showEncryption && !['superadmin'].includes(user.role)) { setShowEncryption(true); return setIsSubmitting(false); } if (token) setToken(token); setSessionInitialTimestamp(Date.now()); - if (organisation._id !== window.localStorage.getItem('mano-organisationId')) { - await resetCache(); - } window.localStorage.setItem('mano-organisationId', organisation._id); setOrganisation(organisation); if (!['superadmin'].includes(user.role) && !!signinForm.orgEncryptionKey) { diff --git a/e2e/global_cache-still-here-when-logout.spec.js b/e2e/global_cache-when-logout.spec.js similarity index 82% rename from e2e/global_cache-still-here-when-logout.spec.js rename to e2e/global_cache-when-logout.spec.js index 366814cf4..27bf93f0a 100644 --- a/e2e/global_cache-still-here-when-logout.spec.js +++ b/e2e/global_cache-when-logout.spec.js @@ -103,4 +103,21 @@ test("Person creation", async ({ page }) => { await expect(page).toHaveURL("http://localhost:8090/person"); await expect(page.getByRole("cell", { name: person1Name })).toBeVisible(); + + await test.step("Login with user from other organisation should clear the cache", async () => { + await page.getByRole("button", { name: "User Admin Test - 5" }).click(); + await page.getByRole("menuitem", { name: "Se déconnecter et vider le cache" }).click(); + await expect(page).toHaveURL("http://localhost:8090/auth"); + + await page.getByLabel("Email").fill("admin4@example.org"); + await page.getByLabel("Mot de passe").fill("secret"); + await page.getByRole("button", { name: "Se connecter" }).click(); + await page.getByLabel("Clé de chiffrement d'organisation").fill("plouf"); + await page.getByRole("button", { name: "Se connecter" }).click(); + await expect(page).toHaveURL("http://localhost:8090/reception?calendarTab=2"); + await page.getByRole("link", { name: "Personnes suivies" }).click(); + await expect(page).toHaveURL("http://localhost:8090/person"); + + await expect(page.getByRole("cell", { name: person1Name })).not.toBeVisible(); + }); });