wmcmahan / react-native-calendar-events

📆 React Native Module for iOS and Android Calendar Events
MIT License
900 stars 290 forks source link

Unable to saveEvent() after upgrading version to 1.7.3 from 1.6.1 #278

Closed ldco2016 closed 3 years ago

ldco2016 commented 4 years ago

I used to have a working react-native-calendar-events library at version 1.6.1, but since I upgraded to React Native 60.4 and react-native-calendar-events to version 1.7.3 users cannot add events to calendar.

On Android, they get an error message, on iOS it just crashes the app.

The error message on Android is coming from this asynchronous component helper function:

async function addToCalendar(event) {
  try {
    const startDate =
      Platform.OS === "ios"
        ? format(parse(event.StartDateLocal))
        : parse(event.StartDateLocal);
    const endDate =
      Platform.OS === "ios"
        ? format(parse(event.EndDateLocal))
        : parse(event.EndDateLocal);
    const allEvents = await RNCalendarEvents.fetchAllEvents(startDate, endDate);

    const calendarEvent = allEvents.find(e => e.title === event.Title);
    if (calendarEvent) {
      alert("You have already added this event to your calendar.");
    } else {
      const title = event.Title;

      const {
        Location: {
          AddressLine1: address,
          City: city,
          StateAbbreviation: state,
          PostalCode: zip
        }
      } = event;

      const location = `${address}, ${city}, ${state}, ${zip}`;

      const settings = {
        location,
        startDate,
        endDate
      };
      RNCalendarEvents.saveEvent(title, settings)
        .then(() => {
          alert("Event Saved");
        })
        .catch(() => {
          alert("Error");
        });
    }
  } catch (e) {
    alert("Error");
  }
}

react-native-calendar-events

Environment

System:
    OS: macOS High Sierra 10.13.6
    CPU: (8) x64 Intel(R) Core(TM) i7-4770HQ CPU @ 2.20GHz
    Memory: 417.23 MB / 16.00 GB
    Shell: 5.3 - /bin/zsh
  Binaries:
    Node: 10.16.3 - /usr/local/bin/node
    Yarn: 1.10.1 - /usr/local/bin/yarn
    npm: 6.9.0 - /usr/local/bin/npm
    Watchman: 4.7.0 - /usr/local/bin/watchman
  SDKs:
    iOS SDK:
      Platforms: iOS 12.1, macOS 10.14, tvOS 12.1, watchOS 5.1
    Android SDK:
      API Levels: 23, 25, 26, 27, 28
      Build Tools: 23.0.1, 26.0.2, 27.0.3, 28.0.3
      System Images: android-28 | Google Play Intel x86 Atom
  IDEs:
    Android Studio: 3.5 AI-191.8026.42.35.5900203
    Xcode: 10.1/10B61 - /usr/bin/xcodebuild
  npmPackages:
    @react-native-community/cli: 2.9.0 => 2.9.0
    react: 16.8.6 => 16.8.6
    react-native: 0.60.4 => 0.60.4
  npmGlobalPackages:
    react-native-cli: 2.0.1
    react-native-git-upgrade: 0.2.7

I went back to react-native-calendar-events 1.6.1 and I am still getting the same issue.

This library looks like it still uses index.ios.js and index.android.js even though React Native 60+ versions are now on index.js as root. Could this be the problem?

Steps to Reproduce

Please clone: https://github.com/ldco2016/RNapp

What would be a cause of receiving undetermined as a Promise?

ldco2016 commented 4 years ago

@wmcmahan , could you at least tell me under what circumstance const status = await RNCalendarEvents.authorizationStatus(); would return undetermined for a React Native version 0.60.4 version but not for a 0.53.3 version, despite the user being the same?

ldco2016 commented 4 years ago

So I was able to get react-native-calendar-events working on the iOS side via a refactor, but it still does not save an event on the Android side.

djohnsonkc commented 4 years ago

I was able to get it to work for Android with RN v0.61.4 by removing all of the manual hacks that the instructions say to do. With auto-linking, seems to work without all of those coding hacks to the gradle files and the *.java files. It also seems to work fine with iOS.

mbrucher commented 4 years ago

I have the code working in debug, but not in release, even if I removed all the hacks :/ Any update on a version compatible with react native 0.61?

voslartomas commented 4 years ago

Same here, everything works as expected in debug, but when you release version, Promise returns error Unable to save event.

voslartomas commented 4 years ago

Ok, I found out that this issue is about start and end dates. You just need to pass them in timestamp format and then it works even in release version.

mbrucher commented 4 years ago

timestamp format, got it, thanks! will try it.

ldco2016 commented 4 years ago

@voslartomas , could you share an example of how you are passing them in timestamp format for it to save in Android side?

voslartomas commented 4 years ago

Sure it looks like this.

startDate: new Date(eventObject.date).getTime(),
jodelat commented 4 years ago

@voslartomas Does using new Date(eventObject.date).toISOString() work for you? Using .getTime() and .toISOString() give me the error if "Unhandled Promise Rejection (id:1), RangeError: Invalid Date toISOString@[native code]

jodelat commented 4 years ago

@voslartomas can you also send a full example of your working code?

jodelat commented 4 years ago

@voslartomas using startDate: new Date(eventObject.date).getTime() give me the error "Malformed calls fromJS: field sizes are different."

jodelat commented 4 years ago

I am on version 1.7.3

djohnsonkc commented 4 years ago

I use moment(mydate).toISOString() and this works well for me for iOS and Android, in React Native v0.61.4

If it helps, I put together some code samples of a helper class I built for my recent project: https://github.com/djohnsonkc/code-samples/tree/master/React-Native/CalendarSync

voslartomas commented 4 years ago

This works for me @jodelat .

RNCalendarEvents.saveEvent(eventObject.name, {
        ...(eventObject.calendarEvent && eventObject.calendarEvent.calendarEventId ? { id: eventObject.calendarEvent.calendarEventId } : {}),
        calendarId: res.data.getUser.profile.calendar,
        startDate: new Date(eventObject.date).getTime(),
        location: eventObject.placeName,
        endDate: new Date(eventObject.date).getTime() + 1.5 * 60 * 60 * 1000
      }).then(eventId => {
        ...
      }).catch(err => console.log(err))

"react-native": "0.61.4" "react-native-calendar-events": "^1.7.3"

MoOx commented 4 years ago

Does anyone here think that we should make a change to the lib?

Akijunior commented 4 years ago

@MoOx I believe that you're right

MoOx commented 3 years ago

I am thinking about only allowing dates as time float.