Skip to content

Commit

Permalink
Merge branch 'main' into donorEmail
Browse files Browse the repository at this point in the history
  • Loading branch information
ZL-Asica authored Dec 6, 2024
2 parents 3bac6ac + 93a3e75 commit 0aded02
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 35 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
23 changes: 23 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,25 @@ const AppRoutes = () => {
{ path: 'alerts', element: <Alerts /> },
];

const loading = useUserStore((state) => state.loading);
const user = useUserStore((state) => state.user);

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

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

return () => unsubscribe && unsubscribe();
}, [subscribeToProfiles]);
if (error) {
toast.error(error);
}
}

return (
<Routes>
<Route
Expand Down

0 comments on commit 0aded02

Please sign in to comment.