adding appointment to calendar using createEventAsync

Please provide the following:

  1. SDK Version: 38
  2. Platforms(Android/iOS):

I am having difficulties getting this code to return a calendar ID, I’d appreciate help from anyone as there seem to be very little information about the new calendar api change.

async obtainCalendarPermission() {
    let permission = await Permissions.getAsync(Permissions.CALENDAR)
     if (permission.status !== 'granted') {
       permission = await Permissions.askAsync(Permissions.CALENDAR)
       return
     }
    if (permission.status !== 'granted') {
      permission = await Permissions.askAsync(Permissions.REMINDERS)
      return
     
      if (permission.status !== 'granted') {
        Alert.alert('Permission not granted to calendar')
      }
    }
    return permission
  }

async function getDefaultCalendarSource() {
  const calendars = await Calendar.getCalendarsAsync()
  const defaultCalendars = calendars.filter(
    (each) => each.source.name === 'Default',
  )
  return defaultCalendars[0].source
}

async addReservationToCalendar(date){
    await this.obtainCalendarPermission()
    const permission = await Permissions.askAsync(Permissions.REMINDERS)
    if (permission.status !== 'granted') 
    var calendars = Calendar.getCalendarsAsync().then(calendars => console.log(calendars))
      
    const defaultCalendarSource = Platform.OS === 'ios' ? await getDefaultCalendarSource(): { isLocalAccount: true, name: 'Expo Calendar' };
  console.log(defaultCalendarSource ,+'emeka')

    let dateMs = Date.parse(date)
    let startDate = new Date(dateMs)
    let endDate = new Date(dateMs + 2 * 60 * 60 * 1000)

    const calendarId = await Calendar.createEventAsync(
      defaultCalendarSource.id,
      {
        title: 'Con Fusion Table Reservation',
        startDate: startDate,
        endDate: endDate,
        timeZone: 'Asia/Hong_Kong',
        location:
          '121, Clear Water Bay Road, Clear Water Bay, Kowloon, Hong Kong',
      },
    )}

Hey @emekaokoli,

Are you trying on Android or iOS? If you could throw your code into a Snack so we could interact with it that would be quite helpful.

Cheers,
Adam

Hello @adamjnav,

I have solved it. I was having a hard time getting the calendar ID as it is a requirement for the new createEventAsync() api. this is the complete code that solved the problem for me, I am pasting it here so that someone can use or improve on it.

It generates unique calendar ID and saves reservations to the calendar on IOS, I did not test it on android.

Thanks.

async function getDefaultCalendarSource() {
  await Calendar.getCalendarsAsync().then((id) => console.log(id))
}

 async obtainCalendarPermission() {
    let permission = await Permissions.getAsync(Permissions.CALENDAR)
    if (permission.status !== 'granted') {
      permission = await Permissions.askAsync(Permissions.CALENDAR)
      return
    }
    if (permission.status !== 'granted') {
      permission = await Permissions.askAsync(Permissions.REMINDERS)
      return

      if (permission.status !== 'granted') {
        Alert.alert('Permission not granted to calendar')
      }
    }
    return permission
  }
async addReservationToCalendar(date) {
    await this.obtainCalendarPermission()
    var dateMs = Date.parse(date)
    var startDate = new Date(dateMs)
    var endDate = new Date(dateMs + 2 * 60 * 60 * 1000)

    getDefaultCalendarSource()
    const newCalendar = await Calendar.createCalendarAsync({
      title: 'Test Reservation',
      color: '#512DA8',
      entityType: Calendar.EntityTypes.EVENT,
      sourceId: getDefaultCalendarSource.id,
      source: getDefaultCalendarSource,
      name: 'Restauran Reservation',
      ownerAccount: 'personal',
      accessLevel: Calendar.CalendarAccessLevel.OWNER,
    })

      .then((id) => {
        Calendar.createEventAsync(id, {
          title: 'Table Reservation',
          startDate: startDate,
          endDate: endDate,
          timeZone: 'Asia/Hong_Kong',
          location:
            '121, Clear Water Bay Road, Clear Water Bay, Kowloon, Hong Kong',
        }).catch((err) => console.log(err))
        // console.log(`calendar ID is: ${id}`)
      })
      .catch((err) => console.log(err))
  }

That’s awesome. Glad you got things figured out. We really appreciate you sharing your code and knowledge with the community. It’s one of the things were most proud of in the Expo community. :clap:

My pleasure, keep doing the good job.

Many thanks for that complete information!