mirror of
https://github.com/urosran/cally.git
synced 2025-07-15 17:47:08 +00:00
Syncing improvement
This commit is contained in:
@ -1,4 +1,4 @@
|
||||
export async function fetchGoogleCalendarEvents(token, startDate, endDate) {
|
||||
export async function fetchGoogleCalendarEvents(token, email, startDate, endDate) {
|
||||
console.log(token);
|
||||
const response = await fetch(
|
||||
`https://www.googleapis.com/calendar/v3/calendars/primary/events?single_events=true&time_min=${startDate}&time_max=${endDate}`,
|
||||
@ -45,6 +45,7 @@ export async function fetchGoogleCalendarEvents(token, startDate, endDate) {
|
||||
startDate: startDateTime,
|
||||
endDate: endDateTime,
|
||||
allDay: isAllDay,
|
||||
email
|
||||
};
|
||||
googleEvents.push(googleEvent);
|
||||
});
|
||||
|
@ -1,4 +1,4 @@
|
||||
export async function fetchMicrosoftCalendarEvents(token, startDate, endDate) {
|
||||
export async function fetchMicrosoftCalendarEvents(token, email, startDate, endDate) {
|
||||
const response = await fetch(
|
||||
`https://graph.microsoft.com/v1.0/me/calendar/calendarView?startDateTime=${startDate}&endDateTime=${endDate}`,
|
||||
{
|
||||
@ -34,6 +34,7 @@ export async function fetchMicrosoftCalendarEvents(token, startDate, endDate) {
|
||||
startDate: startDateTime,
|
||||
endDate: endDateTime,
|
||||
allDay: item.isAllDay,
|
||||
email
|
||||
};
|
||||
|
||||
|
||||
|
@ -23,6 +23,8 @@ export const EventCalendar: React.FC<EventCalendarProps> = memo(({calendarHeight
|
||||
const setEventForEdit = useSetAtom(eventForEditAtom)
|
||||
const setSelectedNewEndDate = useSetAtom(selectedNewEventDateAtom)
|
||||
|
||||
console.log("Events: ", events)
|
||||
|
||||
return (
|
||||
<Calendar
|
||||
bodyContainerStyle={styles.calHeader}
|
||||
|
@ -6,7 +6,7 @@ import {colorMap} from "@/contexts/SettingsContext";
|
||||
import {TouchableOpacity} from "react-native-gesture-handler";
|
||||
import {fetchGoogleCalendarEvents} from "@/calendar-integration/google-calendar-utils";
|
||||
import {fetchMicrosoftCalendarEvents} from "@/calendar-integration/microsoft-calendar-utils";
|
||||
import {useCreateEventFromProvider} from "@/hooks/firebase/useCreateEvent";
|
||||
import {useCreateEventsFromProvider} from "@/hooks/firebase/useCreateEvent";
|
||||
import {useAuthContext} from "@/contexts/AuthContext";
|
||||
import {useUpdateUserData} from "@/hooks/firebase/useUpdateUserData";
|
||||
import debounce from "debounce";
|
||||
@ -60,7 +60,7 @@ const CalendarSettingsPage = (props: {
|
||||
profileData?.eventColor ?? colorMap.pink
|
||||
);
|
||||
|
||||
const {mutateAsync: createEventFromProvider} = useCreateEventFromProvider();
|
||||
const {mutateAsync: createEventsFromProvider} = useCreateEventsFromProvider();
|
||||
const {mutateAsync: updateUserData} = useUpdateUserData();
|
||||
|
||||
WebBrowser.maybeCompleteAuthSession();
|
||||
@ -70,41 +70,46 @@ const CalendarSettingsPage = (props: {
|
||||
signInWithGoogle();
|
||||
}, [response]);
|
||||
|
||||
const fetchAndSaveGoogleEvents = () => {
|
||||
console.log("fetch");
|
||||
const timeMin = new Date(new Date().setHours(0, 0, 0, 0));
|
||||
const timeMax = new Date(
|
||||
new Date(new Date().setHours(0, 0, 0, 0)).setDate(timeMin.getDate() + 30)
|
||||
);
|
||||
const fetchAndSaveGoogleEvents = async (token?: string, email?: string) => {
|
||||
console.log("Fetching Google Calendar events...");
|
||||
const timeMin = new Date(new Date().setFullYear(new Date().getFullYear() - 1));
|
||||
const timeMax = new Date(new Date().setFullYear(new Date().getFullYear() + 5));
|
||||
|
||||
console.log("Token: ", token ?? profileData?.googleToken)
|
||||
fetchGoogleCalendarEvents(
|
||||
profileData?.googleToken,
|
||||
token ?? profileData?.googleToken,
|
||||
email ?? profileData?.googleMail,
|
||||
timeMin.toISOString().slice(0, -5) + "Z",
|
||||
timeMax.toISOString().slice(0, -5) + "Z"
|
||||
).then((response) => {
|
||||
response?.forEach((item) => saveData(item));
|
||||
).then(async (response) => {
|
||||
console.log("Google Calendar events fetched:", response);
|
||||
const items = response?.map((item) => {
|
||||
if (item.allDay) {
|
||||
item.startDate = new Date(new Date(item.startDate).setHours(0, 0, 0, 0))
|
||||
item.endDate = item.startDate
|
||||
}
|
||||
return item;
|
||||
}) || [];
|
||||
await createEventsFromProvider(items);
|
||||
}).catch((error) => {
|
||||
console.error("Error fetching Google Calendar events:", error);
|
||||
});
|
||||
};
|
||||
|
||||
async function saveData(item: any) {
|
||||
await createEventFromProvider(item);
|
||||
}
|
||||
|
||||
const fetchAndSaveMicrosoftEvents = () => {
|
||||
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
|
||||
)
|
||||
);
|
||||
const fetchAndSaveMicrosoftEvents = async (token?: string, email?: string) => {
|
||||
const timeMin = new Date(new Date().setFullYear(new Date().getFullYear() - 1));
|
||||
const timeMax = new Date(new Date().setFullYear(new Date().getFullYear() + 3));
|
||||
|
||||
console.log("Token: ", token ?? profileData?.microsoftToken)
|
||||
fetchMicrosoftCalendarEvents(
|
||||
profileData?.microsoftToken,
|
||||
startDateTime.toISOString().slice(0, -5) + "Z",
|
||||
endDateTime.toISOString().slice(0, -5) + "Z"
|
||||
).then((response) => {
|
||||
token ?? profileData?.microsoftToken,
|
||||
email ?? profileData?.outlookMail,
|
||||
timeMin.toISOString().slice(0, -5) + "Z",
|
||||
timeMax.toISOString().slice(0, -5) + "Z"
|
||||
).then(async (response) => {
|
||||
console.log(response);
|
||||
response?.forEach((item) => saveData(item));
|
||||
const items = response ?? [];
|
||||
await createEventsFromProvider(items);
|
||||
});
|
||||
};
|
||||
|
||||
@ -126,6 +131,8 @@ const CalendarSettingsPage = (props: {
|
||||
await updateUserData({
|
||||
newUserData: {googleToken: accessToken, googleMail: googleMail},
|
||||
});
|
||||
|
||||
await fetchAndSaveGoogleEvents(accessToken, googleMail)
|
||||
}
|
||||
} catch (error) {
|
||||
console.error("Error during Google sign-in:", error);
|
||||
@ -207,6 +214,7 @@ const CalendarSettingsPage = (props: {
|
||||
newUserData: {microsoftToken: tokenData.access_token, outlookMail: outlookMail},
|
||||
});
|
||||
|
||||
await fetchAndSaveMicrosoftEvents(tokenData.access_token, outlookMail)
|
||||
console.log("User data updated successfully.");
|
||||
}
|
||||
}
|
||||
@ -257,7 +265,7 @@ const CalendarSettingsPage = (props: {
|
||||
|
||||
return (
|
||||
<ScrollView>
|
||||
<View marginH-30>
|
||||
<View marginH-30 marginB-30>
|
||||
<TouchableOpacity onPress={() => props.setSelectedPage(0)}>
|
||||
<View row marginT-20 marginB-35 centerV>
|
||||
<Ionicons
|
||||
@ -412,9 +420,10 @@ const CalendarSettingsPage = (props: {
|
||||
<View style={{marginTop: 20}}>
|
||||
{!!profileData?.googleMail && (
|
||||
<Button
|
||||
onPress={fetchAndSaveGoogleEvents}
|
||||
onPress={() => fetchAndSaveGoogleEvents()}
|
||||
label={`Sync ${profileData?.googleMail}`}
|
||||
labelStyle={styles.addCalLbl}
|
||||
labelProps={{numberOfLines: 2}}
|
||||
iconSource={() => (
|
||||
<View marginR-15>
|
||||
<GoogleIcon/>
|
||||
@ -428,9 +437,10 @@ const CalendarSettingsPage = (props: {
|
||||
|
||||
{!!profileData?.outlookMail && (
|
||||
<Button
|
||||
onPress={fetchAndSaveMicrosoftEvents}
|
||||
onPress={() => fetchAndSaveMicrosoftEvents()}
|
||||
label={`Sync ${profileData?.outlookMail}`}
|
||||
labelStyle={styles.addCalLbl}
|
||||
labelProps={{numberOfLines: 2}}
|
||||
iconSource={() => (
|
||||
<View marginR-15>
|
||||
<OutlookIcon/>
|
||||
|
@ -11,7 +11,6 @@ export const useCreateEvent = () => {
|
||||
mutationKey: ["createEvent"],
|
||||
mutationFn: async (eventData: Partial<EventData>) => {
|
||||
try {
|
||||
console.log("CALLLLL")
|
||||
await firestore()
|
||||
.collection("Events")
|
||||
.add({...eventData, creatorId: currentUser?.uid, familyId: profileData?.familyId})
|
||||
@ -25,14 +24,17 @@ export const useCreateEvent = () => {
|
||||
})
|
||||
}
|
||||
|
||||
export const useCreateEventFromProvider = () => {
|
||||
const {user: currentUser} = useAuthContext()
|
||||
const queryClients = useQueryClient()
|
||||
export const useCreateEventsFromProvider = () => {
|
||||
const {user: currentUser} = useAuthContext();
|
||||
const queryClient = useQueryClient();
|
||||
|
||||
return useMutation({
|
||||
mutationKey: ["createEventFromProvider"],
|
||||
mutationFn: async (eventData: Partial<EventData>) => {
|
||||
mutationKey: ["createEventsFromProvider"],
|
||||
mutationFn: async (eventDataArray: Partial<EventData>[]) => {
|
||||
try {
|
||||
for (const eventData of eventDataArray) {
|
||||
console.log("Processing EventData: ", eventData);
|
||||
|
||||
const snapshot = await firestore()
|
||||
.collection("Events")
|
||||
.where("id", "==", eventData.id)
|
||||
@ -41,21 +43,22 @@ export const useCreateEventFromProvider = () => {
|
||||
if (snapshot.empty) {
|
||||
await firestore()
|
||||
.collection("Events")
|
||||
.add({...eventData, creatorId: currentUser?.uid})
|
||||
.add({...eventData, creatorId: currentUser?.uid});
|
||||
} else {
|
||||
console.log("ENTER HERE")
|
||||
console.log("Event already exists, updating...");
|
||||
const docId = snapshot.docs[0].id;
|
||||
await firestore()
|
||||
.collection("Events")
|
||||
.doc(docId)
|
||||
.update({...eventData, creatorId: currentUser?.uid});
|
||||
.set({...eventData, creatorId: currentUser?.uid}, {merge: true});
|
||||
}
|
||||
}
|
||||
} catch (e) {
|
||||
console.error(e)
|
||||
console.error("Error creating/updating events: ", e);
|
||||
}
|
||||
},
|
||||
onSuccess: () => {
|
||||
queryClients.invalidateQueries("events")
|
||||
}
|
||||
})
|
||||
queryClient.invalidateQueries("events");
|
||||
}
|
||||
});
|
||||
};
|
Reference in New Issue
Block a user