From 83288563dd456f35c04d9b014bbec21f7f033cb9 Mon Sep 17 00:00:00 2001 From: Bruno Bernardino Date: Tue, 5 Oct 2021 15:09:33 +0100 Subject: [PATCH] Delete local data when logging out. --- components/Panels/All.tsx | 2 +- lib/data-utils.ts | 9 +++++++++ modules/auth/LogoutLink.tsx | 14 +++++++++++--- 3 files changed, 21 insertions(+), 4 deletions(-) diff --git a/components/Panels/All.tsx b/components/Panels/All.tsx index 2bf93c7..faa2e2f 100644 --- a/components/Panels/All.tsx +++ b/components/Panels/All.tsx @@ -160,7 +160,7 @@ const All = () => { syncToken={syncToken} db={db.current} /> - + ); }; diff --git a/lib/data-utils.ts b/lib/data-utils.ts index 853359d..d3d7711 100644 --- a/lib/data-utils.ts +++ b/lib/data-utils.ts @@ -536,6 +536,15 @@ export const deleteAllData = async (db: RxDatabase, syncToken: string) => { await remoteDb.erase(); }; +export const deleteLocalData = async (db: RxDatabase) => { + await db.events.remove(); + + // NOTE: The erase below doesn't work locally, so we need the line above + const localDb = new PouchDB(localDbName); + // @ts-ignore erase comes from pouchdb-erase + await localDb.erase(); +}; + type ExportAllData = ( db: RxDatabase, ) => Promise<{ diff --git a/modules/auth/LogoutLink.tsx b/modules/auth/LogoutLink.tsx index 5026685..45c28f0 100644 --- a/modules/auth/LogoutLink.tsx +++ b/modules/auth/LogoutLink.tsx @@ -1,10 +1,16 @@ import { useCallback, useState } from 'react'; import Swal from 'sweetalert2'; import styled from 'styled-components'; +import { RxDatabase } from 'rxdb'; import Loading from 'components/Loading'; import Button from 'components/Button'; import { doLogout } from 'lib/utils'; +import { deleteLocalData } from 'lib/data-utils'; + +interface LogoutLinkProps { + db: RxDatabase; +} const Container = styled.div` top: 8px; @@ -12,11 +18,13 @@ const Container = styled.div` position: absolute; `; -const LoginButton = () => { +const LogoutLink = ({ db }: LogoutLinkProps) => { const [isSubmitting, setIsSubmitting] = useState(false); - const handleLogout = useCallback(() => { + const handleLogout = useCallback(async () => { setIsSubmitting(true); + await deleteLocalData(db); + doLogout(); Swal.fire('Alright!', 'No idea who you are right now.', 'success'); @@ -34,4 +42,4 @@ const LoginButton = () => { ); }; -export default LoginButton; +export default LogoutLink;