Skip to content
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] Create Custom Multi-Select Dropdown and Style Seasonal Planting Guide #62

Open
wants to merge 28 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
89dc29f
applying styling to ReactMultiSelect component
kylezryr Dec 3, 2024
490fc33
modifying seasonal planting guide page styles
kylezryr Dec 4, 2024
5bd2a96
refactor multi dropdown to react-select
kylezryr Dec 4, 2024
4e8f4ff
apply styling to react-select
kylezryr Dec 5, 2024
a75868d
temp refactor filterdropdownsingle
kylezryr Dec 6, 2024
95243f6
refactored FilterDropdownSingle to use Select
kylezryr Dec 7, 2024
605bc82
fix lint errors
kylezryr Dec 7, 2024
5b001cb
remove unused imports
kylezryr Dec 7, 2024
1ec4cb3
finalise filter stylings
kylezryr Dec 7, 2024
e111a96
move MonthHeader to inside PlantCalendarList
kylezryr Dec 7, 2024
5c76ed4
make PlantCalendarList scrollable
kylezryr Dec 7, 2024
46fa944
fix menu overlay issue
kylezryr Dec 7, 2024
25773d3
create plant card key
kylezryr Dec 7, 2024
c90c0cf
adjust seasonal planting guide styles
kylezryr Dec 7, 2024
ef2f1c6
rebase and fix lint errors
kylezryr Dec 7, 2024
783e913
uninstall multi-select, change growing season to only filter outdoor …
kylezryr Dec 7, 2024
a34cd15
fix lint error
kylezryr Dec 8, 2024
a0968ae
lint error
kylezryr Dec 8, 2024
8a333f0
fix onChange console error
kylezryr Dec 13, 2024
6605a47
add missing dependencies to pnpm-lock
kylezryr Dec 13, 2024
f5b9569
reinstall pnpm lock
kylezryr Dec 13, 2024
6f031e1
fix plantcardkey horizontalline styling
kylezryr Dec 13, 2024
284883c
change header link styles
ccatherinetan Dec 29, 2024
9e06512
rename useTitleCase to toTitleCase; style Clear Filters button
ccatherinetan Dec 29, 2024
293eccd
prevent whitespace collapse in filter text
ccatherinetan Dec 29, 2024
954d7de
add instanceId to both dropdowns
ccatherinetan Dec 30, 2024
f41df1b
clean up PlantCardKey styling
ccatherinetan Dec 30, 2024
7906153
fix text styling of dropdown options
ccatherinetan Dec 30, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion app/plant-page/my-garden/[userPlantId]/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,6 @@ export default function UserPlantPage() {

<Flex $direction="column" $gap="8px">
<H4>Planting Timeline</H4>
{/*add SeasonalColorKey here */}
<PlantCalendarRow
harvestStart={currentPlant.harvest_start}
harvestEnd={currentPlant.harvest_end}
Expand Down
25 changes: 17 additions & 8 deletions app/seasonal-planting-guide/page.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
'use client';

import React, { useEffect, useState } from 'react';
import { SmallButton } from '@/components/Buttons';
import FilterDropdownMultiple from '@/components/FilterDropdownMultiple';
import FilterDropdownSingle from '@/components/FilterDropdownSingle';
import { PlantCalendarList } from '@/components/PlantCalendarList';
Expand All @@ -15,13 +16,15 @@ import {
seasonOptions,
usStateOptions,
} from '@/utils/dropdownOptions';
import { toTitleCase } from '@/utils/helpers';
import { useProfile } from '@/utils/ProfileProvider';
import {
FilterContainer,
HeaderContainer,
PageContainer,
PageTitle,
StateOptionsContainer,
VerticalSeparator,
} from './styles';

// (static) filter options imported from utils/dropdownOptions
Expand All @@ -39,7 +42,8 @@ export default function SeasonalPlantingGuide() {
const [selectedPlantingType, setSelectedPlantingType] = useState<
DropdownOption<PlantingTypeEnum>[]
>([]);
const [selectedUsState, setSelectedUsState] = useState<string>('');
const [selectedUsState, setSelectedUsState] =
useState<DropdownOption<string> | null>(null);
const [searchTerm, setSearchTerm] = useState<string>('');

const clearFilters = () => {
Expand All @@ -50,7 +54,10 @@ export default function SeasonalPlantingGuide() {

useEffect(() => {
if (profileReady && profileData) {
setSelectedUsState(profileData.us_state);
setSelectedUsState({
label: toTitleCase(profileData.us_state),
value: profileData.us_state,
});
}
}, [profileData, profileReady]);

Expand All @@ -65,14 +72,17 @@ export default function SeasonalPlantingGuide() {
<SearchBar searchTerm={searchTerm} setSearchTerm={setSearchTerm} />
<FilterContainer>
<FilterDropdownSingle
id="usState"
value={selectedUsState}
setStateAction={setSelectedUsState}
placeholder="State"
options={usStateOptions}
disabled={!selectedUsState}
small={true}
/>

{/* vertical bar to separate state and other filters */}
<VerticalSeparator />

<FilterDropdownMultiple
value={selectedGrowingSeason}
setStateAction={setSelectedGrowingSeason}
Expand All @@ -96,24 +106,23 @@ export default function SeasonalPlantingGuide() {
placeholder="Planting Type"
disabled={!selectedUsState}
/>

<button onClick={clearFilters}>Clear filters</button>
<SmallButton $secondaryColor={COLORS.shrub} onClick={clearFilters}>
Clear Filters
</SmallButton>
</FilterContainer>
</HeaderContainer>
{!selectedUsState ? (
<StateOptionsContainer>
<H3 $color={COLORS.shrub}>Choose Your State</H3>
<FilterDropdownSingle
name="usState"
id="usState"
value={selectedUsState}
setStateAction={setSelectedUsState}
placeholder="State"
options={usStateOptions}
/>
</StateOptionsContainer>
) : (
<Box $pl="16px" $pt="12px">
<Box $p="20px">
<SeasonColorKey />
<PlantCalendarList
growingSeasonFilterValue={selectedGrowingSeason}
Expand Down
22 changes: 19 additions & 3 deletions app/seasonal-planting-guide/styles.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import styled from 'styled-components';
import COLORS from '@/styles/colors';

export const PageContainer = styled.div`
display: flex;
Expand All @@ -10,27 +11,36 @@ export const PageContainer = styled.div`
export const HeaderContainer = styled.div`
display: flex;
flex-direction: column;
padding: 2px 24px 20px 24px;
padding: 2px 24px 0 24px;
box-shadow: 0px 2px 4px 0px rgba(0, 0, 0, 0.1);
background-color: #fff;
position: relative;
z-index: 2;
`;

//TODO: consolidate styling for Filters in view plants and seasonal planting guide
export const FilterContainer = styled.div`
display: flex;
flex-direction: row;
gap: 0.5rem;
white-space: nowrap; // Prevent line break
gap: 8px;
margin-top: 12px;
margin-bottom: 20px;
padding-top: 1px;
padding-bottom: 1px; // ensure filter border isn't cut off
position: relative;
overflow-x: auto;
align-items: center;
`;

export const StateOptionsContainer = styled.div`
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
gap: 1rem;
gap: 16px;
flex-grow: 1;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ooo this is a great handle!

we can probably do smth similar in the /add-details and /onboarding screens, which currently have their height set to 100vh - 60px (subtracting the height of the header), but i think flex-grow is a more elegant solution

background-color: ${COLORS.glimpse};
`;

export const PageTitle = styled.div`
Expand All @@ -40,3 +50,9 @@ export const PageTitle = styled.div`
gap: 12px;
align-items: center;
`;

export const VerticalSeparator = styled.div`
height: 30px;
width: 1px;
background-color: ${COLORS.lightgray};
`;
53 changes: 49 additions & 4 deletions app/view-plants/page.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use client';

import { useEffect, useMemo, useState } from 'react';
import { useEffect, useMemo, useRef, useState } from 'react';
import { useRouter } from 'next/navigation';
import {
getAllPlants,
Expand All @@ -11,6 +11,7 @@ import { Button, SmallButton } from '@/components/Buttons';
import FilterDropdownMultiple from '@/components/FilterDropdownMultiple';
import Icon from '@/components/Icon';
import PlantCard from '@/components/PlantCard';
import PlantCardKey from '@/components/PlantCardKey';
import SearchBar from '@/components/SearchBar';
import CONFIG from '@/lib/configs';
import COLORS from '@/styles/colors';
Expand All @@ -35,6 +36,7 @@ import {
AddButtonContainer,
FilterContainer,
HeaderButton,
InfoButton,
NumberSelectedPlants,
NumberSelectedPlantsContainer,
PlantGridContainer,
Expand Down Expand Up @@ -84,6 +86,9 @@ export default function Page() {
const [searchTerm, setSearchTerm] = useState<string>('');
const [selectedPlants, setSelectedPlants] = useState<Plant[]>([]);
const [ownedPlants, setOwnedPlants] = useState<OwnedPlant[]>([]);
const [isCardKeyOpen, setIsCardKeyOpen] = useState<boolean>(false);
Copy link
Collaborator

@ccatherinetan ccatherinetan Dec 23, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

optimization note: each time isCardKeyOpen changes, the entire page will re-render, which is expensive and unnecessary, so we should consider

  1. maybe separate out the PlantCardList out into a separate component (and wrap it in a memo), or
  2. wrap the PlantCardList section (i.e. everything under the title) in a useMemo? -- this would do the same thing but would mitigate having to define a new component

for more on re-rendering / how to prevent it: https://www.developerway.com/posts/react-re-renders-guide

however, we've already wrapped PlantCard in a memo, so maybe it's chill? something to consider tho!

const cardKeyRef = useRef<HTMLDivElement>(null);
const infoButtonRef = useRef<HTMLButtonElement>(null);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

honestly i think it's ok if we close the card anytime someone clicks outside of the cardKey, so we can probably remove the infoButtonRef?

const userState = profileData?.us_state ?? null;

const profileAndAuthReady = profileReady && !authLoading;
Expand Down Expand Up @@ -378,12 +383,52 @@ export default function Page() {

const plantPluralityString = selectedPlants.length > 1 ? 'Plants' : 'Plant';

// close plant card key when clicking outside, even on info button
const handleClickOutside = (event: MouseEvent) => {
if (
cardKeyRef.current &&
!cardKeyRef.current.contains(event.target as Node) &&
infoButtonRef.current &&
!infoButtonRef.current.contains(event.target as Node)
) {
setIsCardKeyOpen(false);
}
};

// handle clicking outside PlantCardKey to close it if open
useEffect(() => {
if (isCardKeyOpen) {
document.addEventListener('mousedown', handleClickOutside);
} else {
document.removeEventListener('mousedown', handleClickOutside);
}

return () => {
document.removeEventListener('mousedown', handleClickOutside);
};
}, [isCardKeyOpen]);

return (
<div id="plantContent">
<TopRowContainer>
<H1 $color={COLORS.shrub} $fontWeight={500}>
View Plants
</H1>
<Flex $direction="row" $gap="10px" $align="center">
<H1 $color={COLORS.shrub} $fontWeight={500}>
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: can we get the TopRowContainer to cover the dropshadow from the Header?
Screen Shot 2024-12-29 at 10 38 55 PM

View Plants
</H1>
<div style={{ position: 'relative' }}>
<InfoButton
onClick={() => setIsCardKeyOpen(!isCardKeyOpen)}
ref={infoButtonRef}
>
<Icon type="info" />
</InfoButton>
{isCardKeyOpen && (
<div ref={cardKeyRef}>
<PlantCardKey />
</div>
Comment on lines +426 to +428
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: instead of wrapping PlantCardKey in other div, it might be cleaner to use forwardRef, which would look smth like this:

export const PlantCardKey = forwardRef<HTMLDivElement>((props, ref) => {...} 
PlantCardKey.displayName = 'PlantCardKey';

HOWEVER, apparently, in React 19 (we're using React 18.3) you can directly pass ref as a prop, so maybe we can hold off on this change.
Apparently React 19 might introduce some breaking changes, so upgrading might be done in a separate PR

)}
</div>
</Flex>
<SearchBar searchTerm={searchTerm} setSearchTerm={setSearchTerm} />
<FilterContainer>
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Screen Shot 2024-12-29 at 10 50 24 PM

In View Plants, the upper/lower borders of the filters are being cutoff. This issue is already addressed in the Planting Timeline page by adding 1px padding to top and bottom. I think this is another argument for consolidating the filters in View Plants and Planting Timeline into a cobined component: FilterGroup which can take in props: CompleteFilter[], where CompleteFilter is typed

filterProps: FilterDropdownProps<T> // coming from FilterDropdownMultiple
checkFilterMatch: (plant: Plant) => boolean,

Then we could refactor filteredPlantList like so

// filterFuncs = completeFilters.map(cf => cf.checkFilterMatch);
const filteredPlantList = useMemo(() => 
  plants.filter(plant =>
      filterFuncs.every(fn => fn(plant));
), [filterFuncs];

this can be done in a separate PR, but I think consolidating the filters into a single component would be cleaner, since it creates a single source of truth and keep styling consistent

<FilterDropdownMultiple
Expand Down
10 changes: 10 additions & 0 deletions app/view-plants/styles.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ export const FilterContainer = styled.div`
gap: 8px;
margin-bottom: 20px;
align-items: center;
overflow-x: auto;
`;

export const TopRowContainer = styled.div`
Expand Down Expand Up @@ -74,3 +75,12 @@ export const NumberSelectedPlants = styled.p`
line-height: 16px;
color: #fff;
`;

export const InfoButton = styled.button`
display: flex;
align-items: center;
justify-content: center;
background: none;
border: none;
cursor: pointer;
`;
46 changes: 0 additions & 46 deletions components/Button.tsx

This file was deleted.

2 changes: 1 addition & 1 deletion components/Buttons.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,6 @@ export const SmallButton = styled(P3).attrs({ as: 'button' })<ButtonProps>`
// Unique to Small Button
border-radius: 20px;
min-width: 60px;
height: 24px;
min-height: 24px; // to prevent Clear Filters text overflow
padding: 4px 10px;
`;
Loading
Loading