-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[feat] ProfileProvider implementation, lint + prettier fixes
- Loading branch information
Showing
5 changed files
with
152 additions
and
59 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,13 @@ | ||
import BPLogo from '@/assets/images/bp-logo.png'; | ||
import { Container, Image } from './page.style'; | ||
import { ProfileProvider } from '@/utils/ProfileProvider'; | ||
import OnboardingFlow from './onboarding/page'; | ||
import { Container } from './page.style'; | ||
|
||
export default function Home() { | ||
return ( | ||
<Container> | ||
<Image src={BPLogo} alt="Blueprint Logo" /> | ||
<p>Open up app/page.tsx to get started!</p> | ||
<ProfileProvider> | ||
<OnboardingFlow /> | ||
</ProfileProvider> | ||
</Container> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
'use client'; | ||
|
||
import React, { | ||
createContext, | ||
ReactNode, | ||
useCallback, | ||
useContext, | ||
useEffect, | ||
useMemo, | ||
useState, | ||
} from 'react'; | ||
import { upsertProfile } from '@/api/supabase/queries/profiles'; | ||
import { Profile } from '@/types/schema'; | ||
|
||
// Define a placeholder user ID for development purposes | ||
const placeholderUserId = '2abd7296-374a-42d1-bb4f-b813da1615ae'; | ||
|
||
interface ProfileContextType { | ||
profileData: Profile | null; | ||
profileReady: boolean; | ||
setProfile: (newProfileData: Partial<Profile>) => Promise<void>; | ||
loadProfile: () => Promise<void>; | ||
} | ||
|
||
const ProfileContext = createContext<ProfileContextType | undefined>(undefined); | ||
|
||
export const useProfile = () => { | ||
const context = useContext(ProfileContext); | ||
if (!context) { | ||
throw new Error('useProfile must be used within a ProfileProvider'); | ||
} | ||
return context; | ||
}; | ||
|
||
export const ProfileProvider: React.FC<{ children: ReactNode }> = ({ | ||
children, | ||
}) => { | ||
const [profileData, setProfileData] = useState<Profile | null>(null); | ||
const [profileReady, setProfileReady] = useState<boolean>(false); | ||
|
||
const loadProfile = useCallback(async () => { | ||
setProfileReady(false); | ||
|
||
try { | ||
const profile: Profile = { | ||
user_id: placeholderUserId, | ||
state: '', | ||
user_type: '', | ||
has_plot: false, | ||
}; | ||
// Fetch or upsert the profile for the placeholder user ID | ||
const fetchedProfile = await upsertProfile(profile); | ||
setProfileData(fetchedProfile); | ||
} catch (error) { | ||
console.error('Error loading profile:', error); | ||
} finally { | ||
setProfileReady(true); | ||
} | ||
}, []); | ||
|
||
const setProfile = useCallback( | ||
async (newProfileData: Partial<Profile>) => { | ||
const profileToUpdate: Profile = { | ||
...profileData!, | ||
...newProfileData, | ||
user_id: placeholderUserId, // Using placeholder user ID for now | ||
}; | ||
|
||
try { | ||
const updatedProfile = await upsertProfile(profileToUpdate); | ||
setProfileData(updatedProfile); | ||
} catch (error) { | ||
console.error('Error updating profile:', error); | ||
throw new Error('Error updating profile'); | ||
} | ||
}, | ||
[profileData], | ||
); | ||
|
||
useEffect(() => { | ||
loadProfile(); | ||
}, [loadProfile]); | ||
|
||
const providerValue = useMemo( | ||
() => ({ | ||
profileData, | ||
profileReady, | ||
setProfile, | ||
loadProfile, | ||
}), | ||
[profileData, profileReady, setProfile, loadProfile], | ||
); | ||
|
||
return ( | ||
<ProfileContext.Provider value={providerValue}> | ||
{children} | ||
</ProfileContext.Provider> | ||
); | ||
}; |