mirror of
https://github.com/urosran/cally.git
synced 2025-11-26 00:24:53 +00:00
- Introduced a button for syncing Microsoft calendar
- Implemented saving of the microsoft event data in db
This commit is contained in:
40
calendar-integration/microsoft-calendar-utils.js
Normal file
40
calendar-integration/microsoft-calendar-utils.js
Normal file
@ -0,0 +1,40 @@
|
|||||||
|
export async function fetchMicrosoftCalendarEvents(token, startDate, endDate) {
|
||||||
|
const response = await fetch(
|
||||||
|
`https://graph.microsoft.com/v1.0/me/calendar/calendarView?startDateTime=${startDate}&endDateTime=${endDate}`,
|
||||||
|
{
|
||||||
|
headers: {
|
||||||
|
Authorization: `Bearer ${token}`,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
);
|
||||||
|
|
||||||
|
const data = await response.json();
|
||||||
|
|
||||||
|
const microsoftEvents = [];
|
||||||
|
data?.value?.forEach((item) => {
|
||||||
|
const start = item.start;
|
||||||
|
let startDateTime;
|
||||||
|
if (start !== undefined) {
|
||||||
|
const stringDate = start.dateTime;
|
||||||
|
startDateTime = new Date(stringDate);
|
||||||
|
}
|
||||||
|
|
||||||
|
const end = item.end;
|
||||||
|
let endDateTime;
|
||||||
|
if (end !== undefined) {
|
||||||
|
const stringDate = end.dateTime;
|
||||||
|
endDateTime = new Date(stringDate);
|
||||||
|
}
|
||||||
|
|
||||||
|
const microsoftEvent = {
|
||||||
|
id: item.uid,
|
||||||
|
title: item.subject,
|
||||||
|
startDate: startDateTime,
|
||||||
|
endDate: endDateTime,
|
||||||
|
allDay: item.isAllDay,
|
||||||
|
};
|
||||||
|
microsoftEvents.push(microsoftEvent);
|
||||||
|
});
|
||||||
|
|
||||||
|
return microsoftEvents;
|
||||||
|
}
|
||||||
@ -5,6 +5,7 @@ import { StyleSheet } from "react-native";
|
|||||||
import { colorMap } from "@/contexts/SettingsContext";
|
import { colorMap } from "@/contexts/SettingsContext";
|
||||||
import { TouchableOpacity } from "react-native-gesture-handler";
|
import { TouchableOpacity } from "react-native-gesture-handler";
|
||||||
import { fetchGoogleCalendarEvents } from "@/calendar-integration/google-calendar-utils";
|
import { fetchGoogleCalendarEvents } from "@/calendar-integration/google-calendar-utils";
|
||||||
|
import { fetchMicrosoftCalendarEvents } from "@/calendar-integration/microsoft-calendar-utils";
|
||||||
import { useCreateEvent } from "@/hooks/firebase/useCreateEvent";
|
import { useCreateEvent } from "@/hooks/firebase/useCreateEvent";
|
||||||
import { GoogleSignin } from "@react-native-google-signin/google-signin";
|
import { GoogleSignin } from "@react-native-google-signin/google-signin";
|
||||||
|
|
||||||
@ -25,18 +26,27 @@ const CalendarSettingsPage = (props: {
|
|||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
|
const fetchAndSaveMicrosoftEvents = (token: string) => {
|
||||||
|
const startDateTime = new Date(new Date().setHours(0, 0, 0, 0));
|
||||||
|
const endDateTime = new Date(
|
||||||
|
new Date(new Date().setHours(0, 0, 0, 0)).setDate(
|
||||||
|
startDateTime.getDate() + 30,
|
||||||
|
),
|
||||||
|
);
|
||||||
|
|
||||||
|
fetchMicrosoftCalendarEvents(
|
||||||
|
token,
|
||||||
|
startDateTime.toISOString().slice(0, -5) + "Z",
|
||||||
|
endDateTime.toISOString().slice(0, -5) + "Z",
|
||||||
|
).then((response) => {
|
||||||
|
response?.forEach((item) => createEvent(item));
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
async function saveData(item) {
|
async function saveData(item) {
|
||||||
await createEvent(item);
|
await createEvent(item);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Note: Right key
|
|
||||||
GoogleSignin.configure({
|
|
||||||
webClientId:
|
|
||||||
"406146460310-81gld1b58ujsoe6a2t3ht0haprv5pa69.apps.googleusercontent.com",
|
|
||||||
scopes: ["profile", "email", "calendar"],
|
|
||||||
offlineAccess: true,
|
|
||||||
});
|
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<View marginH-30>
|
<View marginH-30>
|
||||||
<TouchableOpacity onPress={() => props.setSelectedPage(0)}>
|
<TouchableOpacity onPress={() => props.setSelectedPage(0)}>
|
||||||
@ -150,7 +160,7 @@ const CalendarSettingsPage = (props: {
|
|||||||
{/** Note: Should check for the user if it has connected calendars **/}
|
{/** Note: Should check for the user if it has connected calendars **/}
|
||||||
<View style={{ marginTop: 20 }}>
|
<View style={{ marginTop: 20 }}>
|
||||||
<Button
|
<Button
|
||||||
label={"Sync Calendar"}
|
label={"Sync Google"}
|
||||||
iconSource={() => (
|
iconSource={() => (
|
||||||
<View
|
<View
|
||||||
backgroundColor="#ededed"
|
backgroundColor="#ededed"
|
||||||
@ -170,6 +180,27 @@ const CalendarSettingsPage = (props: {
|
|||||||
borderRadius={15}
|
borderRadius={15}
|
||||||
onPress={fetchAndSaveGoogleEvents}
|
onPress={fetchAndSaveGoogleEvents}
|
||||||
/>
|
/>
|
||||||
|
<Button
|
||||||
|
label={"Sync Outlook"}
|
||||||
|
iconSource={() => (
|
||||||
|
<View
|
||||||
|
backgroundColor="#ededed"
|
||||||
|
width={40}
|
||||||
|
height={40}
|
||||||
|
style={{ borderRadius: 50 }}
|
||||||
|
marginR-10
|
||||||
|
centerV
|
||||||
|
centerH
|
||||||
|
>
|
||||||
|
<AntDesign name="google" size={30} color="#6c645b" />
|
||||||
|
</View>
|
||||||
|
)}
|
||||||
|
icon={<AntDesign name="checkcircleo" size={30} color="#8005eb" />}
|
||||||
|
backgroundColor="white"
|
||||||
|
color="#464039"
|
||||||
|
borderRadius={15}
|
||||||
|
onPress={fetchAndSaveMicrosoftEvents}
|
||||||
|
/>
|
||||||
</View>
|
</View>
|
||||||
</View>
|
</View>
|
||||||
</View>
|
</View>
|
||||||
@ -177,6 +208,13 @@ const CalendarSettingsPage = (props: {
|
|||||||
};
|
};
|
||||||
|
|
||||||
const signInGoogle = async () => {
|
const signInGoogle = async () => {
|
||||||
|
GoogleSignin.configure({
|
||||||
|
webClientId:
|
||||||
|
"406146460310-81gld1b58ujsoe6a2t3ht0haprv5pa69.apps.googleusercontent.com",
|
||||||
|
scopes: ["profile", "email", "calendar"],
|
||||||
|
offlineAccess: true,
|
||||||
|
});
|
||||||
|
|
||||||
try {
|
try {
|
||||||
await GoogleSignin.hasPlayServices();
|
await GoogleSignin.hasPlayServices();
|
||||||
let userInfo = await GoogleSignin.signIn();
|
let userInfo = await GoogleSignin.signIn();
|
||||||
|
|||||||
Reference in New Issue
Block a user