import React, { useEffect, useRef, useState } from 'react'; import { createRoot } from 'react-dom/client'; import { ToastContainer, toast } from 'react-toastify'; import 'react-toastify/dist/ReactToastify.css'; import { __ } from '@wordpress/i18n'; import { LocationWithRole } from '../../../types/location.entity'; import { App } from '../../../app'; import { LocationHeader } from '../location-header/location-header'; import { AuthFooter } from '../../auth/auth-footer/auth-footer'; import { Card } from '../../../shared/card/card'; import { CopyButton } from '../../../shared/copy-button/copy-button'; import { apiService } from '../../../services/api.service'; interface EmbedsProps { locationWithRole: LocationWithRole; bookingAppUrl: string; } interface ServiceCategory { id: number | string; name: string; } interface ServiceCategoriesPayload { serviceCategories?: ServiceCategory[]; } function Embeds({ locationWithRole, bookingAppUrl }: EmbedsProps): JSX.Element { const bookingShortcode = `[ttsbs_btn_service_menu text="${__('Book now', 'timetailor-salon-booking')}"]`; const bookingShortcodeWithClass = `[ttsbs_btn_service_menu text="${__('Book now', 'timetailor-salon-booking')}" class="my-custom-button"]`; const serviceMenuShortcode = `[ttsbs_service_menu]`; const bookingButtonRef = useRef(null); const bookingWithClassButtonRef = useRef(null); const serviceMenuButtonRef = useRef(null); const categoryButtonRef = useRef(null); const [categories, setCategories] = useState([]); const [selectedCategoryId, setSelectedCategoryId] = useState(''); const categoryShortcode = selectedCategoryId ? `[ttsbs_category_services category_id="${selectedCategoryId}"]` : __('Shortcode will appear after selecting a category', 'timetailor-salon-booking'); const handleBookingCodeClick = (): void => { bookingButtonRef.current?.click(); }; const handleBookingWithClassCodeClick = (): void => { bookingWithClassButtonRef.current?.click(); }; const handleServiceMenuCodeClick = (): void => { serviceMenuButtonRef.current?.click(); }; const handleCategoryCodeClick = (): void => { if (!selectedCategoryId) { return; } categoryButtonRef.current?.click(); }; const extractServiceCategories = (payload: unknown): ServiceCategory[] => { if (!payload || typeof payload !== 'object' || !('categories' in payload)) { return []; } const categoriesPayload = (payload as { categories?: ServiceCategoriesPayload }).categories; if (!categoriesPayload || !Array.isArray(categoriesPayload.serviceCategories)) { return []; } return categoriesPayload.serviceCategories; }; const fetchCategories = async (forceRefresh: boolean): Promise => { try { const response = await apiService.getServiceCategories(forceRefresh); setCategories(extractServiceCategories(response)); } catch (error) { setCategories([]); } }; useEffect(() => { fetchCategories(false); }, []); const handleRefreshCategoriesClick = async () => { await fetchCategories(true); toast.success(__('Service menu updated.', 'timetailor-salon-booking')); }; return (

{__('Displays a button linking to your booking page.', 'timetailor-salon-booking')}

{__('Basic usage', 'timetailor-salon-booking')}
{bookingShortcode}
{__('Customizations', 'timetailor-salon-booking')}

{__('Add CSS classes from your WordPress theme.', 'timetailor-salon-booking')}

{bookingShortcodeWithClass}

{__('Displays the full service menu embed inside the page content.', 'timetailor-salon-booking')}

{serviceMenuShortcode}

{__('Display services from a specific category. Select a category below to generate the shortcode.', 'timetailor-salon-booking')}

{categoryShortcode} {selectedCategoryId && }

{__('Not seeing recent menu changes?', 'timetailor-salon-booking')}{' '}

); } function initEmbeds(): void { const container = document.getElementById('ttsbs-embeds-root'); if (!container) { return; } // init the app App.bootstrap({ ajaxUrl: container.dataset.ajaxUrl || '', nonce: container.dataset.nonce || '', bookingAppUrl: container.dataset.bookingAppUrl || '', }); const locationWithRole = JSON.parse(container.dataset.locationWithRole || '{}') as LocationWithRole; const bookingAppUrl = container.dataset.bookingAppUrl || ''; const root = createRoot(container); root.render( <>
); } if (document.readyState === 'loading') { document.addEventListener('DOMContentLoaded', initEmbeds); } else { initEmbeds(); }