import { useState, useEffect } from '@wordpress/element'; import { useSelect } from '@wordpress/data'; import { store as coreStore } from '@wordpress/core-data'; import { Spinner as WCSpinner } from '@wordpress/components'; import { __ } from '@wordpress/i18n'; import { mapMarker } from '@wordpress/icons'; import { Icon, Stack, EmptyState } from '@wordpress/ui'; import { EventsList, type WPEvent } from './components'; import type { EventsWidgetAttributes } from './types'; import styles from './style.module.css'; interface WPEventsResponse { events: WPEvent[]; location?: { description: string; }; } const EVENTS_API = 'https://api.wordpress.org/events/1.0/'; function EventsListSection( { events, loading, error, showEmptyState, location, }: { events: WPEvent[]; loading: boolean; error: boolean; showEmptyState: boolean; location?: string; } ) { if ( error ) { return (

{ __( 'An error occurred. Please try again.' ) }

); } return ( ); } type WordPressEventsProps = { attributes?: EventsWidgetAttributes; }; export default function WordPressEvents( { attributes = {}, }: WordPressEventsProps ) { const userLocale = useSelect( ( select ) => ( ( select( coreStore ) as any ).getCurrentUser() ?.locale as string ) ?? 'en_US', [] ); const persistedLocation = typeof attributes.location === 'string' ? attributes.location.trim() : ''; const [ activeLocation, setActiveLocation ] = useState( persistedLocation ); const [ locationLabel, setLocationLabel ] = useState( '' ); useEffect( () => { setActiveLocation( persistedLocation ); }, [ persistedLocation ] ); const [ events, setEvents ] = useState< WPEvent[] >( [] ); const [ eventsLoading, setEventsLoading ] = useState( false ); const [ eventsError, setEventsError ] = useState( false ); const hasSelectedLocation = Boolean( activeLocation.trim() ); useEffect( () => { if ( ! hasSelectedLocation ) { setLocationLabel( '' ); return; } const controller = new AbortController(); setEventsLoading( true ); setEventsError( false ); setLocationLabel( '' ); const params = new URLSearchParams( { number: '5', locale: userLocale, location: activeLocation, } ); fetch( `${ EVENTS_API }?${ params }`, { signal: controller.signal } ) .then( ( r ) => r.json() as Promise< WPEventsResponse > ) .then( ( data ) => { setEvents( data.events ?? [] ); if ( data.location?.description ) { setLocationLabel( data.location.description ); } setEventsLoading( false ); } ) .catch( ( err: Error ) => { if ( err.name !== 'AbortError' ) { setEventsError( true ); setEventsLoading( false ); } } ); return () => controller.abort(); }, [ activeLocation, hasSelectedLocation, userLocale ] ); return ( { ! hasSelectedLocation && ( { __( 'Attend an upcoming event near you.' ) } { __( 'Select a city to view events.' ) } ) } { hasSelectedLocation && eventsLoading && ( ) } { hasSelectedLocation && ! eventsLoading && ( ) } ); }