-
Notifications
You must be signed in to change notification settings - Fork 2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[feat] profile context integration with view plants and add details #63
Merged
ccatherinetan
merged 15 commits into
main
from
58-use-profilecontext-in-view-plants-and-add-details
Dec 13, 2024
Merged
Changes from 14 commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
d494c86
profile context integration with view plants and add detaisl
SashankBalusu 1596b4d
add loading logic
ccatherinetan 0b2115c
implement add functionality on GeneralPlantPage
ccatherinetan afabf65
yikes
SashankBalusu 9d3d390
style PlantDetails
ccatherinetan be1ed99
add placeholder image in PlantDetails
ccatherinetan 93ca2d3
add header in add-details
ccatherinetan dcaba4a
remove fallback userState
ccatherinetan 779c8c8
footer button styling and nav bar
SashankBalusu e407314
messed up back button
SashankBalusu 99baddd
remove nav button usage
SashankBalusu 678d167
fix button placement
ccatherinetan 7d8ef47
add loading styles to view plants
ccatherinetan 7cf753e
remove unused NavigationBar component
ccatherinetan 01150ad
use async handleSubmit in add-detaisl
ccatherinetan File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,65 +2,28 @@ | |
|
||
import { useState } from 'react'; | ||
import { useRouter } from 'next/navigation'; | ||
import { UUID } from 'crypto'; | ||
import { insertUserPlants } from '@/api/supabase/queries/userPlants'; | ||
import PlantDetails from '@/components/PlantDetails'; | ||
import { Plant, UserPlant } from '@/types/schema'; | ||
|
||
const plants: Plant[] = [ | ||
{ | ||
id: 'cfed129c-1cdf-4089-89d2-83ae2fb2f83d', | ||
plant_name: 'cabbage', | ||
us_state: 'string', | ||
harvest_season: 'SPRING', | ||
water_frequency: 'string', | ||
weeding_frequency: 'string', | ||
indoors_start: 'string', | ||
indoors_end: 'string', | ||
outdoors_start: 'string', | ||
outdoors_end: 'string', | ||
transplant_start: 'string', | ||
transplant_end: 'string', | ||
harvest_start: 'string', | ||
harvest_end: 'string', | ||
beginner_friendly: true, | ||
plant_tips: 'string', | ||
img: 'string', | ||
difficulty_level: 'HARD', | ||
sunlight_min_hours: 1, | ||
sunlight_max_hours: 1, | ||
}, | ||
{ | ||
id: '8f25fca8-6e86-486b-9a2b-79f68efa3658', | ||
plant_name: 'tomato', | ||
us_state: 'string', | ||
harvest_season: 'SPRING', | ||
water_frequency: 'string', | ||
weeding_frequency: 'string', | ||
indoors_start: 'string', | ||
indoors_end: 'string', | ||
outdoors_start: 'string', | ||
outdoors_end: 'string', | ||
transplant_start: 'string', | ||
transplant_end: 'string', | ||
harvest_start: 'string', | ||
harvest_end: 'string', | ||
beginner_friendly: true, | ||
plant_tips: 'string', | ||
img: 'string', | ||
difficulty_level: 'HARD', | ||
sunlight_min_hours: 1, | ||
sunlight_max_hours: 1, | ||
}, | ||
]; | ||
const user_id: UUID = '0802d796-ace8-480d-851b-d16293c74a21'; | ||
import COLORS from '@/styles/colors'; | ||
import { Flex } from '@/styles/containers'; | ||
import { H1, P1 } from '@/styles/text'; | ||
import { UserPlant } from '@/types/schema'; | ||
import { useAuth } from '@/utils/AuthProvider'; | ||
import { useProfile } from '@/utils/ProfileProvider'; | ||
import { ButtonDiv, FooterButton, MoveButton } from './styles'; | ||
|
||
export default function Home() { | ||
const { profileData, profileReady, plantsToAdd } = useProfile(); | ||
const { userId } = useAuth(); | ||
const router = useRouter(); | ||
|
||
if (profileReady && !profileData) { | ||
router.push('/view-plants'); | ||
} | ||
const [currentIndex, setCurrentIndex] = useState<number>(1); | ||
const [details, setDetails] = useState<Partial<UserPlant>[]>( | ||
plants.map(plant => ({ plant_id: plant.id, user_id: user_id })), | ||
plantsToAdd.map(plant => ({ plant_id: plant.id, user_id: userId! })), | ||
); | ||
const router = useRouter(); | ||
|
||
const getDefaultDate = () => new Date().toISOString().substring(0, 10); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. note: this date is a little funky. it's 1:48am 12/13, but the plant i just added says 12/12. Perhaps we should look into other date formatting options e.g. |
||
|
||
|
@@ -71,28 +34,24 @@ export default function Home() { | |
// Set curr date in details to default date if not on submission page | ||
if ( | ||
(!currentDetail || !currentDetail.date_added) && | ||
currentIndex <= plants.length | ||
currentIndex <= plantsToAdd.length | ||
) { | ||
updateInput('date_added', getDefaultDate()); | ||
} | ||
// For valid moves, move to next page | ||
if ( | ||
steps !== 0 && | ||
currentIndex + steps > 0 && | ||
currentIndex + steps <= plants.length + 1 | ||
currentIndex + steps <= plantsToAdd.length + 1 | ||
) { | ||
setCurrentIndex(prevIndex => prevIndex + steps); | ||
} | ||
} | ||
|
||
function disableNext() { | ||
// disable next if planting type is "SELECT" or undefined | ||
return !( | ||
details[currentIndex - 1].planting_type | ||
// requires refactor of details to ensure that planting_type is PlantingTypeEnum | ||
// && details[currentIndex - 1].planting_type !== 'SELECT' | ||
); | ||
} | ||
// disable next if planting type not selected (undefined) | ||
const disableNext = | ||
currentIndex <= plantsToAdd.length && | ||
!details[currentIndex - 1].planting_type; | ||
|
||
function updateInput(field: string, value: string) { | ||
const updatedDetails = [...details]; | ||
|
@@ -102,38 +61,74 @@ export default function Home() { | |
}; | ||
setDetails(updatedDetails); | ||
} | ||
|
||
// const handleSubmit = async () => { | ||
// try { | ||
// await insertUserPlants(userId!, details); | ||
// router.push('/view-plants'); | ||
// } catch (error) { | ||
// console.error('Error inserting user plants:', error); | ||
// // Optionally, add user-facing error handling | ||
// } | ||
// }; | ||
async function updateDB() { | ||
await insertUserPlants(user_id, details); | ||
await insertUserPlants(userId!, details); | ||
router.push('/view-plants'); | ||
} | ||
|
||
return ( | ||
<div> | ||
{currentIndex !== plants.length + 1 && ( | ||
<div> | ||
<PlantDetails | ||
plant={plants[currentIndex - 1]} | ||
date={details[currentIndex - 1].date_added || getDefaultDate()} | ||
plantingType={details[currentIndex - 1].planting_type || 'SELECT'} | ||
onDateChange={date => updateInput('date_added', date)} | ||
onPlantingTypeChange={type => updateInput('planting_type', type)} | ||
/> | ||
<button onClick={() => move(-1)}>Back</button> | ||
<p> | ||
{currentIndex} / {plants.length} | ||
</p> | ||
<button disabled={disableNext()} onClick={() => move(1)}> | ||
Next | ||
</button> | ||
</div> | ||
<> | ||
{currentIndex !== plantsToAdd.length + 1 && ( | ||
<Flex $direction="column" $justify="between"> | ||
<Flex $direction="column" $justify="start"> | ||
<Flex $gap="16px" $direction="column" $textAlign="center"> | ||
<H1 $color={COLORS.shrub}>Add Plant Details</H1> | ||
<P1 $color={COLORS.midgray}> | ||
{currentIndex} / {plantsToAdd.length} | ||
</P1> | ||
</Flex> | ||
<PlantDetails | ||
plant={plantsToAdd[currentIndex - 1]} | ||
date={details[currentIndex - 1].date_added || getDefaultDate()} | ||
plantingType={details[currentIndex - 1].planting_type || 'SELECT'} | ||
onDateChange={date => updateInput('date_added', date)} | ||
onPlantingTypeChange={type => updateInput('planting_type', type)} | ||
/> | ||
</Flex> | ||
<FooterButton> | ||
<ButtonDiv> | ||
{currentIndex > 1 && ( | ||
<MoveButton | ||
type="button" | ||
onClick={() => move(-1)} | ||
$secondaryColor={COLORS.shrub} | ||
> | ||
Back | ||
</MoveButton> | ||
)} | ||
|
||
<MoveButton | ||
type="button" | ||
disabled={disableNext} | ||
onClick={() => move(1)} | ||
$primaryColor={disableNext ? COLORS.midgray : COLORS.shrub} | ||
$secondaryColor="white" | ||
> | ||
Next | ||
</MoveButton> | ||
</ButtonDiv> | ||
</FooterButton> | ||
</Flex> | ||
)} | ||
{currentIndex === plants.length + 1 && ( | ||
{currentIndex === plantsToAdd.length + 1 && ( | ||
<div> | ||
<button onClick={() => move(-1)}>Back</button> | ||
<button onClick={updateDB}>Submit</button> | ||
<button type="button" onClick={() => move(-1)}> | ||
Back | ||
</button> | ||
<button type="button" onClick={updateDB}> | ||
Submit | ||
</button> | ||
</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,34 @@ | ||
import styled from 'styled-components'; | ||
import { SmallRoundedButton } from '@/components/Button'; | ||
|
||
export const MoveButton = styled(SmallRoundedButton)` | ||
border: 1px solid; | ||
font-family: inherit; | ||
margin-bottom: 10px; | ||
width: 170px; | ||
height: 50px; | ||
font-size: 15px; | ||
font-style: normal; | ||
font-weight: 400; | ||
line-height: normal; | ||
`; | ||
export const FooterButton = styled.div` | ||
display: flex; | ||
flex-direction: row; | ||
justify-content: end; | ||
width: 100%; | ||
position: absolute; | ||
bottom: 0; | ||
left: 50%; | ||
transform: translateX(-50%); | ||
padding: 24px; | ||
`; | ||
|
||
export const ButtonDiv = styled.div` | ||
display: flex; | ||
width: 100%; | ||
justify-content: space-between; | ||
&:has(:only-child) { | ||
justify-content: flex-end; | ||
} | ||
`; |
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
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
note: we should replace all routes with CONFIG
e.g.