beherbergung/frontend/search/components/ngo/HostOfferLookupWrapper.tsx

69 lines
2.9 KiB
TypeScript
Raw Normal View History

2022-03-14 19:22:17 +01:00
import React, { useEffect } from 'react'
import { useGetOffersQuery, useGetRwQuery } from "../../codegen/generates"
2022-03-14 14:22:03 +01:00
import HostOfferLookupTable, {HostOfferLookupTableProps} from "./HostOfferLookupTable"
import { Box } from "@mui/material"
import { useTranslation } from 'react-i18next'
import { Login, useAuthStore } from '../Login'
2022-03-14 19:22:17 +01:00
import { useLeafletStore } from './LeafletStore'
import { filterUndefOrNull } from '../util/notEmpty'
2022-03-14 21:08:55 +01:00
import { haversine_distance } from '../util/distance'
2022-03-09 15:39:15 +01:00
2022-03-14 14:22:03 +01:00
type HostOfferLookupWrapperProps = Partial<HostOfferLookupTableProps>
2022-03-09 15:39:15 +01:00
2022-03-14 21:08:55 +01:00
function floor(v: number|undefined) {
return v && Math.floor(v)
}
2022-03-14 14:22:03 +01:00
const HostOfferLookupWrapper = (props: HostOfferLookupWrapperProps) => {
const { t } = useTranslation()
const auth = useAuthStore()
const staleTimeMinutes_ro = 5
const staleTimeMinutes_rw = 1
2022-03-14 19:22:17 +01:00
const queryResult_ro = useGetOffersQuery({auth}, {enabled: !!auth.jwt, staleTime: staleTimeMinutes_ro * 60 * 1000})
const queryResult_rw = useGetRwQuery({auth}, {enabled: !!auth.jwt, staleTime: staleTimeMinutes_rw * 60 * 1000})
const {data: data_ro} = queryResult_ro
const {data: data_rw} = queryResult_rw
2022-03-14 21:08:55 +01:00
const {setMarkers, center} = useLeafletStore()
2022-03-14 19:22:17 +01:00
useEffect(() => {
const markers = data_ro?.get_offers?.map(row => (row.id && row.place_lon && row.place_lat
2022-03-14 21:08:55 +01:00
&& ({id: row.id,
2022-03-14 19:22:17 +01:00
lat: row.place_lat,
lng: row.place_lon,
radius: 1000, // TODO
content: 'TODO'}) || undefined))
2022-03-14 21:08:55 +01:00
setMarkers(filterUndefOrNull(markers))
2022-03-14 19:22:17 +01:00
}, [data_ro])
2022-03-09 21:58:51 +01:00
2022-03-14 21:08:55 +01:00
const data_ro_withDistance = data_ro?.get_offers?.map(r => ({...r,
place_distance: floor(haversine_distance(center?.lat, center?.lng, r.place_lat, r.place_lon))}))
2022-03-10 11:50:33 +01:00
return <>
2022-03-14 13:27:20 +01:00
<Box sx={{
display: 'flex',
alignItems: 'stretch',
flexDirection: 'column',
2022-03-14 13:33:33 +01:00
height: '100%'}}>
<div style={{minHeight: '2em', display: 'flex'}}>
{ (queryResult_ro.isFetching || queryResult_rw.isFetching) && t('loading…') }
{ (queryResult_ro.error || queryResult_rw.error) && t('An error occurred while trying to get data from the backend.') }
2022-03-14 19:22:17 +01:00
{ (data_ro && !data_ro.get_offers || data_rw && !data_rw.get_rw)
2022-03-14 13:27:20 +01:00
&& t('Seems like you have no permissions. Please try to login again.') }
<Login/>
</div>
2022-03-14 19:22:17 +01:00
{data_ro && <div
2022-03-11 11:25:02 +01:00
style={{flex: '1 1', height: '100%'}}>
2022-03-14 14:22:03 +01:00
<HostOfferLookupTable
{...props}
2022-03-14 21:08:55 +01:00
data_ro={data_ro_withDistance}
2022-03-14 19:22:17 +01:00
data_rw={data_rw?.get_rw}
2022-03-14 14:22:03 +01:00
refetch_rw={queryResult_rw.refetch}
/>
2022-03-14 13:27:20 +01:00
</div>}
</Box>
2022-03-10 11:50:33 +01:00
</>
2022-03-09 15:39:15 +01:00
}
export default HostOfferLookupWrapper