forked from Kubessandra/react-google-calendar-api
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ApiCalendar.ts
194 lines (178 loc) · 5.67 KB
/
ApiCalendar.ts
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
189
190
191
192
193
194
const Config = require("../../../apiGoogleconfig.json");
class ApiCalendar {
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);
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);
})
}
/**
* 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")
}
}
/**
* 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")
}
}
/**
* Execute the callback function when gapi is loaded
* @param callback
*/
public onLoad(callback: any): void {
if (this.gapi) {
callback();
} else {
this.onLoadCallback = callback;
}
}
/**
* Sign out user google account
*/
public handleSignoutClick(): void {
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}
*/
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;
}
}
/**
* 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",
}
};
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}
*/
public createEvent(event: object, calendarId: string = this.calendar): any {
return this.gapi.client.calendar.events.insert({
'calendarId': calendarId,
'resource': event,
});
}
}
let apiCalendar;
try {
apiCalendar = new ApiCalendar();
} catch (e) {
console.log(e);
}
export default apiCalendar;