From 44ba14ff58ac7d6c3fabc10a7e575139c55f82f2 Mon Sep 17 00:00:00 2001 From: ZL Asica <40444637+ZL-Asica@users.noreply.github.com> Date: Fri, 6 Dec 2024 13:04:25 -0600 Subject: [PATCH] fix: events and organization persist removed. --- src/components/Schedule/index.tsx | 7 +- src/pages/Schedule.tsx | 5 +- src/stores/eventStore.ts | 137 ++++++++++++++---------------- src/stores/organizationStore.ts | 107 +++++++++++------------ src/stores/userStore.ts | 7 ++ 5 files changed, 125 insertions(+), 138 deletions(-) diff --git a/src/components/Schedule/index.tsx b/src/components/Schedule/index.tsx index 756bfe2..0c711c5 100644 --- a/src/components/Schedule/index.tsx +++ b/src/components/Schedule/index.tsx @@ -15,18 +15,21 @@ import dayjs from 'dayjs'; import EventsCalendar from './EventsCalendar'; +import { useEventStore } from '@/stores'; + import { generateICSFile, generateCombinedICSFile, } from '@/utils/generateICSFile'; interface ScheduleBaseProps { - events: DonationEvent[]; title: string; description: string; } -const ScheduleBase = ({ events, title, description }: ScheduleBaseProps) => { +const ScheduleBase = ({ title, description }: ScheduleBaseProps) => { + const events = useEventStore((store) => store.events); + const [selectedDate, setSelectedDate] = useState(dayjs()); const [selectedEvent, setSelectedEvent] = useState( null diff --git a/src/pages/Schedule.tsx b/src/pages/Schedule.tsx index 4191080..716577c 100644 --- a/src/pages/Schedule.tsx +++ b/src/pages/Schedule.tsx @@ -1,4 +1,4 @@ -import { useUserStore, useEventStore } from '@/stores'; +import { useUserStore } from '@/stores'; import ScheduleBase from '@/components/Schedule'; @@ -7,8 +7,6 @@ const DonorSchedule = () => { if (!user) return null; - const events = useEventStore((store) => store.events); - const title = user.role === 'donor' ? 'Your Donation Schedule' @@ -20,7 +18,6 @@ const DonorSchedule = () => { return ( diff --git a/src/stores/eventStore.ts b/src/stores/eventStore.ts index 8303286..dd3dcfc 100644 --- a/src/stores/eventStore.ts +++ b/src/stores/eventStore.ts @@ -1,5 +1,4 @@ import { create } from 'zustand'; -import { createJSONStorage, persist } from 'zustand/middleware'; import { collection, getDocs } from 'firebase/firestore'; import useUserStore from './userStore'; @@ -17,78 +16,70 @@ interface EventState { ) => Promise; } -const useEventStore = create()( - persist( - (set, get) => ({ - events: [], - - setEvents: (events) => set({ events }), - - fetchEventsByIds: async (eventIds: string[]) => { - if (!eventIds?.length) { - console.info('No event IDs provided.'); - return; - } - - try { - const eventsCollection = collection(db, 'events'); - const snapshot = await getDocs(eventsCollection); - - if (!snapshot.docs?.length) { - console.info('No events found in the collection.'); - return; - } - - const events: DonationEvent[] = snapshot.docs - .filter((doc) => eventIds.includes(doc.id)) - .map((doc) => ({ - ...doc.data(), - eventId: doc.id, - })) as DonationEvent[]; - - set({ events }); - } catch (error) { - console.error('Error fetching events by IDs:', error); - throw error; - } - }, - - addDonationEvent: async ( - event, - donorUpdates, - organizationUpdates - ): Promise => { - try { - if (organizationUpdates.uid) { - await updateDocument( - 'organization', - organizationUpdates.uid, - organizationUpdates - ); - } - - if (donorUpdates.uid) { - const userStore = useUserStore.getState(); - await userStore.updateProfile(donorUpdates); - } - - await updateEvent(event); - - const currentEvents = get().events; - set({ events: [...currentEvents, event] }); - - console.info(`Donation event ${event.eventId} created successfully.`); - } catch (error) { - console.error('Error adding donation event:', error); - throw new Error('Failed to create donation event.'); - } - }, - }), - { - name: 'event-store', - storage: createJSONStorage(() => sessionStorage), +const useEventStore = create()((set, get) => ({ + events: [], + + setEvents: (events) => set({ events }), + + fetchEventsByIds: async (eventIds: string[]) => { + if (!eventIds?.length) { + console.info('No event IDs provided.'); + return; + } + + try { + const eventsCollection = collection(db, 'events'); + const snapshot = await getDocs(eventsCollection); + + if (!snapshot.docs?.length) { + console.info('No events found in the collection.'); + return; + } + + const events: DonationEvent[] = snapshot.docs + .filter((doc) => eventIds.includes(doc.id)) + .map((doc) => ({ + ...doc.data(), + eventId: doc.id, + })) as DonationEvent[]; + + set({ events }); + } catch (error) { + console.error('Error fetching events by IDs:', error); + throw error; + } + }, + + addDonationEvent: async ( + event, + donorUpdates, + organizationUpdates + ): Promise => { + try { + if (organizationUpdates.uid) { + await updateDocument( + 'organization', + organizationUpdates.uid, + organizationUpdates + ); + } + + if (donorUpdates.uid) { + const userStore = useUserStore.getState(); + await userStore.updateProfile(donorUpdates); + } + + await updateEvent(event); + + const currentEvents = get().events; + set({ events: [...currentEvents, event] }); + + console.info(`Donation event ${event.eventId} created successfully.`); + } catch (error) { + console.error('Error adding donation event:', error); + throw new Error('Failed to create donation event.'); } - ) -); + }, +})); export default useEventStore; diff --git a/src/stores/organizationStore.ts b/src/stores/organizationStore.ts index b20f010..b06a6fd 100644 --- a/src/stores/organizationStore.ts +++ b/src/stores/organizationStore.ts @@ -1,5 +1,4 @@ import { create } from 'zustand'; -import { createJSONStorage, persist } from 'zustand/middleware'; import { collection, onSnapshot, getDocs } from 'firebase/firestore'; import { db } from '@/utils/firebase'; @@ -12,72 +11,62 @@ interface OrganizationState { subscribeToProfiles: () => void; } -const useOrganizationStore = create()( - persist( - (set) => ({ - organizationProfiles: [], - loading: true, - error: null, +const useOrganizationStore = create()((set) => ({ + organizationProfiles: [], + loading: true, + error: null, - fetchProfiles: async () => { - try { - set({ loading: true }); - const organizationsCollection = collection(db, 'organization'); - const snapshot = await getDocs(organizationsCollection); + fetchProfiles: async () => { + try { + set({ loading: true }); + const organizationsCollection = collection(db, 'organization'); + const snapshot = await getDocs(organizationsCollection); - if (snapshot.empty) { - console.info('No organization profiles found.'); - set({ organizationProfiles: [], loading: false }); - return; - } + if (snapshot.empty) { + console.info('No organization profiles found.'); + set({ organizationProfiles: [], loading: false }); + return; + } - const profiles: OrganizationProfile[] = snapshot.docs.map((doc) => ({ - ...doc.data(), - uid: doc.id, - })) as OrganizationProfile[]; + const profiles: OrganizationProfile[] = snapshot.docs.map((doc) => ({ + ...doc.data(), + uid: doc.id, + })) as OrganizationProfile[]; - set({ organizationProfiles: profiles, loading: false }); - } catch (error) { - console.error('Error fetching organization profiles:', error); - set({ error: (error as Error).message, loading: false }); - } - }, - - subscribeToProfiles: () => { - const organizationsCollection = collection(db, 'organization'); + set({ organizationProfiles: profiles, loading: false }); + } catch (error) { + console.error('Error fetching organization profiles:', error); + set({ error: (error as Error).message, loading: false }); + } + }, - const unsubscribe = onSnapshot( - organizationsCollection, - (snapshot) => { - if (snapshot.empty) { - console.info('No organization profiles found.'); - set({ organizationProfiles: [], loading: false }); - return; - } + subscribeToProfiles: () => { + const organizationsCollection = collection(db, 'organization'); - const profiles: OrganizationProfile[] = snapshot.docs.map( - (doc) => ({ - ...doc.data(), - uid: doc.id, - }) - ) as OrganizationProfile[]; + const unsubscribe = onSnapshot( + organizationsCollection, + (snapshot) => { + if (snapshot.empty) { + console.info('No organization profiles found.'); + set({ organizationProfiles: [], loading: false }); + return; + } - set({ organizationProfiles: profiles, loading: false }); - }, - (error) => { - console.error('Error subscribing to organization profiles:', error); - set({ error: error.message, loading: false }); - } - ); + const profiles: OrganizationProfile[] = snapshot.docs.map((doc) => ({ + ...doc.data(), + uid: doc.id, + })) as OrganizationProfile[]; - return unsubscribe; + set({ organizationProfiles: profiles, loading: false }); }, - }), - { - name: 'organization-store', - storage: createJSONStorage(() => sessionStorage), - } - ) -); + (error) => { + console.error('Error subscribing to organization profiles:', error); + set({ error: error.message, loading: false }); + } + ); + + return unsubscribe; + }, +})); export default useOrganizationStore; diff --git a/src/stores/userStore.ts b/src/stores/userStore.ts index 40c3079..4987cb9 100644 --- a/src/stores/userStore.ts +++ b/src/stores/userStore.ts @@ -53,6 +53,8 @@ const useUserStore = create()( }, login: async (userType, navigate) => { + const fetchEventsByIds = useEventStore.getState().fetchEventsByIds; + try { set({ loading: true }); const profile = await loginUser(navigate, userType); @@ -61,6 +63,10 @@ const useUserStore = create()( user: { ...profile, role: userType }, error: null, }); + const currentUser = get().user; + if (currentUser && currentUser.joinedEvents?.length > 0) { + await fetchEventsByIds(currentUser.joinedEvents); + } } } catch (error) { console.error('Error during login:', error); @@ -76,6 +82,7 @@ const useUserStore = create()( await logoutUser(navigate); set({ user: undefined, error: null }); + useEventStore.getState().setEvents([]); } catch (error) { console.error('Error during logout:', error); set({ error: 'Logout failed. Please try again.' });