Skip to content

Commit

Permalink
unstyled version of view plants
Browse files Browse the repository at this point in the history
  • Loading branch information
SashankBalusu committed Oct 14, 2024
1 parent f243253 commit 7cc4a5b
Show file tree
Hide file tree
Showing 5 changed files with 113 additions and 61 deletions.
2 changes: 1 addition & 1 deletion api/supabase/queries/plant_by_id.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import supabase from '../createClient';
export async function get_plant_by_id(
i_state: string,
p_id: UUID,
): Promise<Plant[]> {
): Promise<Plant> {
const { data, error } = await supabase.rpc('get_plant_by_id', {
input_state: i_state,
plant_id: p_id,
Expand Down
124 changes: 104 additions & 20 deletions app/view-plants/page.tsx
Original file line number Diff line number Diff line change
@@ -1,26 +1,110 @@
'use client';

import { useEffect, useState } from 'react';
import { UUID } from 'crypto';
import supabase from '@/api/supabase/createClient';
import { get_plant_by_id } from '@/api/supabase/queries/plant_by_id';
import { getPlantSeasonality } from '@/api/supabase/queries/plantSeasonality';
import PlantCard from '@/components/PlantCard/PlantCard';
import { Plant } from '@/types/schema';

export default function Home() {
const plant: Plant = {
id: '3067b896-ba05-45b0-bb5c-e09277cf1e68',
plant_name: 'tomato',
state: 'tennessee',
harvest_season: 'SPRING',
water_num_times_per_week: 7,
plant_seed_indoors_start: 'January',
plant_seed_indoors_end: 'February',
plant_seed_outdoors_start: 'March',
plant_seed_outdoors_end: 'April',
plant_transplant_start: 'April',
plant_transplant_end: 'May',
harvest_start: 'April',
harvest_end: 'May',
water_inches_per_week: 7,
harvest_days_after_plant: 5,
sunlight_required: 'Yes',
beginner_friendly: true,
plant_tips: 'aerg',
const [selectedOption, setSelectedOption] = useState<'myPlants' | 'all'>(
'myPlants',
);
const [isSelected, setIsSelected] = useState<true | false>(false);

const [plants, setPlants] = useState<Plant[]>([]);
const [userPlants, setUserPlants] = useState<Plant[]>([]);
const user_id: UUID = 'e72af66d-7aae-45f6-935a-187197749d9f';
async function fetchUserPlants(user_id: UUID) {
const { data, error } = await supabase
.from('user_plants')
.select('plant_id')
.eq('user_id', user_id);

if (error) {
console.error('Error fetching plant IDs:', error);
return [];
}

const plantIds = data?.map(row => row.plant_id) || [];
if (plantIds.length === 0) {
return [];
}

const plantsUser: Plant[] = await Promise.all(
plantIds.map(plantId => get_plant_by_id('Tennessee', plantId)),
);
return plantsUser;
}
useEffect(() => {
const fetchPlantSeasonality = async () => {
const plantList = await getPlantSeasonality('Tennessee');
setPlants(plantList);
};

fetchPlantSeasonality();
}, []);
useEffect(() => {
const fetchData = async () => {
const result = await fetchUserPlants(user_id);
setUserPlants(result);
};
fetchData();
}, []);

// Handle button clicks
const handleClick = (option: 'myPlants' | 'all') => {
setSelectedOption(option);
};
const handleSelected = (isSelectedInput: true | false) => {
setIsSelected(isSelectedInput);
};
return <PlantCard {...plant}></PlantCard>;
return (
<div className="main">
<div id="plantContent">
<div className="plantSelectionHeader">
<button onClick={() => handleClick('myPlants')}>My Plants</button>
<button onClick={() => handleClick('all')}>All</button>
</div>
<div className="componentsDisplay">
{selectedOption === 'myPlants' && userPlants.length == 0 && (
<div>
<button onClick={() => handleClick('all')}>Add Plants</button>
</div>
)}
{selectedOption === 'myPlants' && (
<div>
{userPlants.map((plant, key) => (
<PlantCard key={key} plantObj={plant} canSelect={false} />
))}
</div>
)}
{selectedOption === 'all' && isSelected == true && (
<div>
{plants.map((plant, key) => (
<PlantCard key={key} plantObj={plant} canSelect={true} />
))}
<div className="footer">
<button>Add to my plants</button>
</div>
</div>
)}
{selectedOption === 'all' && isSelected == false && (
<div>
{plants.map((plant, key) => (
<PlantCard key={key} plantObj={plant} canSelect={false} />
))}
<div className="footer">
<button onClick={() => handleSelected(true)}>
Add to my plants
</button>
</div>
</div>
)}
</div>
</div>
</div>
);
}
9 changes: 8 additions & 1 deletion components/PlantCard/PlantCard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,14 @@ import React from 'react';
import { Plant } from '@/types/schema';
import styles from './PlantCardStyles.module.css';

export default function PlantCard(plantObj: Plant) {
export default function PlantCard({
plantObj,
canSelect,
}: {
plantObj: Plant;
canSelect: boolean;
}) {
console.log(canSelect);
return (
<div className={styles.Card}>
<div className={styles.CardPic}>
Expand Down
20 changes: 0 additions & 20 deletions src/api/supabase/createClient.ts

This file was deleted.

19 changes: 0 additions & 19 deletions src/api/supabase/queries/plant_by_id.ts

This file was deleted.

0 comments on commit 7cc4a5b

Please sign in to comment.