react-google-calendar-api
An api to manage your google calendar
Install
Npm
npm install --save react-google-calendar-api
yarn
yarn add react-google-calendar-api
Use (Javascript / Typescript)
You will need to enable the "Google Calendar API"(https://console.developers.google.com/flows/enableapi?apiid=calendar.)
You will need a clientId and ApiKey from Google(https://developers.google.com/workspace/guides/create-credentials)
import ApiCalendar from 'react-google-calendar-api';
const config = {
"clientId": "<CLIENT_ID>",
"apiKey": "<API_KEY>",
"scope": "https://www.googleapis.com/auth/calendar",
"discoveryDocs": [
"https://www.googleapis.com/discovery/v1/apis/calendar/v3/rest"
]
}
const apiCalendar = new ApiCalendar(config)
Setup
handleAuthClick:
public handleAuthClick(): void
handleSignOutClick:
public handleSignoutClick(): void
Example
import React, {ReactNode, SyntheticEvent} from 'react';
import ApiCalendar from 'react-google-calendar-api';
const config = {
"clientId": "<CLIENT_ID>",
"apiKey": "<API_KEY>",
"scope": "https://www.googleapis.com/auth/calendar",
"discoveryDocs": [
"https://www.googleapis.com/discovery/v1/apis/calendar/v3/rest"
]
}
const apiCalendar = new ApiCalendar(config)
export default class DoubleButton extends React.Component {
constructor(props) {
super(props);
this.handleItemClick = this.handleItemClick.bind(this);
}
public handleItemClick(event: SyntheticEvent<any>, name: string): void {
if (name === 'sign-in') {
apiCalendar.handleAuthClick()
} else if (name === 'sign-out') {
apiCalendar.handleSignoutClick();
}
}
render(): ReactNode {
return (
<button
onClick={(e) => this.handleItemClick(e, 'sign-in')}
>
sign-in
</button>
<button
onClick={(e) => this.handleItemClick(e, 'sign-out')}
>
sign-out
</button>
);
}
}
setCalendar:
public setCalendar(newCalendar: string): void
Manage Event
You need to be registered with handleAuthClick.
Create Event:
public createEvent(event: object, calendarId: string = this.calendar, sendUpdates: string = 'none',): any {
Create Event From Now:
public createEventFromNow({time, summary, description = ''}: any, calendarId: string = this.calendar, timeZone: string = "Europe/Paris"): any
Example
import ApiCalendar from 'react-google-calendar-api';
const config = {
"clientId": "<CLIENT_ID>",
"apiKey": "<API_KEY>",
"scope": "https://www.googleapis.com/auth/calendar",
"discoveryDocs": [
"https://www.googleapis.com/discovery/v1/apis/calendar/v3/rest"
]
}
const apiCalendar = new ApiCalendar(config)
const eventFromNow: object = {
summary: 'Poc Dev From Now',
time: 480,
};
apiCalendar.createEventFromNow(eventFromNow)
.then((result: object) => {
console.log(result);
})
.catch((error: any) => {
console.log(error);
});
List All Upcoming Events:
public listUpcomingEvents(maxResults: number, calendarId: string = this.calendar): any
Example
apiCalendar.listUpcomingEvents(10).then(({ result }: any) => {
console.log(result.items);
List All Events:
public listEvents(queryOptions, calendarId = this.calendar): any
Example
apiCalendar.listEvents({
timeMin: new Date()..toISOString(),
timeMax: new Date().addDays(10).toISOString(),
showDeleted: true,
maxResults: 10,
orderBy: 'updated'
}).then(({ result }: any) => {
console.log(result.items);
});
Update Event
public updateEvent(event: object, eventId: string, calendarId: string = this.calendar, sendUpdates: string = 'none'): any
Example
const event = {
summary: 'New Event Title',
};
apiCalendar.updateEvent(event, '2eo85lmjkkd2i63uo3lhi8a2cq').then(console.log);
Delete Event
public deleteEvent(eventId: string, calendarId: string = this.calendar): any
Example
apiCalendar.deleteEvent('2eo85lmjkkd2i63uo3lhi8a2cq').then(console.log);
Get Event
public getEvent(eventId: string, calendarId: string = this.calendar): any
Example
apiCalendar.getEvent('2eo85lmjkkd2i63uo3lhi8a2cq').then(console.log);