Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
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
6 changes: 5 additions & 1 deletion src/components/infinite-banner-list/InfiniteBannerList.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { FC, useState } from 'react'
import { FC, useEffect, useState } from 'react'
import { BannerFilter } from '../../features/banner/filter'
import { useBannerList } from '../../features/banner/hooks'
import BannerList from '../banner-list'
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 @@ -19,4 +21,5 @@ export type BannerFilter = {
proximityLatitude?: number
proximityLongitude?: number
author?: string
listTypes?: BannerListType
}
Loading