import {__} from "@wordpress/i18n"; import {InspectorControls, useBlockProps} from '@wordpress/block-editor'; import {PanelBody, CheckboxControl, SelectControl, TextControl} from '@wordpress/components'; import React, {useEffect, useState} from "react"; import FullCalendar from '@fullcalendar/react'; import resourceTimeGridPlugin from '@fullcalendar/resource-timegrid'; import {Stage} from "../interfaces/stage"; import {Event} from "../interfaces/event"; interface EditProps { attributes: { fair: number, selectedStages: string, calendarHeight: number, minTime: string, maxTime: string, startDate: string, endDate: string, }; setAttributes: (attributes: { fair?: number, selectedStages?: string, calendarHeight?: number, minTime?: string, maxTime?: string, startDate?: string, endDate?: string, }) => void; } console.log('editor/stages-vertikal.block.tsx'); /* Block name */ export const name = `multistage-event-planner/stages-vertical-view`; /* Block title */ export const title = __('Stages Vertical view', 'multistage-event-planner'); // Updated title /* Block icon */ export const icon = 'calendar'; /* Block category */ export const category = `widgets`; /* Block edit function */ export const edit: React.FC = ({attributes, setAttributes}) => { const {fair: fairId} = attributes; const [fairs, setFairs] = useState([]); const [stages, setStages] = useState([]); const [selectedStages, setSelectedStages] = useState(''); const [startDate, setStartDate] = useState(Date()); const [endDate, setEndDate] = useState(Date()); const [randId] = useState(Math.random().toString(36).substring(2, 10)); const [events, setEvents] = useState([]); const [filteredEvents, setFilteredEvents] = useState([]); // Fetch fairs useEffect(() => { fetch('/wp-json/wp/v2/msep-fair') .then(response => { if (!response.ok) { throw new Error(`HTTP error! status: ${response.status}`); } return response.json(); // changed to text(), not json() }) .then(data => { //console.log('Raw data loaded', data); setFairs(data.map((fair) => ({label: fair.title.rendered, value: fair.id}))); }) .catch(error => console.error('Error:', error)); }, []); useEffect(() => { setSelectedStages(attributes.selectedStages || ""); }, [attributes.selectedStages]); useEffect(() => { if (fairId) { fetch(`/wp-json/multistage-event-planner/v1/fair/${fairId}`) .then(response => response.json()) .then(data => { const stageData = data.stages .filter(stage => stage.events && stage.events.length > 0) .map(stage => ({ id: stage.id, title: stage.title, events: stage.events.map(event => ({ ...event, title: event.shortTitle, resourceId: `${stage.id}` })) })); setStages(stageData); // Set minTime and maxTime attributes from data setAttributes({ startDate: attributes.startDate === '1976-06-03' ? data.start : attributes.startDate, endDate: attributes.endDate === '1976-06-03' ? data.end : attributes.endDate, minTime: attributes.minTime === '00:00:00' ? data.minTime : attributes.minTime, maxTime: attributes.maxTime === '00:00:00' ? data.maxTime : attributes.maxTime }); setEvents(stageData.flatMap(stage => stage.events)); setFilteredEvents(stageData.flatMap(stage => stage.events)); }) .catch(error => console.error('Error fetching events:', error)); } }, [fairId]); const Presenters = ({presenters}) => { const presentersByRole = { 'moderator': [], 'speaker': [], }; presenters.forEach((presenter) => { const {link, linkColor, title, firstname, lastname, id, role} = presenter; let presenterComponent; switch (link) { case 'page': presenterComponent = {`${title} ${firstname} ${lastname}`} ; break; case 'modal': presenterComponent = {`${title} ${firstname} ${lastname}`} ; break; default: presenterComponent = {`${title} ${firstname} ${lastname}`} ; break; } presentersByRole[role].push(presenterComponent); }); return (
{['moderator', 'speaker'].map(role => presentersByRole[role].length > 0 &&
{`${role.charAt(0).toUpperCase() + role.slice(1)}${presentersByRole[role].length > 1 ? 's' : ''}: `} {presentersByRole[role]}
)}
); }; function renderEventContent(eventInfo) { const {timeText, event} = eventInfo; const {langIcon, linkColor, moreLink, presenters, videoUrl} = event.extendedProps; return (
{timeText}
{langIcon && }
{event.title}
); } return ( <> setAttributes({fair: parseInt(newFair, 10) || 0})} // Parse selected value as integer />

{__('Select stages', 'multistage-event-planner')}

{__('If non are selected, all are displayed', 'multistage-event-planner')}

{/* Map through the stages and create a checkbox for each one */} {stages.map(stage => { const stageIdStr = stage.id.toString(); return ( { const stagesArray = selectedStages.split(','); if (isChecked) { stagesArray.push(stageIdStr); } else { const index = stagesArray.indexOf(stageIdStr); if (index > -1) { stagesArray.splice(index, 1); } } const updatedStagesString = stagesArray.join(','); setSelectedStages(updatedStagesString); setAttributes({selectedStages: updatedStagesString}); }} /> ); })} setAttributes({ startDate: value })} /> setAttributes({ minTime: value })} /> setAttributes({ endDate: value })} /> setAttributes({ maxTime: value })} /> setAttributes({ calendarHeight: parseInt(value) })} />
selectedStages.split(',').includes(stage.id.toString())) : stages ).map(stage => { return {id: `${stage.id}`, title: `${stage.title}`} }) } events={ (selectedStages ? stages.filter(stage => selectedStages.split(',').includes(stage.id.toString())) : stages ).flatMap(stage => stage.events.map(event => ({...event, id: event.id.toString(), resourceId: `${stage.id}`}))) } eventContent={renderEventContent} initialDate={'2024-01-01'} //headerToolbar={false} // Disable the header toolbar headerToolbar={{ left: 'title', center: '', right: 'prev,next' }} allDaySlot={false} // Optionally hide the all-day slot //dayHeaders={false} // Optionally hide day headers // Disable interaction features selectable={false} editable={false} eventStartEditable={false} eventDurationEditable={false} locale={navigator.language} // Use the browser locale //locale='en' // Use the browser locale slotMinTime={attributes.minTime} // Start of the calendar slotMaxTime={attributes.maxTime} // End of the calendar expandRows={true} // add the below line validRange={{ start: attributes.startDate, end: attributes.endDate }} schedulerLicenseKey='CC-Attribution-NonCommercial-NoDerivatives' // Add this line />
); }; /* Block save function */ export const save: React.FC<{ attributes: { fair: number, selectedStages: string, calendarHeight: number, minTime: string, maxTime: string, startDate: string, endDate: string, } }> = ({attributes}) => { const {fair, selectedStages, calendarHeight, minTime, maxTime, startDate, endDate} = attributes; // Continue with your component rendering const blockProps = useBlockProps.save(); return (
{/* Add any additional markup needed here */}
); }; /* Block styles */ export const styles = [ {name: 'default', label: __('Default', 'multistage-event-planner'), isDefault: true}, {name: 'custom', label: __('Custom', 'multistage-event-planner')}, ]; /* Block variations * / export const variations = [ { name: 'example/fullcalendar-day-view-variant', title: __('FullCalendar Day View Variant', 'multistage-event-planner') }, ]; */