【问题标题】:How can I run code when event is scheduled in embedded Calendly widget?在嵌入式 Calendly 小部件中安排事件时如何运行代码?
【发布时间】:2020-10-06 09:34:44
【问题描述】:

当我使用 Calendly 安排活动时,我正在使用 Calendly API 令牌显示活动创建日期,但日期仅在重新加载页面后显示,我希望在安排活动后在控制台中更新一次.

下面是代码。

import React, { useState, useEffect } from 'react';
import axios from 'axios';
import { InlineWidget } from 'react-calendly';

const Calendly = () => {

    const [state] = useState()

    useEffect(() => {
        axios({
            method: 'get',
            url: 'https://v1.nocodeapi.com/user/calendly/hobiiHVeoqPxvtTc',
        }).then(function (response) {
                // handle success
                console.log(response.data.collection[response.data.collection.length - 1].created_at);
        }).catch(function (error) {
                // handle error
                console.log(error);
        })
    }, [state])
    return (
        <div>
            <InlineWidget url="https://calendly.com/user/15min"
                styles={{
                    height: '1000px'
                }}
                pageSettings={{
                    backgroundColor: 'ffffff',
                    hideEventTypeDetails: false,
                    hideLandingPageDetails: false,
                    primaryColor: '00a2ff',
                    textColor: '4d5055'
                }}
                prefill={{
                    email: 'kanna@gmail.com',
                    firstName: 'Kanna',
                    lastName: 'Suresh',
                    name: 'Kanna Suresh',
                    customAnswers: {
                        a1: 'a1',
                        a2: 'a2',
                        a3: 'a3',
                        a4: 'a4',
                        a5: 'a5',
                        a6: 'a6',
                        a7: 'a7',
                        a8: 'a8',
                        a9: 'a9',
                        a10: 'a10'
                    }
                }}
                utm={{
                    utmCampaign: 'Spring Sale 2019',
                    utmContent: 'Shoe and Shirts',
                    utmMedium: 'Ad',
                    utmSource: 'Facebook',
                    utmTerm: 'Spring'
                }} />
            <div>
                
            </div>     
        
        </div>

    );

}
    
export default Calendly;

【问题讨论】:

  • 添加了一个使用来自 react-calendly 的 CalendlyEventListener 的示例。 Lmk 如果这对你有用!

标签: javascript reactjs calendly


【解决方案1】:

如果您想在安排 Calendly 事件时运行一些代码,您想收听 Calendly iframe 将回传到您的主机页面的消息。这是Calendly's JavaScript API 的一部分。这是大概的代码。

import React, { useState, useEffect } from 'react';
import axios from 'axios';
import { InlineWidget } from 'react-calendly';

const isCalendlyScheduledEvent = (e) => {
  return e.data.event &&
         e.data.event.indexOf('calendly') === 0 &&
         e.data.event === 'calendly.event_scheduled'
}

const Calendly = () => {

    const [state] = useState()

    useEffect(() => {
      window.addEventListener(
        'message',
        (e) => {
          if (isCalendlyScheduledEvent(e)) {
            axios({
              method: 'get',
              url: 'https://v1.nocodeapi.com/user/calendly/hobiiHVeoqPxvtTc',
            }).then(function (response) {
              // handle success
              console.log(response.data.collection[response.data.collection.length - 1].created_at);
            }).catch(function (error) {
              // handle error
              console.log(error);
            })
          }
        }
      )
    }, []) // notice the empty array as second argument - we only need to run it once, equivalent to the old componentDidMount behavior
    
    return (
        <div>
            ...    
        </div>

    );

}
    
export default Calendly;

更新

react-calendly 包实际上包含一个CalendlyEventListener,它设置了消息侦听器,因此您必须编写更少的样板代码。这是相同的代码,但使用了CalendlyEventListener 组件:

import React, { useState, useEffect } from 'react';
import axios from 'axios';
import { InlineWidget, CalendlyEventListener } from 'react-calendly';

const Calendly = () => {
    const onEventScheduled = () => {
      axios({
        method: 'get',
        url: 'https://v1.nocodeapi.com/user/calendly/hobiiHVeoqPxvtTc',
      }).then(function (response) {
        // handle success
        console.log(response.data.collection[response.data.collection.length - 1].created_at);
      }).catch(function (error) {
        // handle error
        console.log(error);
      })
    }
    
    return (
      <div>
        ...

        <CalendlyEventListener onEventScheduled={onEventScheduled} />
      </div>

    );

}
    
export default Calendly;

【讨论】:

  • 我没有看到取消和重新安排的活动。有没有办法知道这些事件发生了?
  • 没有取消和重新安排流程的事件,因为这些不会发生在嵌入中。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-04-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-08-31
  • 2012-04-22
相关资源
最近更新 更多