You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
So, you HAVE TO USE the provided "permission" functions in order to get this working. I was using react-native-permissions to get the WRITE permission granted but I was just getting "write" errors. Enjoy the snipped
import{useCallback,useEffect,useState}from'react';import{Platform}from'react-native';importRNCalendarEvents,{CalendarEventReadable,CalendarEventWritable,Options,}from'react-native-calendar-events';import{formatRFC3339,addMonths}from'date-fns';constNOW=newDate();constIN_ONE_MONTH=addMonths(NOW,1);typeUseCalendarEventsProps={phoneCalendars: string[];loadEvents: (startDate: Date,endDate: Date,)=>Promise<CalendarEventReadable[]>;saveEventToCalendar: (title: string,details: CalendarEventWritable,options?: Options,)=>Promise<void>;transformDate: (date: Date)=>string|undefined;loadError?: Error;saveError?: Error;};// results is undefined on the iOS sim.// use a device to test calendar accessasyncfunctionrequestCalendarAccess(): Promise<boolean>{try{awaitRNCalendarEvents.checkPermissions(false);constres=awaitRNCalendarEvents.requestPermissions(false);returnres==='authorized';}catch(error){returnfalse;}}/* * This function will return ALL of the calendars saved in your phone * The idea is to return only YOUR calendar, exclusing birthdays, holidays * and subscribed calendars. * */exportconstloadCalendars=async()=>{try{awaitrequestCalendarAccess();constcalendars=awaitRNCalendarEvents.findCalendars();returncalendars?.filter(c=>c?.isPrimary)?.map(c=>c?.id);}catch(e){return[];}};exportfunctionuseCalendarEvents(): UseCalendarEventsProps{const[saveError,setSaveError]=useState<Error>();const[loadError,setLoadError]=useState<Error>();const[permissionGranted,setGranted]=useState<boolean>(false);const[phoneCalendars,setCalendars]=useState<string[]>([]);useEffect(()=>{requestCalendarAccess().then(granted=>setGranted(granted));},[]);useEffect(()=>{loadCalendars().then((results: string[])=>{setCalendars(results);}).catch(e=>setLoadError(e));},[]);consttransformDate=useCallback((date: Date)=>{returnPlatform.select({ios: formatRFC3339(date,{fractionDigits: 3}),android: date.toISOString(),});},[]);constloadEvents=useCallback(async(startDate: Date=NOW,endDate: Date=IN_ONE_MONTH)=>{constpg=awaitrequestCalendarAccess();if(!pg){return[];}constsd=transformDate(startDate);consted=transformDate(endDate);if(sd&&ed){returnRNCalendarEvents.fetchAllEvents(sd,ed,phoneCalendars);}else{return[];}},[transformDate,phoneCalendars],);constsaveEventToCalendar=useCallback(async(title: string,details: CalendarEventWritable,options?: Options,)=>{try{if(permissionGranted){// you need the calendar ID so the event is actually saved+displayed.constcalendarId=phoneCalendars[0];awaitRNCalendarEvents.saveEvent(title,{...details, calendarId},{
...options,sync: true,},);}}catch(error){setSaveError(errorasError);}},[permissionGranted,phoneCalendars],);return{
phoneCalendars,
loadEvents,
saveEventToCalendar,
transformDate,
loadError,
saveError,};}
The text was updated successfully, but these errors were encountered:
So, you HAVE TO USE the provided "permission" functions in order to get this working. I was using
react-native-permissions
to get the WRITE permission granted but I was just getting "write" errors. Enjoy the snippedThe text was updated successfully, but these errors were encountered: