Untitled

mail@pastecode.io avatar
unknown
plain_text
6 months ago
2.6 kB
3
Indexable
Never
import React, { useEffect } from 'react';
import { Text, TouchableOpacity, View } from 'react-native-ui-lib';
import { useTranslation } from 'react-i18next';
import { Icon } from '../ui';
import { PermissionsAndroid } from 'react-native';
import * as AddCalendarEvent from 'react-native-add-calendar-event';

export function AddPhoneCalendar(
  title: 'Meeting',
  startDate = new Date().toISOString(),
  endDate = new Date(Date.now() + 3600000).toISOString(),
  location = 'Office',
  url = 'https://example.com',
) {
  const { t } = useTranslation();

  const addEventToCalendar = async () => {
    try {
      const permission = PermissionsAndroid.PERMISSIONS.WRITE_CALENDAR;
      const granted = await PermissionsAndroid.request(permission, {
        title: 'Calendar Permission',
        message: 'This app needs calendar access to add events',
        buttonNegative: 'Cancel',
        buttonPositive: 'OK',
      });
      if (granted === PermissionsAndroid.RESULTS.GRANTED) {
        console.log('Permission granted');
        const calendarEvent = {
          title: title,
          startDate: startDate,
          endDate: endDate,
          location: location,
          notes: title,
          url: url,
        };

        AddCalendarEvent.presentEventCreatingDialog(calendarEvent)
          .then((eventInfo: { calendarItemIdentifier: string; eventIdentifier: string }) => {
            console.warn(JSON.stringify(eventInfo));
          })
          .catch((error: string) => {
            console.log('Error creating event:', error);
          });
      } else {
        console.log('Permission denied');
      }
    } catch (error) {
      console.warn('Error requesting calendar permission:', error);
    }
  };
  const handleFirstTouchableOpacityPress = () => {
    addEventToCalendar();
  };

  return (
    <View marginL-20 marginT-10>
      <Text heading3 primaryText>
        {t('addPhoneCalendar')}
      </Text>
      <View marginT-20>
        <TouchableOpacity onPress={handleFirstTouchableOpacityPress}>
          <View row>
            <Icon name="email" width={20} height={25} />
            <Text centerV marginL-4 primaryText>
              {t('mailAdress')}
            </Text>
          </View>
        </TouchableOpacity>
        <View marginT-20 />
        <TouchableOpacity onPress={handleFirstTouchableOpacityPress}>
          <View row>
            <View>
              <Icon name="email" width={20} height={25} />
            </View>
            <Text marginL-4 primaryText>
              {t('work')}
            </Text>
          </View>
        </TouchableOpacity>
      </View>
    </View>
  );
}
Leave a Comment