Skip to content

Commit

Permalink
refactor getPlantById query
Browse files Browse the repository at this point in the history
  • Loading branch information
ccatherinetan committed Oct 21, 2024
1 parent 6ff3e11 commit 1077750
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 33 deletions.
27 changes: 0 additions & 27 deletions api/supabase/queries/plant_by_id.ts

This file was deleted.

13 changes: 13 additions & 0 deletions api/supabase/queries/plants.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { UUID } from 'crypto';
import { Plant } from '@/types/schema';
import supabase from '../createClient';

Expand All @@ -6,3 +7,15 @@ export async function getAllPlants(): Promise<Plant[]> {
if (error) throw new Error(`Error fetching all plants: ${error.message}`);
return data;
}

export async function getPlantById(plantId: UUID): Promise<Plant> {
const { data, error } = await supabase
.from('plants')
.select('*')
.eq('id', plantId);
if (error) {
throw new Error(`Error getting matching plant: ${error.message}`);
}

return data[0];
}
7 changes: 3 additions & 4 deletions app/view-plants/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,16 @@

import { useEffect, useState } from 'react';
import { UUID } from 'crypto';
import { getPlantById } from '@/api/supabase/queries/plant_by_id';
import { getPlantById } from '@/api/supabase/queries/plants';
import PlantCard from '@/components/PlantCard/PlantCard';
import { Plant } from '@/types/schema';

export default function Home() {
const [result, setResult] = useState<Plant>();
useEffect(() => {
const getData = async () => {
const testState: string = 'Tennessee';
const testUUID: UUID = '185a3300-b0fc-4383-8fb4-417737d77659';
const plant2 = await getPlantById(testState, testUUID);
const testUUID: UUID = '010ae695-6cc8-4af4-919a-d15b92fdd68d';
const plant2 = await getPlantById(testUUID);
setResult(plant2); // Set the result to state
};

Expand Down
10 changes: 8 additions & 2 deletions components/PlantCard/PlantCard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,17 @@ export default function PlantCard({ plant }: { plant: Plant }) {
</div>
<div className={styles.Attribute}>
{/* icon */}
<p>{plant.water_num_times_per_week + ' times / wk'}</p>
<p>{plant.water_frequency}</p>
</div>
<div className={styles.Attribute}>
{/* icon */}
<p>{plant.sunlight_required}</p>
<p>
{plant.sunlight_min_hours}
{plant.sunlight_max_hours
? ' - ' + plant.sunlight_max_hours
: ''}{' '}
hours/day
</p>
</div>
</div>
</div>
Expand Down

0 comments on commit 1077750

Please sign in to comment.