Skip to content

Commit

Permalink
fixup! fix: mf-6493 add missing location
Browse files Browse the repository at this point in the history
  • Loading branch information
swkatmask committed Nov 22, 2024
1 parent 2180746 commit e22a7ef
Show file tree
Hide file tree
Showing 6 changed files with 59 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ interface Props extends HTMLProps<HTMLDivElement> {
export function CalendarContent(props: Props) {
const { classes, cx } = useStyles()
const [currentTab, onChange, tabs] = useTabs('news', 'events')
const [date, setDate] = useState(() => new Date())
const [date, setDate] = useState(() => new Date(Math.floor(Date.now() / 1000) * 1000)) // round to seconds
const [open, setOpen] = useState(false)

const [allowedDates, setAllowedDates] = useState<string[]>(EMPTY_LIST)
Expand Down
16 changes: 14 additions & 2 deletions packages/plugins/Calendar/src/SiteAdaptor/components/EventList.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Trans } from '@lingui/macro'
import { ElementAnchor, EmptyStatus, Image, LoadingStatus } from '@masknet/shared'
import { ElementAnchor, EmptyStatus, Image, LoadingStatus, ReloadStatus } from '@masknet/shared'
import { EMPTY_LIST } from '@masknet/shared-base'
import { LoadingBase, makeStyles } from '@masknet/theme'
import { resolveIPFS_URL } from '@masknet/web3-shared-base'
Expand Down Expand Up @@ -99,7 +99,7 @@ interface EventListProps {

export function EventList({ date, onDatesUpdate }: EventListProps) {
const { classes, cx } = useStyles()
const { isLoading, isFetching, data, hasNextPage, fetchNextPage } = useLumaEvents()
const { isLoading, isFetching, data, error, hasNextPage, fetchNextPage } = useLumaEvents(date)

const comingEvents = useMemo(() => {
if (!data) return EMPTY_LIST
Expand All @@ -122,6 +122,18 @@ export function EventList({ date, onDatesUpdate }: EventListProps) {
</div>
)
}

if (error) {
return (
<div className={classes.container}>
<div className={classes.paddingWrap}>
<div className={cx(classes.empty, classes.eventTitle)}>
<ReloadStatus message={error.message}></ReloadStatus>
</div>
</div>
</div>
)
}
if (!comingEvents.length) {
return (
<div className={classes.container}>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ interface NewsListProps {
}

export function NewsList({ date, onDatesUpdate }: NewsListProps) {
const { data: list = EMPTY_OBJECT, isLoading } = useNewsList(date, true)
const { data: list = EMPTY_OBJECT, isLoading } = useNewsList(date)
const dateString = date.toLocaleDateString()
const empty = !Object.keys(list).length
const { classes, cx } = useStyles()
Expand Down
5 changes: 2 additions & 3 deletions packages/plugins/Calendar/src/hooks/useEventList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,9 @@ import { useQuery } from '@tanstack/react-query'
import { addDays, startOfMonth } from 'date-fns'

export function useNewsList(date: Date) {
const startTime = startOfMonth(date).getTime() / 1000
const endTime = Math.floor(addDays(date, 45).getTime() / 1000)
const startTime = startOfMonth(date).getTime()
const endTime = addDays(date, 45).getTime()
return useQuery({
enabled,
queryKey: ['newsList', startTime, endTime],
queryFn: async () => Calendar.getNewsList(startTime, endTime),
select(data) {
Expand Down
9 changes: 6 additions & 3 deletions packages/plugins/Calendar/src/hooks/useLumaEvents.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
import { Calendar } from '@masknet/web3-providers'
import { useInfiniteQuery } from '@tanstack/react-query'
import { addDays, startOfMonth } from 'date-fns'

export function useLumaEvents() {
export function useLumaEvents(date: Date) {
const startTime = startOfMonth(date).getTime()
const endTime = Math.floor(addDays(date, 45).getTime())
return useInfiniteQuery({
queryKey: ['lumaEvents'],
queryKey: ['lumaEvents', startTime, endTime],
initialPageParam: undefined as any,
queryFn: async ({ pageParam }) => {
return Calendar.getEventList(pageParam)
return Calendar.getEventList(startTime, endTime, pageParam)
},
getNextPageParam(page) {
return page.nextIndicator
Expand Down
50 changes: 35 additions & 15 deletions packages/web3-providers/src/Calendar/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,22 +13,40 @@ function fixEventDate(event: Event): ParsedEvent {
}
}

function fixEvent(event: Event): ParsedEvent {
const originData = (
event.raw_data as {
calendar: {
geo_city: string
geo_country: string
geo_region: string
}
interface LumaRawEvent {
calendar: {
geo_city: string
geo_country: string
geo_region: string
}
event: {
geo_address_info: {
address: string
city: string
city_state: string
country: string
full_address: string
latitude: string
longitude: string
mode: string
place_id: string
region: string
type: string
}
).calendar
}
}
function fixEvent(event: Event): ParsedEvent {
const rawEvent = event.raw_data ? (event.raw_data as LumaRawEvent) : null
return {
...event,
event_date: +event.event_date * 1000,
event_city: originData.geo_city,
event_country: originData.geo_country,
event_full_location: compact([originData.geo_region, originData.geo_city, originData.geo_country]).join(', '),
event_city: rawEvent?.calendar.geo_city,
event_country: rawEvent?.calendar.geo_country,
event_full_location:
rawEvent?.event.geo_address_info.full_address ||
compact([rawEvent?.calendar.geo_region, rawEvent?.calendar.geo_city, rawEvent?.calendar.geo_country]).join(
', ',
),
}
}

Expand All @@ -37,20 +55,22 @@ export class Calendar {
const list = await fetchCachedJSON<EventResponse>(
urlcat(BASE_URL, {
provider_type: 'coincarp',
start_date: startDate,
end_date: endDate ? endDate : 0,
start_date: Math.floor(startDate / 1000),
end_date: endDate ? Math.floor(endDate / 1000) : 0,
cursor: 0,
}),
)
if (!list.data) return
return list.data.events.map(fixEventDate)
}
static async getEventList(indicator?: PageIndicator) {
static async getEventList(start_date: number, end_date: number, indicator?: PageIndicator) {
const res = await fetchCachedJSON<EventResponse>(
urlcat(BASE_URL, {
provider_type: 'luma',
size: 20,
cursor: indicator?.id,
start_date: start_date / 1000,
end_date: end_date / 1000,
}),
)
if (!res?.data?.events.length) {
Expand Down

0 comments on commit e22a7ef

Please sign in to comment.