Repository navigation
Expand file tree
/
Copy pathGoogleAPICalendar.js
More file actions
188 lines (188 loc) · 5.5 KB
/
Copy pathGoogleAPICalendar.js
File metadata and controls
188 lines (188 loc) · 5.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
const Config = require("../../../apiGoogleconfig.json");
class GoogleAPICalendar {
constructor() {
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.handleClientLoad();
}
catch (e) {
console.log(e);
}
}
/**
* Update connection status.
* @param {boolean} isSignedIn
*/
updateSigninStatus(isSignedIn) {
this.sign = isSignedIn;
}
/**
* Auth to the google Api.
*/
initClient() {
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) => {
console.log(e);
});
}
/**
* Init Google Api
* And create gapi in global
*/
handleClientLoad() {
this.gapi = window["gapi"];
const script = document.createElement("script");
script.src = "https://apis.google.com/js/api.js";
document.body.appendChild(script);
script.onload = () => {
window["gapi"].load("client:auth2", this.initClient);
};
}
/**
* Sign in Google user account
*/
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
*/
setCalendar(newCalendar) {
this.calendar = newCalendar;
}
/**
* Execute the callback function when a user is disconnected or connected with the sign status.
* @param callback
*/
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
*/
onLoad(callback) {
if (this.gapi) {
callback();
}
else {
this.onLoadCallback = callback;
}
}
/**
* Sign out user google account
*/
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}
*/
listUpcomingEvents(maxResults, calendarId = this.calendar) {
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
* @returns {any}
*/
createEventFromNow({ time, summary, description = "" }, calendarId = this.calendar) {
const event = {
summary,
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}
*/
createEvent(event, calendarId = this.calendar) {
return this.gapi.client.calendar.events.insert({
calendarId: calendarId,
resource: event
});
}
}
let googleApiCalendar;
try {
googleApiCalendar = new GoogleAPICalendar();
}
catch (e) {
console.log(e);
}
export default googleApiCalendar;