Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

react-google-calendar-api

Package Overview
Dependencies
Maintainers
1
Versions
27
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

react-google-calendar-api - npm Package Compare versions

Comparing version 1.2.1 to 1.3.0

502

build/ApiCalendar.js
'use strict';
Object.defineProperty(exports, "__esModule", {
value: true
value: true
});

@@ -11,234 +11,318 @@

var Config = require("../../../apiGoogleconfig.json");
var Config = require('../../../apiGoogleconfig.json');
var ApiCalendar = function () {
function ApiCalendar() {
_classCallCheck(this, ApiCalendar);
function ApiCalendar() {
_classCallCheck(this, ApiCalendar);
this.sign = false;
this.gapi = null;
this.onLoadCallback = null;
this.calendar = 'primary';
try {
this.updateSigninStatus = this.updateSigninStatus.bind(this);
this.initClient = this.initClient.bind(this);
this.handleSignoutClick = this.handleSignoutClick.bind(this);
this.handleAuthClick = this.handleAuthClick.bind(this);
this.createEvent = this.createEvent.bind(this);
this.listUpcomingEvents = this.listUpcomingEvents.bind(this);
this.createEventFromNow = this.createEventFromNow.bind(this);
this.listenSign = this.listenSign.bind(this);
this.onLoad = this.onLoad.bind(this);
this.setCalendar = this.setCalendar.bind(this);
this.updateEvent = this.updateEvent.bind(this);
this.handleClientLoad();
} catch (e) {
console.log(e);
}
this.sign = false;
this.gapi = null;
this.onLoadCallback = null;
this.calendar = 'primary';
try {
this.updateSigninStatus = this.updateSigninStatus.bind(this);
this.initClient = this.initClient.bind(this);
this.handleSignoutClick = this.handleSignoutClick.bind(this);
this.handleAuthClick = this.handleAuthClick.bind(this);
this.createEvent = this.createEvent.bind(this);
this.listUpcomingEvents = this.listUpcomingEvents.bind(this);
this.createEventFromNow = this.createEventFromNow.bind(this);
this.listenSign = this.listenSign.bind(this);
this.onLoad = this.onLoad.bind(this);
this.setCalendar = this.setCalendar.bind(this);
this.updateEvent = this.updateEvent.bind(this);
this.deleteEvent = this.deleteEvent.bind(this);
this.getEvent = this.getEvent.bind(this);
this.getBasicUserProfile = this.getBasicUserProfile.bind(this);
this.handleClientLoad();
} catch (e) {
console.log(e);
}
}
/**
* Update connection status.
* @param {boolean} isSignedIn
*/
_createClass(ApiCalendar, [{
key: 'updateSigninStatus',
value: function updateSigninStatus(isSignedIn) {
this.sign = isSignedIn;
}
/**
* Update connection status.
* @param {boolean} isSignedIn
* Auth to the google Api.
*/
}, {
key: 'initClient',
value: function initClient() {
var _this = this;
_createClass(ApiCalendar, [{
key: 'updateSigninStatus',
value: function updateSigninStatus(isSignedIn) {
this.sign = isSignedIn;
this.gapi = window['gapi'];
this.gapi.client.init(Config).then(function () {
// Listen for sign-in state changes.
_this.gapi.auth2.getAuthInstance().isSignedIn.listen(_this.updateSigninStatus);
// Handle the initial sign-in state.
_this.updateSigninStatus(_this.gapi.auth2.getAuthInstance().isSignedIn.get());
if (_this.onLoadCallback) {
_this.onLoadCallback();
}
/**
* Auth to the google Api.
*/
}).catch(function (e) {
console.log(e);
});
}
/**
* Init Google Api
* And create gapi in global
*/
}, {
key: 'initClient',
value: function initClient() {
var _this = this;
}, {
key: 'handleClientLoad',
value: function handleClientLoad() {
var _this2 = this;
this.gapi = window['gapi'];
this.gapi.client.init(Config).then(function () {
// Listen for sign-in state changes.
_this.gapi.auth2.getAuthInstance().isSignedIn.listen(_this.updateSigninStatus);
// Handle the initial sign-in state.
_this.updateSigninStatus(_this.gapi.auth2.getAuthInstance().isSignedIn.get());
if (_this.onLoadCallback) {
_this.onLoadCallback();
}
}).catch(function (e) {
console.log(e);
});
}
/**
* Init Google Api
* And create gapi in global
*/
this.gapi = window['gapi'];
var script = document.createElement('script');
script.src = 'https://apis.google.com/js/api.js';
document.body.appendChild(script);
script.onload = function () {
window['gapi'].load('client:auth2', _this2.initClient);
};
}
/**
* Sign in Google user account
*/
}, {
key: 'handleClientLoad',
value: function handleClientLoad() {
var _this2 = this;
}, {
key: 'handleAuthClick',
value: function handleAuthClick() {
if (this.gapi) {
this.gapi.auth2.getAuthInstance().signIn();
} else {
console.log('Error: this.gapi not loaded');
}
}
/**
* Set the default attribute calendar
* @param {string} newCalendar
*/
this.gapi = window['gapi'];
var script = document.createElement("script");
script.src = "https://apis.google.com/js/api.js";
document.body.appendChild(script);
script.onload = function () {
window['gapi'].load('client:auth2', _this2.initClient);
};
}
/**
* Sign in Google user account
*/
}, {
key: 'setCalendar',
value: function setCalendar(newCalendar) {
this.calendar = newCalendar;
}
/**
* Execute the callback function when a user is disconnected or connected with the sign status.
* @param callback
*/
}, {
key: 'handleAuthClick',
value: function handleAuthClick() {
if (this.gapi) {
this.gapi.auth2.getAuthInstance().signIn();
} else {
console.log("Error: this.gapi not loaded");
}
}
/**
* Set the default attribute calendar
* @param {string} newCalendar
*/
}, {
key: 'listenSign',
value: function listenSign(callback) {
if (this.gapi) {
this.gapi.auth2.getAuthInstance().isSignedIn.listen(callback);
} else {
console.log('Error: this.gapi not loaded');
}
}
/**
* Execute the callback function when gapi is loaded
* @param callback
*/
}, {
key: 'setCalendar',
value: function setCalendar(newCalendar) {
this.calendar = newCalendar;
}
/**
* Execute the callback function when a user is disconnected or connected with the sign status.
* @param callback
*/
}, {
key: 'onLoad',
value: function onLoad(callback) {
if (this.gapi) {
callback();
} else {
this.onLoadCallback = callback;
}
}
/**
* Sign out user google account
*/
}, {
key: 'listenSign',
value: function listenSign(callback) {
if (this.gapi) {
this.gapi.auth2.getAuthInstance().isSignedIn.listen(callback);
} else {
console.log("Error: this.gapi not loaded");
}
}
/**
* Execute the callback function when gapi is loaded
* @param callback
*/
}, {
key: 'handleSignoutClick',
value: function handleSignoutClick() {
if (this.gapi) {
this.gapi.auth2.getAuthInstance().signOut();
} else {
console.log('Error: this.gapi not loaded');
}
}
/**
* List all events in the calendar
* @param {number} maxResults to see
* @param {string} calendarId to see by default use the calendar attribute
* @returns {any}
*/
}, {
key: 'onLoad',
value: function onLoad(callback) {
if (this.gapi) {
callback();
} else {
this.onLoadCallback = callback;
}
}
/**
* Sign out user google account
*/
}, {
key: 'listUpcomingEvents',
value: function listUpcomingEvents(maxResults) {
var calendarId = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : this.calendar;
}, {
key: 'handleSignoutClick',
value: function handleSignoutClick() {
if (this.gapi) {
this.gapi.auth2.getAuthInstance().signOut();
} else {
console.log("Error: this.gapi not loaded");
}
}
/**
* List all events in the calendar
* @param {number} maxResults to see
* @param {string} calendarId to see by default use the calendar attribute
* @returns {any}
*/
if (this.gapi) {
return this.gapi.client.calendar.events.list({
calendarId: calendarId,
timeMin: new Date().toISOString(),
showDeleted: false,
singleEvents: true,
maxResults: maxResults,
orderBy: 'startTime'
});
} else {
console.log('Error: this.gapi not loaded');
return false;
}
}
/**
* Create an event from the current time for a certain period
* @param {number} time in minutes for the event
* @param {string} summary of the event
* @param {string} description of the event
* @param {string} calendarId
* @param {string} timeZone The time zone in which the time is specified. (Formatted as an IANA Time Zone Database name, e.g. "Europe/Zurich".)
* @returns {any}
*/
}, {
key: 'listUpcomingEvents',
value: function listUpcomingEvents(maxResults) {
var calendarId = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : this.calendar;
}, {
key: 'createEventFromNow',
value: function createEventFromNow(_ref) {
var time = _ref.time,
summary = _ref.summary,
_ref$description = _ref.description,
description = _ref$description === undefined ? '' : _ref$description;
var calendarId = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : this.calendar;
var timeZone = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : 'Europe/Paris';
if (this.gapi) {
return this.gapi.client.calendar.events.list({
'calendarId': calendarId,
'timeMin': new Date().toISOString(),
'showDeleted': false,
'singleEvents': true,
'maxResults': maxResults,
'orderBy': 'startTime'
});
} else {
console.log("Error: this.gapi not loaded");
return false;
}
var event = {
summary: summary,
description: description,
start: {
dateTime: new Date().toISOString(),
timeZone: timeZone
},
end: {
dateTime: new Date(new Date().getTime() + time * 60000),
timeZone: timeZone
}
/**
* Create an event from the current time for a certain period
* @param {number} time in minutes for the event
* @param {string} summary of the event
* @param {string} description of the event
* @param {string} calendarId
* @returns {any}
*/
};
return this.createEvent(event, calendarId);
}
/**
* Create Calendar event
* @param {string} calendarId for the event.
* @param {object} event with start and end dateTime
* @returns {any}
*/
}, {
key: 'createEventFromNow',
value: function createEventFromNow(_ref) {
var time = _ref.time,
summary = _ref.summary,
_ref$description = _ref.description,
description = _ref$description === undefined ? '' : _ref$description;
var calendarId = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : this.calendar;
}, {
key: 'createEvent',
value: function createEvent(event) {
var calendarId = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : this.calendar;
var event = {
summary: summary,
description: description,
start: {
dateTime: new Date().toISOString(),
timeZone: "Europe/Paris"
},
end: {
dateTime: new Date(new Date().getTime() + time * 60000),
timeZone: "Europe/Paris"
}
};
return this.gapi.client.calendar.events.insert({
'calendarId': calendarId,
'resource': event
});
}
/**
* Create Calendar event
* @param {string} calendarId for the event.
* @param {object} event with start and end dateTime
* @returns {any}
*/
if (this.gapi) {
return this.gapi.client.calendar.events.insert({
calendarId: calendarId,
resource: event
});
} else {
console.log('Error: gapi is not loaded use onLoad before please.');
return null;
}
}
}, {
key: 'createEvent',
value: function createEvent(event) {
var calendarId = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : this.calendar;
/**
* Delete an event in the calendar.
* @param {string} eventId of the event to delete.
* @param {string} calendarId where the event is.
* @returns {any} Promise resolved when the event is deleted.
*/
return this.gapi.client.calendar.events.insert({
'calendarId': calendarId,
'resource': event
});
}
}, {
key: 'updateEvent',
value: function updateEvent(event, eventId, calendarId = this.calendar) {
return this.gapi.client.calendar.events.patch({
calendarId,
eventId,
'resource': event,
});
}
}]);
}, {
key: 'deleteEvent',
value: function deleteEvent(eventId) {
var calendarId = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : this.calendar;
return ApiCalendar;
if (this.gapi) {
return this.gapi.client.calendar.events.delete({
calendarId: calendarId,
eventId: eventId
});
} else {
console.log('Error: gapi is not loaded use onLoad before please.');
return null;
}
}
/**
* @returns {any} Get the user's basic profile information. Documentation: https://developers.google.com/identity/sign-in/web/reference#googleusergetbasicprofile
*/
}, {
key: 'getBasicUserProfile',
value: function getBasicUserProfile() {
if (this.gapi) {
return this.gapi.auth2.getAuthInstance().currentUser.get().getBasicProfile();
} else {
console.log('Error: gapi is not loaded use onLoad before please.');
return null;
}
}
/**
* Update Calendar event
* @param {string} calendarId for the event.
* @param {string} eventId of the event.
* @param {object} event with details to update, e.g. summary
* @returns {any}
*/
}, {
key: 'updateEvent',
value: function updateEvent(event, eventId) {
var calendarId = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : this.calendar;
if (this.gapi) {
return this.gapi.client.calendar.events.patch({
calendarId: calendarId,
eventId: eventId,
resource: event
});
} else {
console.log('Error: gapi is not loaded use onLoad before please.');
return null;
}
}
/**
* Get Calendar event
* @param {string} calendarId for the event.
* @param {string} eventId specifies individual event
* @returns {any}
*/
}, {
key: 'getEvent',
value: function getEvent(eventId) {
var calendarId = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : this.calendar;
if (this.gapi) {
return this.gapi.client.calendar.events.get({
calendarId: calendarId,
eventId: eventId
});
} else {
console.log('Error: gapi is not loaded use onLoad before please.');
return null;
}
}
}]);
return ApiCalendar;
}();

@@ -248,6 +332,6 @@

try {
apiCalendar = new ApiCalendar();
apiCalendar = new ApiCalendar();
} catch (e) {
console.log(e);
console.log(e);
}
exports.default = apiCalendar;
{
"name": "react-google-calendar-api",
"version": "1.2.1",
"version": "1.3.0",
"description": "An api to manage your google calendar",

@@ -5,0 +5,0 @@ "main": "./build/ApiCalendar.js",

@@ -23,2 +23,3 @@ # react-google-calendar-api

### Typescript Import
```

@@ -36,3 +37,5 @@ import ApiCalendar from 'react-google-calendar-api/src/ApiCalendar';

"scope": "https://www.googleapis.com/auth/calendar",
"discoveryDocs": ["https://www.googleapis.com/discovery/v1/apis/calendar/v3/rest"]
"discoveryDocs": [
"https://www.googleapis.com/discovery/v1/apis/calendar/v3/rest"
]
}

@@ -66,3 +69,3 @@ ```

import ApiCalendar from 'react-google-calendar-api';
export default class DoubleButton extends React.Component {

@@ -73,3 +76,3 @@ constructor(props) {

}
public handleItemClick(event: SyntheticEvent<any>, name: string): void {

@@ -99,3 +102,3 @@ if (name === 'sign-in') {

```
### setCalendar:

@@ -117,11 +120,12 @@

```javascript
/**
* Create calendar event
* @param {string} CalendarId for the event by default use 'primary'.
* @param {object} Event with start and end dateTime
* @returns {any} Promise on the event.
*/
public createEvent(event: object, calendarId: string = this.calendar): any {
```
```javascript
/**
* Create calendar event
* @param {string} CalendarId for the event by default use 'primary'.
* @param {object} Event with start and end dateTime
* @returns {any} Promise on the event.
*/
public createEvent(event: object, calendarId: string = this.calendar): any {
```
### Create Event From Now:

@@ -136,23 +140,25 @@

* @param {string} CalendarId by default calendar set by setCalendar.
* @param {string} timeZone The time zone in which the time is specified. (Formatted as an IANA Time Zone Database name, e.g. "Europe/Zurich".)
* @returns {any} Promise on the event.
*/
public createEventFromNow({time, summary, description = ''}: any, calendarId: string = this.calendar): any
```
*/
public createEventFromNow({time, summary, description = ''}: any, calendarId: string = this.calendar, timeZone: string = "Europe/Paris"): any
```
#### Example
```javascript
import ApiCalendar from 'react-google-calendar-api';
import ApiCalendar from 'react-google-calendar-api';
const eventFromNow: object = {
summary: "Poc Dev From Now",
time: 480,
};
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);
});
ApiCalendar.createEventFromNow(eventFromNow)
.then((result: object) => {
console.log(result);
})
.catch((error: any) => {
console.log(error);
});
```

@@ -175,9 +181,8 @@

```javascript
import ApiCalendar from 'react-google-calendar-api';
if (ApiCalendar.sign)
ApiCalendar.listUpcomingEvents(10)
.then(({result}: any) => {
console.log(result.items);
});
import ApiCalendar from 'react-google-calendar-api';
if (ApiCalendar.sign)
ApiCalendar.listUpcomingEvents(10).then(({ result }: any) => {
console.log(result.items);
});
```

@@ -187,26 +192,99 @@

```javascript
/**
* Update Calendar event
* @param {string} calendarId for the event.
* @param {string} eventId of the event.
* @param {object} event with details to update, e.g. summary
* @returns {any} Promise object with result
*/
public updateEvent(event: object, eventId: string, calendarId: string = this.calendar): any
```
```javascript
/**
* Update Calendar event
* @param {string} calendarId for the event.
* @param {string} eventId of the event.
* @param {object} event with details to update, e.g. summary
* @returns {any} Promise object with result
*/
public updateEvent(event: object, eventId: string, calendarId: string = this.calendar): any
```
#### Example
#### Example
```javascript
import ApiCalendar from 'react-google-calendar-api';
const event = {
summary: 'New Event Title'
};
import ApiCalendar from 'react-google-calendar-api';
ApiCalendar.updateEvent(event, '2eo85lmjkkd2i63uo3lhi8a2cq')
.then(console.log);
const event = {
summary: 'New Event Title',
};
ApiCalendar.updateEvent(event, '2eo85lmjkkd2i63uo3lhi8a2cq').then(console.log);
```
### Delete Event
```javascript
/**
* Delete an event in the calendar.
* @param {string} eventId of the event to delete.
* @param {string} calendarId where the event is.
* @returns {any} Promise resolved when the event is deleted.
*/
public deleteEvent(eventId: string, calendarId: string = this.calendar): any
```
#### Example
```javascript
import ApiCalendar from 'react-google-calendar-api';
ApiCalendar.deleteEvent('2eo85lmjkkd2i63uo3lhi8a2cq').then(console.log);
```
### Get Event
```javascript
/**
* Get Calendar event
* @param {string} calendarId for the event.
* @param {string} eventId specifies individual event
* @returns {any}
*/
public getEvent(eventId: string, calendarId: string = this.calendar): any
```
#### Example
```javascript
import ApiCalendar from 'react-google-calendar-api';
ApiCalendar.getEvent('2eo85lmjkkd2i63uo3lhi8a2cq').then(console.log);
```
### Get BasicUserProfile Event
```javascript
/**
* @returns {any} Get the user's basic profile information.
* Documentation: https://developers.google.com/identity/sign-in/web/reference#googleusergetbasicprofile
*/
public getBasicUserProfile(): any
```
#### Examples
```javascript
import ApiCalendar from 'react-google-calendar-api';
ApiCalendar.getBasicUserProfile('2eo85lmjkkd2i63uo3lhi8a2cq')
.getEmail()
.then(console.log);
ApiCalendar.getBasicUserProfile('2eo85lmjkkd2i63uo3lhi8a2cq')
.getName()
.then(console.log);
```
### or with async/wait
```javascript
import ApiCalendar from 'react-google-calendar-api';
const response = await ApiCalendar.getBasicUserProfile();
response.getEmail();
```
## Utils

@@ -239,3 +317,3 @@

import ApiCalendar from 'react-google-calendar-api';
export default class StatusSign extends React.Component<any, any> {

@@ -258,3 +336,3 @@ constructor(props) {

}
render(): ReactNode {

@@ -261,0 +339,0 @@ return (

@@ -1,2 +0,2 @@

const Config = require("../../../apiGoogleconfig.json");
const Config = require('../../../apiGoogleconfig.json');
class ApiCalendar {

@@ -19,2 +19,6 @@ constructor() {

this.setCalendar = this.setCalendar.bind(this);
this.updateEvent = this.updateEvent.bind(this);
this.deleteEvent = this.deleteEvent.bind(this);
this.getEvent = this.getEvent.bind(this);
this.getBasicUserProfile = this.getBasicUserProfile.bind(this);
this.handleClientLoad();

@@ -38,6 +42,9 @@ }

this.gapi = window['gapi'];
this.gapi.client.init(Config)
this.gapi.client
.init(Config)
.then(() => {
// Listen for sign-in state changes.
this.gapi.auth2.getAuthInstance().isSignedIn.listen(this.updateSigninStatus);
this.gapi.auth2
.getAuthInstance()
.isSignedIn.listen(this.updateSigninStatus);
// Handle the initial sign-in state.

@@ -59,4 +66,4 @@ this.updateSigninStatus(this.gapi.auth2.getAuthInstance().isSignedIn.get());

this.gapi = window['gapi'];
const script = document.createElement("script");
script.src = "https://apis.google.com/js/api.js";
const script = document.createElement('script');
script.src = 'https://apis.google.com/js/api.js';
document.body.appendChild(script);

@@ -75,3 +82,3 @@ script.onload = () => {

else {
console.log("Error: this.gapi not loaded");
console.log('Error: this.gapi not loaded');
}

@@ -95,3 +102,3 @@ }

else {
console.log("Error: this.gapi not loaded");
console.log('Error: this.gapi not loaded');
}

@@ -119,3 +126,3 @@ }

else {
console.log("Error: this.gapi not loaded");
console.log('Error: this.gapi not loaded');
}

@@ -132,12 +139,12 @@ }

return this.gapi.client.calendar.events.list({
'calendarId': calendarId,
'timeMin': (new Date()).toISOString(),
'showDeleted': false,
'singleEvents': true,
'maxResults': maxResults,
'orderBy': 'startTime'
calendarId: calendarId,
timeMin: new Date().toISOString(),
showDeleted: false,
singleEvents: true,
maxResults: maxResults,
orderBy: 'startTime',
});
}
else {
console.log("Error: this.gapi not loaded");
console.log('Error: this.gapi not loaded');
return false;

@@ -152,5 +159,6 @@ }

* @param {string} calendarId
* @param {string} timeZone The time zone in which the time is specified. (Formatted as an IANA Time Zone Database name, e.g. "Europe/Zurich".)
* @returns {any}
*/
createEventFromNow({ time, summary, description = '' }, calendarId = this.calendar) {
createEventFromNow({ time, summary, description = '' }, calendarId = this.calendar, timeZone = 'Europe/Paris') {
const event = {

@@ -160,14 +168,11 @@ summary,

start: {
dateTime: (new Date()).toISOString(),
timeZone: "Europe/Paris",
dateTime: new Date().toISOString(),
timeZone: timeZone,
},
end: {
dateTime: (new Date(new Date().getTime() + time * 60000)),
timeZone: "Europe/Paris",
}
dateTime: new Date(new Date().getTime() + time * 60000),
timeZone: timeZone,
},
};
return this.gapi.client.calendar.events.insert({
'calendarId': calendarId,
'resource': event,
});
return this.createEvent(event, calendarId);
}

@@ -181,7 +186,84 @@ /**

createEvent(event, calendarId = this.calendar) {
return this.gapi.client.calendar.events.insert({
'calendarId': calendarId,
'resource': event,
});
if (this.gapi) {
return this.gapi.client.calendar.events.insert({
calendarId: calendarId,
resource: event,
});
}
else {
console.log('Error: this.gapi not loaded');
return false;
}
}
/**
* Delete an event in the calendar.
* @param {string} eventId of the event to delete.
* @param {string} calendarId where the event is.
* @returns {any} Promise resolved when the event is deleted.
*/
deleteEvent(eventId, calendarId = this.calendar) {
if (this.gapi) {
return this.gapi.client.calendar.events.delete({
calendarId: calendarId,
eventId: eventId,
});
}
else {
console.log('Error: gapi is not loaded use onLoad before please.');
return null;
}
}
/**
* @returns {any} Get the user's basic profile information. Documentation: https://developers.google.com/identity/sign-in/web/reference#googleusergetbasicprofile
*/
getBasicUserProfile() {
if (this.gapi) {
return this.gapi.auth2
.getAuthInstance()
.currentUser.get()
.getBasicProfile();
}
else {
console.log('Error: gapi is not loaded use onLoad before please.');
return null;
}
}
/**
* Update Calendar event
* @param {string} calendarId for the event.
* @param {string} eventId of the event.
* @param {object} event with details to update, e.g. summary
* @returns {any}
*/
updateEvent(event, eventId, calendarId = this.calendar) {
if (this.gapi) {
return this.gapi.client.calendar.events.patch({
calendarId: calendarId,
eventId: eventId,
resource: event,
});
}
else {
console.log('Error: gapi is not loaded use onLoad before please.');
return null;
}
}
/**
* Get Calendar event
* @param {string} calendarId for the event.
* @param {string} eventId specifies individual event
* @returns {any}
*/
getEvent(eventId, calendarId = this.calendar) {
if (this.gapi) {
return this.gapi.client.calendar.events.get({
calendarId: calendarId,
eventId: eventId,
});
}
else {
console.log('Error: gapi is not loaded use onLoad before please.');
return null;
}
}
}

@@ -188,0 +270,0 @@ let apiCalendar;

@@ -1,186 +0,280 @@

const Config = require("../../../apiGoogleconfig.json");
const Config = require('../../../apiGoogleconfig.json');
class ApiCalendar {
sign: boolean = false;
gapi: any = null;
onLoadCallback: any = null;
calendar: string = 'primary';
sign: boolean = false;
gapi: any = null;
onLoadCallback: any = null;
calendar: string = 'primary';
constructor() {
try {
this.updateSigninStatus = this.updateSigninStatus.bind(this);
this.initClient = this.initClient.bind(this);
this.handleSignoutClick = this.handleSignoutClick.bind(this);
this.handleAuthClick = this.handleAuthClick.bind(this);
this.createEvent = this.createEvent.bind(this);
this.listUpcomingEvents = this.listUpcomingEvents.bind(this);
this.createEventFromNow = this.createEventFromNow.bind(this);
this.listenSign = this.listenSign.bind(this);
this.onLoad = this.onLoad.bind(this);
this.setCalendar = this.setCalendar.bind(this);
constructor() {
try {
this.updateSigninStatus = this.updateSigninStatus.bind(this);
this.initClient = this.initClient.bind(this);
this.handleSignoutClick = this.handleSignoutClick.bind(this);
this.handleAuthClick = this.handleAuthClick.bind(this);
this.createEvent = this.createEvent.bind(this);
this.listUpcomingEvents = this.listUpcomingEvents.bind(this);
this.createEventFromNow = this.createEventFromNow.bind(this);
this.listenSign = this.listenSign.bind(this);
this.onLoad = this.onLoad.bind(this);
this.setCalendar = this.setCalendar.bind(this);
this.updateEvent = this.updateEvent.bind(this);
this.deleteEvent = this.deleteEvent.bind(this);
this.getEvent = this.getEvent.bind(this);
this.getBasicUserProfile = this.getBasicUserProfile.bind(this);
this.handleClientLoad();
} catch (e) {
console.log(e);
}
}
this.handleClientLoad();
} catch (e) {
console.log(e);
/**
* Update connection status.
* @param {boolean} isSignedIn
*/
private updateSigninStatus(isSignedIn: boolean): void {
this.sign = isSignedIn;
}
/**
* Auth to the google Api.
*/
private initClient(): void {
this.gapi = window['gapi'];
this.gapi.client
.init(Config)
.then(() => {
// Listen for sign-in state changes.
this.gapi.auth2
.getAuthInstance()
.isSignedIn.listen(this.updateSigninStatus);
// Handle the initial sign-in state.
this.updateSigninStatus(
this.gapi.auth2.getAuthInstance().isSignedIn.get()
);
if (this.onLoadCallback) {
this.onLoadCallback();
}
}
})
.catch((e: any) => {
console.log(e);
});
}
/**
* Update connection status.
* @param {boolean} isSignedIn
*/
private updateSigninStatus(isSignedIn: boolean): void {
this.sign = isSignedIn;
/**
* Init Google Api
* And create gapi in global
*/
private handleClientLoad(): void {
this.gapi = window['gapi'];
const script = document.createElement('script');
script.src = 'https://apis.google.com/js/api.js';
document.body.appendChild(script);
script.onload = (): void => {
window['gapi'].load('client:auth2', this.initClient);
};
}
/**
* Sign in Google user account
*/
public handleAuthClick(): void {
if (this.gapi) {
this.gapi.auth2.getAuthInstance().signIn();
} else {
console.log('Error: this.gapi not loaded');
}
}
/**
* Auth to the google Api.
*/
private initClient(): void {
this.gapi = window['gapi'];
this.gapi.client.init(Config)
.then(() => {
// Listen for sign-in state changes.
this.gapi.auth2.getAuthInstance().isSignedIn.listen(this.updateSigninStatus);
// Handle the initial sign-in state.
this.updateSigninStatus(this.gapi.auth2.getAuthInstance().isSignedIn.get());
if (this.onLoadCallback) {
this.onLoadCallback();
}
})
.catch((e: any) => {
console.log(e);
})
/**
* Set the default attribute calendar
* @param {string} newCalendar
*/
public setCalendar(newCalendar: string): void {
this.calendar = newCalendar;
}
/**
* Execute the callback function when a user is disconnected or connected with the sign status.
* @param callback
*/
public listenSign(callback: any): void {
if (this.gapi) {
this.gapi.auth2.getAuthInstance().isSignedIn.listen(callback);
} else {
console.log('Error: this.gapi not loaded');
}
}
/**
* Init Google Api
* And create gapi in global
*/
private handleClientLoad(): void {
this.gapi = window['gapi'];
const script = document.createElement("script");
script.src = "https://apis.google.com/js/api.js";
document.body.appendChild(script);
script.onload = (): void => {
window['gapi'].load('client:auth2', this.initClient);
}
/**
* Execute the callback function when gapi is loaded
* @param callback
*/
public onLoad(callback: any): void {
if (this.gapi) {
callback();
} else {
this.onLoadCallback = callback;
}
}
/**
* Sign in Google user account
*/
public handleAuthClick(): void {
if (this.gapi) {
this.gapi.auth2.getAuthInstance().signIn();
} else {
console.log("Error: this.gapi not loaded")
}
/**
* Sign out user google account
*/
public handleSignoutClick(): void {
if (this.gapi) {
this.gapi.auth2.getAuthInstance().signOut();
} else {
console.log('Error: this.gapi not loaded');
}
}
/**
* Set the default attribute calendar
* @param {string} newCalendar
*/
public setCalendar(newCalendar: string): void {
this.calendar = newCalendar;
/**
* List all events in the calendar
* @param {number} maxResults to see
* @param {string} calendarId to see by default use the calendar attribute
* @returns {any}
*/
public listUpcomingEvents(
maxResults: number,
calendarId: string = this.calendar
): any {
if (this.gapi) {
return this.gapi.client.calendar.events.list({
calendarId: calendarId,
timeMin: new Date().toISOString(),
showDeleted: false,
singleEvents: true,
maxResults: maxResults,
orderBy: 'startTime',
});
} else {
console.log('Error: this.gapi not loaded');
return false;
}
}
/**
* Execute the callback function when a user is disconnected or connected with the sign status.
* @param callback
*/
public listenSign(callback: any): void {
if (this.gapi) {
this.gapi.auth2.getAuthInstance().isSignedIn.listen(callback);
} else {
console.log("Error: this.gapi not loaded")
}
/**
* Create an event from the current time for a certain period
* @param {number} time in minutes for the event
* @param {string} summary of the event
* @param {string} description of the event
* @param {string} calendarId
* @param {string} timeZone The time zone in which the time is specified. (Formatted as an IANA Time Zone Database name, e.g. "Europe/Zurich".)
* @returns {any}
*/
public createEventFromNow(
{ time, summary, description = '' }: any,
calendarId: string = this.calendar,
timeZone: string = 'Europe/Paris'
): any {
const event = {
summary,
description,
start: {
dateTime: new Date().toISOString(),
timeZone: timeZone,
},
end: {
dateTime: new Date(new Date().getTime() + time * 60000),
timeZone: timeZone,
},
};
return this.createEvent(event, calendarId);
}
/**
* Create Calendar event
* @param {string} calendarId for the event.
* @param {object} event with start and end dateTime
* @returns {any}
*/
public createEvent(event: object, calendarId: string = this.calendar): any {
if (this.gapi) {
return this.gapi.client.calendar.events.insert({
calendarId: calendarId,
resource: event,
});
} else {
console.log('Error: this.gapi not loaded');
return false;
}
}
/**
* Execute the callback function when gapi is loaded
* @param callback
*/
public onLoad(callback: any): void {
if (this.gapi) {
callback();
} else {
this.onLoadCallback = callback;
}
/**
* Delete an event in the calendar.
* @param {string} eventId of the event to delete.
* @param {string} calendarId where the event is.
* @returns {any} Promise resolved when the event is deleted.
*/
deleteEvent(eventId: string, calendarId: string = this.calendar): any {
if (this.gapi) {
return this.gapi.client.calendar.events.delete({
calendarId: calendarId,
eventId: eventId,
});
} else {
console.log('Error: gapi is not loaded use onLoad before please.');
return null;
}
}
/**
* Sign out user google account
*/
public handleSignoutClick(): void {
if (this.gapi) {
this.gapi.auth2.getAuthInstance().signOut();
} else {
console.log("Error: this.gapi not loaded");
}
/**
* @returns {any} Get the user's basic profile information. Documentation: https://developers.google.com/identity/sign-in/web/reference#googleusergetbasicprofile
*/
getBasicUserProfile(): any {
if (this.gapi) {
return this.gapi.auth2
.getAuthInstance()
.currentUser.get()
.getBasicProfile();
} else {
console.log('Error: gapi is not loaded use onLoad before please.');
return null;
}
}
/**
* List all events in the calendar
* @param {number} maxResults to see
* @param {string} calendarId to see by default use the calendar attribute
* @returns {any}
*/
public listUpcomingEvents(maxResults: number, calendarId: string = this.calendar): any {
if (this.gapi) {
return this.gapi.client.calendar.events.list({
'calendarId': calendarId,
'timeMin': (new Date()).toISOString(),
'showDeleted': false,
'singleEvents': true,
'maxResults': maxResults,
'orderBy': 'startTime'
})
} else {
console.log("Error: this.gapi not loaded");
return false;
}
/**
* Update Calendar event
* @param {string} calendarId for the event.
* @param {string} eventId of the event.
* @param {object} event with details to update, e.g. summary
* @returns {any}
*/
updateEvent(
event: object,
eventId: string,
calendarId: string = this.calendar
): any {
if (this.gapi) {
return this.gapi.client.calendar.events.patch({
calendarId: calendarId,
eventId: eventId,
resource: event,
});
} else {
console.log('Error: gapi is not loaded use onLoad before please.');
return null;
}
}
/**
* Create an event from the current time for a certain period
* @param {number} time in minutes for the event
* @param {string} summary of the event
* @param {string} description of the event
* @param {string} calendarId
* @returns {any}
*/
public createEventFromNow({time, summary, description = ''}: any, calendarId: string = this.calendar): any {
const event = {
summary,
description,
start: {
dateTime: (new Date()).toISOString(),
timeZone: "Europe/Paris",
},
end: {
dateTime: (new Date(new Date().getTime() + time * 60000)),
timeZone: "Europe/Paris",
}
};
/**
* Get Calendar event
* @param {string} calendarId for the event.
* @param {string} eventId specifies individual event
* @returns {any}
*/
return this.gapi.client.calendar.events.insert({
'calendarId': calendarId,
'resource': event,
});
getEvent(eventId: string, calendarId: string = this.calendar): any {
if (this.gapi) {
return this.gapi.client.calendar.events.get({
calendarId: calendarId,
eventId: eventId,
});
} else {
console.log('Error: gapi is not loaded use onLoad before please.');
return null;
}
/**
* Create Calendar event
* @param {string} calendarId for the event.
* @param {object} event with start and end dateTime
* @returns {any}
*/
public createEvent(event: object, calendarId: string = this.calendar): any {
return this.gapi.client.calendar.events.insert({
'calendarId': calendarId,
'resource': event,
});
}
}
}

@@ -190,6 +284,6 @@

try {
apiCalendar = new ApiCalendar();
apiCalendar = new ApiCalendar();
} catch (e) {
console.log(e);
console.log(e);
}
export default apiCalendar;
export default apiCalendar;
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc