Skip to content

Commit

Permalink
fix: fetching org profiles on mount
Browse files Browse the repository at this point in the history
  • Loading branch information
ZL-Asica committed Dec 6, 2024
1 parent 032c128 commit 805e3f2
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 41 deletions.
5 changes: 4 additions & 1 deletion src/components/Home/DonorDashboard/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,13 @@ import OrganizationCard from './OrganizationCard';

import { useOrganizationStore } from '@/stores';

import SearchBar from '@/components/common/SearchBar';
import { SearchBar, LoadingCircle } from '@/components/common';

const DonorDashboard = () => {
const [searchQuery, setSearchQuery] = useState('');
const loading = useOrganizationStore((state) => state.loading);
if (loading) return <LoadingCircle />;

const organizationProfiles = useOrganizationStore(
(state) => state.organizationProfiles
);
Expand Down
41 changes: 7 additions & 34 deletions src/components/common/ProtectedRoute.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import { Typography } from '@mui/material';
import { useEffect, type ReactElement } from 'react';
import { toast } from 'sonner';
import { type ReactElement } from 'react';

import { useOrganizationStore, useUserStore } from '@/stores';
import { useUserStore, useOrganizationStore } from '@/stores';

import LoadingCircle from '@/components/common/LoadingCircle';

Expand All @@ -13,8 +12,12 @@ type ProtectedRouteProps = {
const ProtectedRoute = ({ element }: ProtectedRouteProps) => {
const user = useUserStore((state) => state.user);
const loading = useUserStore((state) => state.loading);
const orgLoading = useOrganizationStore((state) => state.loading);

if (loading || (user && user.role !== 'organization' && orgLoading)) {
return <LoadingCircle />;
}

if (loading) return <LoadingCircle />;
if (!user) {
return (
<Typography
Expand All @@ -29,36 +32,6 @@ const ProtectedRoute = ({ element }: ProtectedRouteProps) => {
);
}

if (user.role !== 'organization') {
const orgLoading = useOrganizationStore((state) => state.loading);
const subscribeToProfiles = useOrganizationStore(
(state) => state.subscribeToProfiles
);
const error = useOrganizationStore((state) => state.error);

useEffect(() => {
const unsubscribe = subscribeToProfiles;

return () => unsubscribe && unsubscribe();
}, [subscribeToProfiles]);

if (orgLoading) return <LoadingCircle />;

if (error) {
toast.error(error);
return (
<Typography
variant='body1'
color='error'
align='center'
sx={{ mt: 4 }}
>
{error}
</Typography>
);
}
}

return element;
};

Expand Down
25 changes: 25 additions & 0 deletions src/routes.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
import { Routes, Route } from 'react-router-dom';
import { useEffect } from 'react';
import { toast } from 'sonner';

import { useOrganizationStore, useUserStore } from './stores';

import Home from '@/pages/Home';
import Schedule from '@/pages/Schedule';
Expand All @@ -14,6 +18,27 @@ const AppRoutes = () => {
{ path: 'alerts', element: <Alerts /> },
];

const loading = useUserStore((state) => state.loading);
const user = useUserStore((state) => state.user);
const subscribeToProfiles = useOrganizationStore(
(state) => state.subscribeToProfiles
);
const error = useOrganizationStore((state) => state.error);

useEffect(() => {
if (!loading && (!user || user.role !== 'organization')) {
const unsubscribe = subscribeToProfiles;

return () => unsubscribe && unsubscribe();
}
}, [loading, user, subscribeToProfiles]);

useEffect(() => {
if (error) {
toast.error(error);
}
}, [error]);

return (
<Routes>
<Route
Expand Down
15 changes: 9 additions & 6 deletions src/stores/userStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,18 @@ const useUserStore = create<UserState>()(
async (firebaseUser) => {
if (firebaseUser) {
const currentUser = get().user;
if (currentUser) {
await fetchEventsByIds(currentUser.joinedEvents);

if (currentUser && currentUser.joinedEvents?.length > 0) {
try {
await fetchEventsByIds(currentUser.joinedEvents);
} catch (error) {
console.error('Error fetching events:', error);
}
}
set({ user: currentUser || undefined });
} else {
set({ user: undefined });
const eventStore = useEventStore.getState();
eventStore.setEvents([]);
useEventStore.getState().setEvents([]);
}
set({ loading: false });
}
Expand Down Expand Up @@ -69,8 +74,6 @@ const useUserStore = create<UserState>()(
try {
set({ loading: true });
await logoutUser(navigate);
const eventStore = useEventStore.getState();
eventStore.setEvents([]);

set({ user: undefined, error: null });
} catch (error) {
Expand Down

0 comments on commit 805e3f2

Please sign in to comment.