Skip to content

Commit

Permalink
Refactor UserBannerList to functional component
Browse files Browse the repository at this point in the history
  • Loading branch information
ewoerner committed Sep 23, 2024
1 parent 6581e83 commit ab2d460
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 243 deletions.
41 changes: 18 additions & 23 deletions src/components/banner-order-chooser/BannerOrderChooser.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,14 @@ const BannerOrderChooser: FC<BannerOrderChooserProps> = ({
}) => {
const [open, setOpen] = useState<boolean>(false)
const [loadingLocation, setLoadingLocation] = useState<boolean>(false)
const [currentFilter, setCurrentFilter] = useState<BannerFilter>(filter)
const { t } = useTranslation()
useEffect(() => {
if (loadingLocation) {
navigator.geolocation.getCurrentPosition(
(pos) => {
setLoadingLocation(false)
updateFilter({
...currentFilter,
...filter,
orderBy: 'proximityStartPoint',
orderDirection: 'ASC',
proximityLatitude: pos.coords.latitude,
Expand All @@ -40,7 +39,7 @@ const BannerOrderChooser: FC<BannerOrderChooserProps> = ({
{ maximumAge: 0, enableHighAccuracy: true }
)
}
})
}, [loadingLocation, setLoadingLocation, filter])

const show = () => {
setOpen(true)
Expand All @@ -52,7 +51,6 @@ const BannerOrderChooser: FC<BannerOrderChooserProps> = ({
}

const updateFilter = (newFilter: BannerFilter) => {
setCurrentFilter(newFilter)
onFilterChanged(newFilter)
}

Expand All @@ -68,51 +66,51 @@ const BannerOrderChooser: FC<BannerOrderChooserProps> = ({
}

const onOrderClicked = (type: BannerOrder) => {
if (type !== currentFilter.orderBy) {
if (type !== filter.orderBy) {
if (type === 'proximityStartPoint') {
setLoadingLocation(true)
} else {
updateFilter({
...currentFilter,
...filter,
orderBy: type,
orderDirection: getDefaultDirection(type),
proximityLatitude: undefined,
proximityLongitude: undefined,
})
}
} else if (hasBothDirections(currentFilter.orderBy)) {
} else if (hasBothDirections(filter.orderBy)) {
updateFilter({
...currentFilter,
orderDirection: currentFilter.orderDirection === 'ASC' ? 'DESC' : 'ASC',
...filter,
orderDirection: filter.orderDirection === 'ASC' ? 'DESC' : 'ASC',
})
}
}

const onOfficialChanged = (includeUnofficial: boolean) => {
updateFilter({
...currentFilter,
...filter,
onlyOfficialMissions: includeUnofficial ? undefined : true,
})
}

const onOnlineChanged = (showOffline: boolean) => {
updateFilter({
...currentFilter,
...filter,
online: showOffline ? undefined : true,
})
}

const getButtonClass = (type: BannerOrder) => {
let classNames = 'order-button'
if (type === currentFilter.orderBy) {
if (type === filter.orderBy) {
classNames += ' selected'
}
return classNames
}

const getButtonDirection = (type: BannerOrder) => {
return type === currentFilter.orderBy
? currentFilter.orderDirection
return type === filter.orderBy
? filter.orderDirection
: getDefaultDirection(type)
}

Expand Down Expand Up @@ -147,16 +145,13 @@ const BannerOrderChooser: FC<BannerOrderChooserProps> = ({
</h2>
<div className="filter-and-sort-switch-row">
<h3>{t('order.showOfflineBanners')}</h3>
<Switch
checked={!currentFilter.online}
onChange={onOnlineChanged}
/>
<Switch checked={!filter.online} onChange={onOnlineChanged} />
</div>
{includeOfficial && (
<div className="filter-and-sort-switch-row">
<h3>{t('order.showUnofficialBanners')}</h3>
<Switch
checked={!currentFilter.onlyOfficialMissions}
checked={!filter.onlyOfficialMissions}
onChange={onOfficialChanged}
/>
</div>
Expand Down Expand Up @@ -190,19 +185,19 @@ const BannerOrderChooser: FC<BannerOrderChooserProps> = ({
components={{
order: (
<Order
orderBy={currentFilter.orderBy}
orderDirection={currentFilter.orderDirection}
orderBy={filter.orderBy}
orderDirection={filter.orderDirection}
/>
),
}}
/>
{' / '}
</>
)}
{currentFilter.online
{filter.online
? t('order.text.excludeOffline')
: t('order.text.includeOffline')}
{currentFilter.onlyOfficialMissions && (
{filter.onlyOfficialMissions && (
<>
{' / '}
{t('order.onlyOfficial')}
Expand Down
8 changes: 6 additions & 2 deletions src/components/infinite-banner-list/InfiniteBannerList.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { FC, useState } from 'react'
import { FC, useEffect, useState } from 'react'
import { BannerFilter } from '../../features/banner/filter'
import { useBannerList } from '../../features/banner/hooks'
import { useBannerList, usePrevious } from '../../features/banner/hooks'

Check failure on line 3 in src/components/infinite-banner-list/InfiniteBannerList.tsx

View workflow job for this annotation

GitHub Actions / build (18.x)

Module '"../../features/banner/hooks"' declares 'usePrevious' locally, but it is not exported.

Check failure on line 3 in src/components/infinite-banner-list/InfiniteBannerList.tsx

View workflow job for this annotation

GitHub Actions / build (18.x)

Module '"../../features/banner/hooks"' declares 'usePrevious' locally, but it is not exported.
import BannerList from '../banner-list'
import BannerOrderChooser from '../banner-order-chooser'

Expand All @@ -19,6 +19,10 @@ const InfiniteBannerList: FC<InfiniteBannerListProps> = ({
setMaxPages(1)
}

useEffect(() => {
onFilterChanged(initialFilter)
}, [initialFilter])

return (
<div>
<BannerOrderChooser
Expand Down
3 changes: 3 additions & 0 deletions src/features/banner/filter.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { BannerListType } from './types'

export type BannerOrder =
| 'relevance'
| 'listAdded'
Expand All @@ -18,4 +20,5 @@ export type BannerFilter = {
maxEventTimestamp?: string
proximityLatitude?: number
proximityLongitude?: number
listTypes?: BannerListType
}
Loading

0 comments on commit ab2d460

Please sign in to comment.