diff --git a/app/view-plants/page.tsx b/app/view-plants/page.tsx index 31c58e0..694d528 100644 --- a/app/view-plants/page.tsx +++ b/app/view-plants/page.tsx @@ -12,7 +12,7 @@ import PlantCard from '@/components/PlantCard'; import SearchBar from '@/components/SearchBar'; import COLORS from '@/styles/colors'; import { Box, Flex } from '@/styles/containers'; -import { H1 } from '@/styles/text'; +import { H1, P1 } from '@/styles/text'; import { DropdownOption, OwnedPlant, @@ -63,7 +63,7 @@ const growingSeasonOptions: DropdownOption[] = [ export default function Page() { const router = useRouter(); const { hasPlot, profileData, profileReady, setPlantsToAdd } = useProfile(); - const { userId } = useAuth(); + const { userId, loading: authLoading } = useAuth(); const [viewingOption, setViewingOption] = useState<'myPlants' | 'all'>( hasPlot ? 'myPlants' : 'all', @@ -84,6 +84,8 @@ export default function Page() { const [ownedPlants, setOwnedPlants] = useState([]); const userState = profileData?.us_state ?? null; + const profileAndAuthReady = profileReady && !authLoading; + // Fetch All Plants useEffect(() => { // Only fetch plants when profile is ready and we have a state @@ -101,7 +103,7 @@ export default function Page() { // Fetch User Plants for My Garden tab useEffect(() => { // Only fetch user plants if we have a valid userId - if (userId) { + if (!authLoading && userId) { const fetchUserPlants = async () => { const fetchedUserPlants = await getCurrentUserPlantsByUserId(userId); @@ -118,7 +120,7 @@ export default function Page() { }; fetchUserPlants(); } - }, [userId]); + }, [userId, authLoading]); const clearFilters = () => { setSelectedGrowingSeason([]); @@ -183,29 +185,138 @@ export default function Page() { setSelectedPlants([]); setInAddMode(false); } - // Not logged in - if (!userId) { + + function MainBody() { + // assume auth and profile are both ready + // Not logged in + if (!userId) { + return ( + + Login to view all plants + + + ); + } + + // Not onboarded + if (!profileData) { + return ( + + Complete your profile view all plants + + + ); + } + + // Onboarded and Logged in: Normal Screen return ( -
-

Login to view all plants

- -
+ <> + + + setViewingOption('myPlants')} + > + My Plants + + setViewingOption('all')} + > + All + + + {/* Select/Cancel toggles Add Mode; appears in All plants only*/} + {viewingOption === 'all' && + (inAddMode ? ( + + Cancel + + ) : ( + setInAddMode(true)} + > + Select + + ))} + + + {viewingOption === 'myPlants' ? ( + + ) : ( + + )} + ); } - // Not onboarded - if (profileReady && !profileData) { + function MyPlantsDisplay() { return (
-

Complete Your Profile

-

Please complete your onboarding to access view plants

- + {ownedPlants.length === 0 ? ( + <>Add Plants To Your Garden + ) : filteredUserPlantList.length === 0 ? ( +

No plants match your current filters.

+ ) : ( + + {filteredUserPlantList.map(ownedPlant => ( + handleUserPlantCardClick(ownedPlant)} + // aspectRatio="168 / 200" + /> + ))} + + )}
); } + function AllPlantsDisplay() { + return ( + <> + {filteredPlantList.length === 0 ? ( +
+

No plants match your current filters.

+
+ ) : ( + + {filteredPlantList.map(plant => ( + handlePlantCardClick(plant)} + // aspectRatio="168 / 200" + /> + ))} + + )} + {inAddMode && ( + + {selectedPlants.length ? 'Add to My Garden' : 'Select Plants'} + + )} + + ); + } + const plantPluralityString = selectedPlants.length > 1 ? 'Plants' : 'Plant'; return ( @@ -250,96 +361,8 @@ export default function Page() { ) : null} - - - setViewingOption('myPlants')} - > - My Plants - - setViewingOption('all')} - > - All - - - {/* Select/Cancel toggles Add Mode; appears in All plants only*/} - {viewingOption === 'all' && - (inAddMode ? ( - - Cancel - - ) : ( - setInAddMode(true)} - > - Select - - ))} - - {viewingOption === 'myPlants' && ( -
- {ownedPlants.length === 0 ? ( - <>Add Plants To Your Garden - ) : filteredUserPlantList.length === 0 ? ( -

No plants match your current filters.

- ) : ( - - {filteredUserPlantList.map(ownedPlant => ( - handleUserPlantCardClick(ownedPlant)} - // aspectRatio="168 / 200" - /> - ))} - - )} -
- )} - {viewingOption === 'all' && ( - <> - {plants.length === 0 ? ( - <>Loading... - ) : filteredPlantList.length === 0 ? ( -
-

No plants match your current filters.

-
- ) : ( - - {filteredPlantList.map(plant => ( - handlePlantCardClick(plant)} - // aspectRatio="168 / 200" - /> - ))} - - )} - {inAddMode && ( - - {selectedPlants.length ? 'Add to My Garden' : 'Select Plants'} - - )} - - )} + {/* Plant Cards and Body */} + {!profileAndAuthReady ? <>Loading : }
);