-
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.
allow user to click on plants, refactored backend requests
- Loading branch information
1 parent
d1a98e1
commit 09eee6d
Showing
7 changed files
with
167 additions
and
45 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 was deleted.
Oops, something went wrong.
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,46 @@ | ||
import { UUID } from 'crypto'; | ||
import { UserPlants } from '@/types/schema'; | ||
import supabase from '../createClient'; | ||
|
||
export async function updateUserPlants( | ||
userId: UUID, | ||
formData: Partial<UserPlants>[], | ||
) { | ||
formData.map(async curr => { | ||
const { error } = await supabase.from('user_plants').insert({ | ||
user_id: userId, | ||
plant_id: curr['plant_id'], | ||
date_added: curr['date_added'], | ||
date_harvested: null, | ||
planting_type: curr['planting_type'], | ||
}); | ||
if (error) throw new Error(`Error inserting data: ${error.message}`); | ||
}); | ||
} | ||
export async function getUserPlantById(userPlantId: UUID): Promise<UserPlants> { | ||
const { data, error } = await supabase | ||
.from('user_plants') | ||
.select('*') | ||
.eq('id', userPlantId) | ||
.is('date_harvested', null) | ||
.limit(1) | ||
.single(); | ||
|
||
if (error) { | ||
throw new Error(`Error fetching plant ID: ${error}`); | ||
} | ||
return data; | ||
} | ||
export async function getUserPlantsByUserId( | ||
user_id: UUID, | ||
): Promise<UserPlants[]> { | ||
const { data, error } = await supabase | ||
.from('user_plants') | ||
.select('*') | ||
.eq('user_id', user_id) | ||
.is('date_harvested', null); | ||
if (error) { | ||
throw new Error(`Error fetching userPlant: ${error}`); | ||
} | ||
return data; | ||
} |
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,29 @@ | ||
'use client'; | ||
|
||
import { useEffect, useState } from 'react'; | ||
import { useParams } from 'next/navigation'; | ||
import { UUID } from 'crypto'; | ||
import { getPlantById } from '@/api/supabase/queries/plants'; | ||
import { Plant } from '@/types/schema'; | ||
|
||
export default function GeneralPlantPage() { | ||
const params = useParams(); | ||
const plantId: UUID = params.plantId as UUID; | ||
const [currentPlant, setCurrentPlant] = useState<Plant>(); | ||
useEffect(() => { | ||
const getPlant = async () => { | ||
const plant = await getPlantById(plantId); | ||
setCurrentPlant(plant); | ||
}; | ||
getPlant(); | ||
}, [plantId]); | ||
return ( | ||
<div> | ||
{currentPlant && ( | ||
<div> | ||
<h3>{JSON.stringify(currentPlant)}</h3> | ||
</div> | ||
)} | ||
</div> | ||
); | ||
} |
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,36 @@ | ||
'use client'; | ||
|
||
import { useEffect, useState } from 'react'; | ||
import { useParams } from 'next/navigation'; | ||
import { UUID } from 'crypto'; | ||
import { getMatchingPlantForUserPlants } from '@/api/supabase/queries/plants'; | ||
import { getUserPlantById } from '@/api/supabase/queries/userPlants'; | ||
import { Plant, UserPlants } from '@/types/schema'; | ||
|
||
export default function UserPlantPage() { | ||
const params = useParams(); | ||
const userPlantId: UUID = params.userPlantId as UUID; | ||
const [currentPlant, setCurrentPlant] = useState<Plant>(); | ||
const [currentUserPlant, setCurrentUserPlant] = useState<UserPlants>(); | ||
useEffect(() => { | ||
const getPlant = async () => { | ||
const userPlant = await getUserPlantById(userPlantId); | ||
setCurrentUserPlant(userPlant); | ||
const plant = await getMatchingPlantForUserPlants(userPlant); | ||
setCurrentPlant(plant); | ||
// const plant = await getPlantById(await getUserPlantById(userPlantId)) | ||
// setCurrentPlant(plant) | ||
}; | ||
getPlant(); | ||
}, [userPlantId]); | ||
return ( | ||
<div> | ||
{currentPlant && ( | ||
<div> | ||
<h3>{JSON.stringify(currentUserPlant)}</h3> | ||
<h3>{JSON.stringify(currentPlant)}</h3> | ||
</div> | ||
)} | ||
</div> | ||
); | ||
} |
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