From 63796566c30d80e8bc715f9a2541b9643dd5eb8b Mon Sep 17 00:00:00 2001 From: ernie Date: Tue, 14 Nov 2023 12:59:34 +0100 Subject: [PATCH] try catch, unsub --- src/components/StateContainer.tsx | 12 +++++++++--- src/pages/Admin/Admin.tsx | 8 ++++++-- src/pages/Welcome/Welcome.tsx | 9 +++++++-- src/types.ts | 2 +- 4 files changed, 23 insertions(+), 8 deletions(-) diff --git a/src/components/StateContainer.tsx b/src/components/StateContainer.tsx index 5d40e75..95d98d7 100644 --- a/src/components/StateContainer.tsx +++ b/src/components/StateContainer.tsx @@ -1,5 +1,5 @@ import React from "react"; -import { getAuth } from "firebase/auth"; +import { Unsubscribe, getAuth } from "firebase/auth"; import { getFirestore, collection, @@ -31,10 +31,12 @@ const StateContainer: React.FC<{ children: React.ReactNode }> = ({ }, []); React.useEffect(() => { + let unsub1: Unsubscribe | null; + let unsub2: Unsubscribe | null; const getCalendar = async () => { const calendarReference = doc(db, "calendars", name.toLocaleLowerCase()); - onSnapshot(calendarReference, (calendarSnap) => { + unsub1 = onSnapshot(calendarReference, (calendarSnap) => { if (calendarSnap.exists()) { dispatch({ type: SET_CALENDAR, @@ -51,7 +53,7 @@ const StateContainer: React.FC<{ children: React.ReactNode }> = ({ where("calendar", "==", calendarReference), ); - onSnapshot(userQuery, (users) => { + unsub2 = onSnapshot(userQuery, (users) => { dispatch({ type: SET_USERS, payload: users.docs.map((doc) => ({ ...doc.data() }) as UserType), @@ -59,6 +61,10 @@ const StateContainer: React.FC<{ children: React.ReactNode }> = ({ }); }; getCalendar(); + return () => { + if (unsub1) unsub1(); + if (unsub2) unsub2(); + }; }, [name]); React.useEffect(() => { diff --git a/src/pages/Admin/Admin.tsx b/src/pages/Admin/Admin.tsx index 2a855a9..9b1af6a 100644 --- a/src/pages/Admin/Admin.tsx +++ b/src/pages/Admin/Admin.tsx @@ -72,7 +72,9 @@ const Admin: React.FC = () => { await deleteDoc(userReference); const storageRef = ref(storage, `${name.toLocaleLowerCase()}/${id}`); - await deleteObject(storageRef); + try { + await deleteObject(storageRef); + } catch {} } catch (e) {} }); const calendarReference = doc(db, "calendars", name.toLocaleLowerCase()); @@ -313,7 +315,9 @@ const Admin: React.FC = () => { onClick={async () => { const userReference = doc(db, "users", user.id); await deleteDoc(userReference); - await deleteObject(storageRef); + try { + await deleteObject(storageRef); + } catch {} }} /> diff --git a/src/pages/Welcome/Welcome.tsx b/src/pages/Welcome/Welcome.tsx index 8b01d46..652b072 100644 --- a/src/pages/Welcome/Welcome.tsx +++ b/src/pages/Welcome/Welcome.tsx @@ -15,6 +15,7 @@ import { getAuth, GoogleAuthProvider, signInWithPopup, + Unsubscribe, } from "firebase/auth"; import { useState, SET_NOTIFICATION } from "../../StateProvider"; import { Link, useNavigate } from "react-router-dom"; @@ -55,6 +56,7 @@ const Welcome: React.FC = () => { }, []); React.useEffect(() => { + let unsub: Unsubscribe | null; const getCalendars = async () => { if (user) { const calendarReference = collection(db, "calendars"); @@ -63,7 +65,7 @@ const Welcome: React.FC = () => { where("owner", "==", user.uid), ); - onSnapshot(calendarQuery, (calendars) => { + unsub = onSnapshot(calendarQuery, (calendars) => { setCalendars( calendars.docs.map((doc) => ({ ...doc.data() }) as CalendarType), ); @@ -71,6 +73,9 @@ const Welcome: React.FC = () => { } }; getCalendars(); + return () => { + if (unsub) unsub(); + }; }, [user]); const logout = async () => { @@ -96,7 +101,7 @@ const Welcome: React.FC = () => { deleteBy: Timestamp.fromDate( new Date(new Date().getFullYear() + 1, 1, 1), ), - owner: user?.uid ?? undefined, + owner: user?.uid ?? null, name, public: true, settings: { diff --git a/src/types.ts b/src/types.ts index 9f455b0..1807241 100644 --- a/src/types.ts +++ b/src/types.ts @@ -20,6 +20,6 @@ export type CalendarType = { fair?: boolean; ignoreWeekends?: boolean; }; - owner?: string; + owner?: string | null; public?: boolean; };