Skip to content

Commit 31d3fc7

Browse files
committed
refactor MainNav to save user and organization details in global state
1 parent b7161e5 commit 31d3fc7

File tree

1 file changed

+54
-14
lines changed

1 file changed

+54
-14
lines changed

app/[locale]/dashboard/components/main-nav.tsx

Lines changed: 54 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
'use client';
22

3-
import React from 'react';
3+
import React, { useEffect } from 'react';
44
import Image from 'next/image';
55
import Link from 'next/link';
66
import { usePathname } from 'next/navigation';
@@ -24,7 +24,11 @@ import {
2424
Text,
2525
} from 'opub-ui';
2626

27+
import { GraphQL } from '@/lib/api';
2728
import { Icons } from '@/components/icons';
29+
import { useDashboardStore } from '../[entityType]/[entitySlug]/layout';
30+
import { UserDetailsQryDoc } from '../[entityType]/[entitySlug]/schema';
31+
import { allOrganizationsListingDoc } from '../[entityType]/schema';
2832
import Sidebar from './sidebar';
2933

3034
const profileLinks = [
@@ -41,6 +45,8 @@ export function MainNav({ hideSearch = false }) {
4145
const searchRef = React.useRef<HTMLInputElement>(null);
4246
const { data: session, status } = useSession();
4347
const [commandOpen, setCommandOpen] = React.useState(false);
48+
const { setUserDetails, setAllEntityDetails, userDetails, allEntityDetails } =
49+
useDashboardStore();
4450

4551
useMetaKeyPress('k', () => setCommandOpen((e) => !e));
4652

@@ -54,6 +60,45 @@ export function MainNav({ hideSearch = false }) {
5460
}
5561
}
5662

63+
const handleSignIn = async () => {
64+
try {
65+
// First attempt sign in
66+
await signIn('keycloak', {
67+
redirect: true,
68+
callbackUrl: '/dashboard'
69+
});
70+
71+
// The above will redirect automatically, no need for additional code
72+
// If redirect is needed manually, we can use:
73+
// router.push('/dashboard');
74+
75+
} catch (error) {
76+
console.error('Sign in error:', error);
77+
}
78+
};
79+
80+
useEffect(() => {
81+
const fetchData = async () => {
82+
if (session?.user) {
83+
try {
84+
// Fetch both queries in parallel
85+
const [userDetailsRes, entityDetailsRes] = await Promise.all([
86+
GraphQL(UserDetailsQryDoc, {}, []),
87+
GraphQL(allOrganizationsListingDoc, {}, [])
88+
]);
89+
90+
// Update store with results
91+
setUserDetails(userDetailsRes);
92+
setAllEntityDetails(entityDetailsRes);
93+
} catch (queryError) {
94+
console.error('Error fetching data:', queryError);
95+
}
96+
}
97+
};
98+
99+
fetchData();
100+
}, [session]);
101+
57102
if (isLoggingOut) {
58103
return <LogginOutPage />;
59104
}
@@ -77,8 +122,9 @@ export function MainNav({ hideSearch = false }) {
77122
},
78123
];
79124

125+
80126
return (
81-
<nav className='p-4 lg:p-6'>
127+
<nav className="p-4 lg:p-6">
82128
<div className="flex items-center justify-between gap-4 ">
83129
<div className="flex items-center gap-1">
84130
<div className="lg:hidden">
@@ -92,7 +138,7 @@ export function MainNav({ hideSearch = false }) {
92138
</div>
93139
<Link href="/">
94140
<div className="flex items-center gap-2">
95-
<div className="group relative h-[38px] rounded-full w-[38px] overflow-hidden">
141+
<div className="group relative h-[38px] w-[38px] overflow-hidden rounded-full">
96142
{/* Static Logo */}
97143
<div className="absolute inset-0 transition-opacity duration-300 group-hover:opacity-0">
98144
<Image
@@ -179,16 +225,12 @@ export function MainNav({ hideSearch = false }) {
179225
) : (
180226
<Button
181227
onClick={() => {
182-
console.log(
183-
process.env.NEXTAUTH_URL,
184-
process.env.NEXT_PUBLIC_NEXTAUTH_URL
185-
);
186-
signIn('keycloak');
228+
handleSignIn();
187229
}}
188230
kind="secondary"
189-
className=' bg-tertiaryAccent'
231+
className=" bg-tertiaryAccent"
190232
>
191-
<Text variant="headingMd" >LOGIN / SIGN UP</Text>
233+
<Text variant="headingMd">LOGIN / SIGN UP</Text>
192234
</Button>
193235
)}
194236
</div>
@@ -274,11 +316,9 @@ export const ProfileContent = ({
274316

275317
const LogginOutPage = () => {
276318
return (
277-
<div className=" flex items-center bg-surfaceDefault p-2 justify-end gap-4">
319+
<div className=" flex items-center justify-end gap-4 bg-surfaceDefault p-2">
278320
<Spinner color="surface" />
279-
<Text variant="headingLg" >
280-
Logging out...
281-
</Text>
321+
<Text variant="headingLg">Logging out...</Text>
282322
</div>
283323
);
284324
};

0 commit comments

Comments
 (0)