Skip to content

Commit

Permalink
Allow type labels to be null if types load after map locations
Browse files Browse the repository at this point in the history
  • Loading branch information
wbazant committed Oct 25, 2024
1 parent 32743e8 commit 66d53f8
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 23 deletions.
6 changes: 3 additions & 3 deletions src/components/map/Location.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,20 +42,20 @@ const TooltipLabel = styled(MapLabel)`
`

const Location = memo(
({ showLabel, types, selected, editing, onClick, ...props }) => (
({ showLabel, typeIds, selected, editing, onClick, ...props }) => (
<>
{selected && !editing && <MapPin />}
{editing && <BackgroundMapPin />}
<LocationButton onClick={onClick} {...props}>
{!showLabel && (
<TooltipLabel>
<TypeLabels types={types} />
<TypeLabels typeIds={typeIds} />
</TooltipLabel>
)}
</LocationButton>
{showLabel && (
<MapLabel>
<TypeLabels types={types} />
<TypeLabels typeIds={typeIds} />
</MapLabel>
)}
</>
Expand Down
3 changes: 1 addition & 2 deletions src/components/map/MapPage.js
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,6 @@ const MapPage = ({ isDesktop }) => {
const history = useAppHistory()
const dispatch = useDispatch()
const handleViewChangeRef = useRef(() => void 0)
const typesAccess = useSelector((state) => state.type.typesAccess)

const [draggedPosition, setDraggedPosition] = useState(null)

Expand Down Expand Up @@ -426,7 +425,7 @@ const MapPage = ({ isDesktop }) => {
selected={location.id === locationId}
editing={isEditingLocation && location.id === locationId}
showLabel={showLabels}
types={location.type_ids.map((id) => typesAccess.getType(id))}
typeIds={location.type_ids}
/>
))}
{(isEditingLocation || isAddingLocation) && draggedPosition && (
Expand Down
32 changes: 14 additions & 18 deletions src/components/map/TypeLabels.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,31 +15,27 @@ const ScientificName = styled(TypeItem)`
font-style: italic;
`

const TypeLabels = ({ types }) => {
const TypeLabels = ({ typeIds }) => {
const { types: selectedTypes } = useSelector((state) => state.filter)

const typeLabels =
types && types.length > 0
? types.map((type) => ({
label: type.commonName || type.scientificName,
isScientific: !type.commonName && type.scientificName,
isSelected: selectedTypes.includes(type.id),
}))
: []
const typesAccess = useSelector((state) => state.type.typesAccess)

return (
<TypeList>
{typeLabels.map((item, index) =>
item.isScientific ? (
<ScientificName key={index} isSelected={item.isSelected}>
{item.label}
{(typeIds || []).map((id, index) => {
const type = typesAccess.getType(id)
return !type ? null : !type.commonName && type.scientificName ? (
<ScientificName
key={index}
isSelected={selectedTypes.includes(type.id)}
>
{type.scientificName}
</ScientificName>
) : (
<TypeItem key={index} isSelected={item.isSelected}>
{item.label}
<TypeItem key={index} isSelected={selectedTypes.includes(type.id)}>
{type.commonName || type.scientificName}
</TypeItem>
),
)}
)
})}
</TypeList>
)
}
Expand Down

0 comments on commit 66d53f8

Please sign in to comment.