import { StackScreenProps } from '@react-navigation/stack';
import * as Calendar from 'expo-calendar';
import React from 'react';
import { Alert, Platform, ScrollView, StyleSheet, Text, View } from 'react-native';
import Button from '../components/Button';
import HeadingText from '../components/HeadingText';
import ListButton from '../components/ListButton';
import MonoText from '../components/MonoText';
const EventRow: React.FunctionComponent<{
event: Calendar.Event;
getEvent: (event: Calendar.Event) => void;
getAttendees: (event: Calendar.Event) => void;
updateEvent: (event: Calendar.Event) => void;
deleteEvent: (event: Calendar.Event) => void;
openEventInCalendar: (event: Calendar.Event) => void;
}> = ({ event, getEvent, getAttendees, updateEvent, deleteEvent, openEventInCalendar }) => (
{event.title}
{JSON.stringify(event, null, 2)}
getEvent(event)} title="Get Event Using ID" />
getAttendees(event)} title="Get Attendees for Event" />
updateEvent(event)} title="Update Event" />
deleteEvent(event)} title="Delete Event" />
{Platform.OS === 'android' && (
openEventInCalendar(event)} title="Open in Calendar App" />
)}
);
interface State {
events: Calendar.Event[];
}
type Links = {
Events: { calendar: Calendar.Calendar };
};
type Props = StackScreenProps;
export default class EventsScreen extends React.Component {
static navigationOptions = {
title: 'Events',
};
readonly state: State = {
events: [],
};
componentDidMount() {
const { params } = this.props.route;
if (params) {
const { id } = params.calendar;
if (id) {
this._findEvents(id);
}
}
}
_findEvents = async (id: string) => {
const yesterday = new Date();
yesterday.setDate(yesterday.getDate() - 1);
const nextYear = new Date();
nextYear.setFullYear(nextYear.getFullYear() + 1);
const events = await Calendar.getEventsAsync([id], yesterday, nextYear);
this.setState({ events });
};
_addEvent = async (recurring: boolean) => {
const { calendar } = this.props.route.params!;
if (!calendar.allowsModifications) {
Alert.alert('This calendar does not allow modifications');
return;
}
const timeInOneHour = new Date();
timeInOneHour.setHours(timeInOneHour.getHours() + 1);
const newEvent: {
title: string;
location: string;
startDate: Date;
endDate: Date;
notes: string;
timeZone: string;
recurrenceRule?: {
occurrence: number;
frequency: string;
};
} = {
title: 'Celebrate Expo',
location: '420 Florence St',
startDate: new Date(),
endDate: timeInOneHour,
notes: "It's cool",
timeZone: 'America/Los_Angeles',
};
if (recurring) {
newEvent.recurrenceRule = {
occurrence: 5,
frequency: 'daily',
};
}
try {
await Calendar.createEventAsync(calendar.id, newEvent);
Alert.alert('Event saved successfully');
this._findEvents(calendar.id);
} catch (e) {
Alert.alert('Event not saved successfully', e.message);
}
};
_getEvent = async (event: Calendar.Event) => {
try {
const newEvent = await Calendar.getEventAsync(event.id!, {
futureEvents: false,
instanceStartDate: event.startDate,
});
Alert.alert('Event found using getEventAsync', JSON.stringify(newEvent));
} catch (e) {
Alert.alert('Error finding event', e.message);
}
};
_getAttendees = async (event: Calendar.Event) => {
try {
const attendees = await Calendar.getAttendeesForEventAsync(event.id!, {
futureEvents: false,
instanceStartDate: event.startDate,
});
Alert.alert('Attendees found using getAttendeesForEventAsync', JSON.stringify(attendees));
} catch (e) {
Alert.alert('Error finding attendees', e.message);
}
};
_updateEvent = async (event: Calendar.Event) => {
const { calendar } = this.props.route.params!;
if (!calendar.allowsModifications) {
Alert.alert('This calendar does not allow modifications');
return;
}
const newEvent = {
title: 'update test',
};
try {
await Calendar.updateEventAsync(event.id!, newEvent, {
futureEvents: false,
instanceStartDate: event.startDate,
});
Alert.alert('Event saved successfully');
this._findEvents(calendar.id);
} catch (e) {
Alert.alert('Event not saved successfully', e.message);
}
};
_deleteEvent = async (event: Calendar.Event) => {
try {
const { calendar } = this.props.route.params!;
await Calendar.deleteEventAsync(event.id!, {
futureEvents: false,
instanceStartDate: event.recurrenceRule ? event.startDate : undefined,
});
Alert.alert('Event deleted successfully');
this._findEvents(calendar.id);
} catch (e) {
Alert.alert('Event not deleted successfully', e.message);
}
};
_openEventInCalendar = (event: Calendar.Event) => {
Calendar.openEventInCalendar(event.id!);
};
_renderActionButtons = () => {
return (
);
};
render() {
const events = this.state.events.length ? (
{this.state.events.map((event) => (
))}
) : (
This calendar has no events.
);
return (
{this._renderActionButtons()}
{events}
);
}
}
const styles = StyleSheet.create({
container: {
paddingHorizontal: 10,
paddingVertical: 16,
flex: 1,
},
eventRow: {
marginBottom: 12,
},
});