-
Notifications
You must be signed in to change notification settings - Fork 2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Book Appointment for 100 Year Plan VIP
- Loading branch information
1 parent
a7b46f5
commit 9359f79
Showing
5 changed files
with
187 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
88 changes: 88 additions & 0 deletions
88
...g/stepper/declarative-flow/internals/steps-repository/components/calendy-widget/index.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
import React, { useEffect, useMemo } from 'react'; | ||
|
||
declare global { | ||
interface Window { | ||
Calendly: any; | ||
} | ||
} | ||
|
||
interface PrefillData { | ||
name?: string; | ||
email?: string; | ||
// Add other prefill fields as needed | ||
} | ||
|
||
interface CalendlyWidgetProps { | ||
url: string; | ||
id?: string; | ||
prefill?: PrefillData; | ||
onSchedule?: () => void; | ||
} | ||
|
||
const CALENDLY_SCRIPT_URL = 'https://assets.calendly.com/assets/external/widget.js'; | ||
|
||
function isCalendlyEvent( e: MessageEvent ): boolean { | ||
return ( | ||
e.origin === 'https://calendly.com' && e.data.event && e.data.event.indexOf( 'calendly.' ) === 0 | ||
); | ||
} | ||
|
||
const CalendlyWidget: React.FC< CalendlyWidgetProps > = ( { url, id, prefill, onSchedule } ) => { | ||
const widgetId = useMemo( | ||
() => id || `calendly-widget-${ Math.random().toString( 36 ).substr( 2, 9 ) }`, | ||
[ id ] | ||
); | ||
|
||
useEffect( () => { | ||
const handleMessage = ( e: MessageEvent ) => { | ||
if ( isCalendlyEvent( e ) && e.data.event === 'calendly.event_scheduled' ) { | ||
onSchedule?.(); | ||
} | ||
}; | ||
|
||
window.addEventListener( 'message', handleMessage ); | ||
return () => window.removeEventListener( 'message', handleMessage ); | ||
}, [ onSchedule ] ); | ||
|
||
useEffect( () => { | ||
// Check if the script is already loaded | ||
if ( ! document.querySelector( `script[src="${ CALENDLY_SCRIPT_URL }"]` ) ) { | ||
const script = document.createElement( 'script' ); | ||
script.src = CALENDLY_SCRIPT_URL; | ||
script.async = true; | ||
document.body.appendChild( script ); | ||
} | ||
|
||
const loadCalendly = async () => { | ||
while ( typeof window.Calendly === 'undefined' || ! url ) { | ||
await new Promise( ( resolve ) => setTimeout( resolve, 100 ) ); | ||
} | ||
|
||
const element = document.getElementById( widgetId ); | ||
if ( element ) { | ||
// Clear out the container div when props change. | ||
element.innerHTML = ''; | ||
window.Calendly.initInlineWidget( { | ||
url: `https://calendly.com/${ url }`, | ||
parentElement: document.getElementById( widgetId ), | ||
prefill: prefill || {}, | ||
utm: {}, | ||
} ); | ||
} | ||
}; | ||
|
||
loadCalendly(); | ||
|
||
// Cleanup function | ||
return () => { | ||
const existingScript = document.querySelector( `script[src="${ CALENDLY_SCRIPT_URL }"]` ); | ||
if ( existingScript ) { | ||
existingScript.remove(); | ||
} | ||
}; | ||
}, [ url, widgetId, prefill ] ); | ||
|
||
return <div id={ widgetId } className="calendly-widget" />; | ||
}; | ||
|
||
export default CalendlyWidget; |
4 changes: 4 additions & 0 deletions
4
.../stepper/declarative-flow/internals/steps-repository/components/calendy-widget/style.scss
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
.calendly-widget { | ||
min-width: 320px; | ||
height: 630px; | ||
} |
69 changes: 69 additions & 0 deletions
69
...larative-flow/internals/steps-repository/hundred-year-plan-schedule-appointment/index.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
import { UserSelect } from '@automattic/data-stores'; | ||
import { useSelect } from '@wordpress/data'; | ||
import { useTranslate } from 'i18n-calypso'; | ||
import FormattedHeader from 'calypso/components/formatted-header'; | ||
import { USER_STORE } from '../../../../stores'; | ||
import CalendlyWidget from '../components/calendy-widget'; | ||
import HundredYearPlanStepWrapper from '../hundred-year-plan-step-wrapper'; | ||
import type { Step } from '../../types'; | ||
|
||
import './styles.scss'; | ||
|
||
declare global { | ||
interface Window { | ||
Calendly: any; | ||
} | ||
} | ||
|
||
const CALENDLY_ID = 'tim-broddin-a8c/30min'; | ||
|
||
const HundredYearPlanScheduleAppointment: Step = function HundredYearPlanScheduleAppointment( { | ||
navigation, | ||
flow, | ||
} ) { | ||
// eslint-disable-next-line @typescript-eslint/no-unused-vars | ||
const { submit } = navigation; | ||
const translate = useTranslate(); | ||
|
||
const currentUser = useSelect( | ||
( select ) => ( select( USER_STORE ) as UserSelect ).getCurrentUser(), | ||
[] | ||
); | ||
|
||
return ( | ||
<HundredYearPlanStepWrapper | ||
stepContent={ | ||
<div className="hundred-year-plan-schedule-appointment-content"> | ||
<div className="hundred-year-plan-schedule-appointment-content__left-pane"> | ||
// TODO: content | ||
</div> | ||
<div className="hundred-year-plan-schedule-appointment-content__right-pane"> | ||
<CalendlyWidget | ||
url={ CALENDLY_ID } | ||
prefill={ | ||
currentUser | ||
? { name: currentUser?.display_name, email: currentUser?.email } | ||
: undefined | ||
} | ||
onSchedule={ () => { | ||
submit?.(); | ||
} } | ||
/> | ||
</div> | ||
</div> | ||
} | ||
formattedHeader={ | ||
<FormattedHeader | ||
brandFont | ||
headerText={ translate( 'TODO: title' ) } | ||
subHeaderText={ translate( 'TODO: header' ) } | ||
subHeaderAlign="center" | ||
/> | ||
} | ||
stepName="hundred-year-plan-setup" | ||
flowName={ flow } | ||
/> | ||
); | ||
}; | ||
|
||
export default HundredYearPlanScheduleAppointment; |
18 changes: 18 additions & 0 deletions
18
...rative-flow/internals/steps-repository/hundred-year-plan-schedule-appointment/styles.scss
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
.hundred-year-plan-schedule-appointment-content { | ||
display: flex; | ||
flex-direction: row; | ||
justify-content: space-between; | ||
|
||
&__left-pane { | ||
width: 50%; | ||
} | ||
|
||
&__right-pane { | ||
width: 50%; | ||
} | ||
|
||
.calendly-widget { | ||
min-width: 320px; | ||
height: 700px; | ||
} | ||
} |