Skip to content

Commit

Permalink
passes userId as a prop into ReviewPage
Browse files Browse the repository at this point in the history
  • Loading branch information
ccatherinetan committed Dec 3, 2024
1 parent 0a6ea63 commit d312e2a
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 8 deletions.
32 changes: 25 additions & 7 deletions app/onboarding/page.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
'use client';

import React, { useState } from 'react';
import React, { useEffect, useState } from 'react';
import { useRouter } from 'next/navigation';
import { UUID } from 'crypto';
import { BigButton } from '@/components/Buttons';
import LabeledCustomSelect from '@/components/EditableInput';
import COLORS from '@/styles/colors';
Expand Down Expand Up @@ -42,6 +43,7 @@ interface PlotSelectionProps {
setSelectedPlot: (selected: boolean) => void;
}
interface ReviewPageProps {
userId: UUID;
selectedState: string;
setSelectedState: (selected: string) => void;
selectedGardenType: UserTypeEnum;
Expand Down Expand Up @@ -125,23 +127,23 @@ const PlotSelection = ({
);
};

// TODO: Maybe just directly include this inside OnboardingFlow
// to mitigate needing to redefine router.
const ReviewPage = ({
userId,
selectedState,
setSelectedState,
selectedGardenType,
setSelectedGardenType,
selectedPlot,
setSelectedPlot,
}: ReviewPageProps) => {
const { userId } = useAuth();
const { setProfile, setHasPlot } = useProfile();
const router = useRouter();

// assumes userId is not null, since the not-logged in case
// would have been handled by rerouting from the page
const handleSubmit = async () => {
if (!userId) {
console.error('User ID is not available. Please log in.');
return;
}
const profile: Profile = {
user_id: userId,
us_state: selectedState,
Expand Down Expand Up @@ -192,6 +194,8 @@ const ReviewPage = ({

// Main Onboarding Component
export default function OnboardingFlow() {
const { userId, loading: authLoading } = useAuth();
const { profileReady, profileData } = useProfile();
const [step, setStep] = useState(1);
const [selectedState, setSelectedState] = useState<string>('');
const [selectedGardenType, setSelectedGardenType] = useState<
Expand All @@ -200,6 +204,17 @@ export default function OnboardingFlow() {
const [selectedPlot, setSelectedPlot] = useState<boolean | undefined>(
undefined,
);
const { push } = useRouter();

// If user not logged in, re-route to /login
useEffect(() => {
if (!authLoading && !userId) push('/login');
}, [authLoading, userId, push]);

// If user already onboarded, re-route to /view-plants
useEffect(() => {
if (profileReady && profileData) push('/view-plants');
}, [profileReady, profileData, push]);

const handleNext = () => {
setStep(step + 1);
Expand All @@ -209,7 +224,9 @@ export default function OnboardingFlow() {
setStep(step - 1);
};

return (
return !userId ? (
<>Loading</>
) : (
<div>
{step === 1 && (
<StateSelection
Expand All @@ -231,6 +248,7 @@ export default function OnboardingFlow() {
)}
{step === 4 && (
<ReviewPage
userId={userId}
selectedState={selectedState}
setSelectedState={setSelectedState}
selectedGardenType={selectedGardenType!}
Expand Down
2 changes: 1 addition & 1 deletion app/view-plants/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import {
checkSearchTerm,
checkSunlight,
} from '@/utils/helpers';
import { useProfile } from '@/utils/ProfileProvider';
import {
AddButton,
FilterContainer,
Expand All @@ -38,7 +39,6 @@ import {
TopRowContainer,
ViewSelection,
} from './styles';
import { useProfile } from '@/utils/ProfileProvider';

// Declaring (static) filter options outside so they're not re-rendered
// TODO: Maybe export shared filter options from a centralized file
Expand Down

0 comments on commit d312e2a

Please sign in to comment.