mirror of
https://github.com/urosran/cally.git
synced 2025-11-26 00:24:53 +00:00
Syncing improvement
This commit is contained in:
@ -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 = 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));
|
||||
|
||||
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
|
||||
)
|
||||
);
|
||||
|
||||
fetchMicrosoftCalendarEvents(
|
||||
profileData?.microsoftToken,
|
||||
startDateTime.toISOString().slice(0, -5) + "Z",
|
||||
endDateTime.toISOString().slice(0, -5) + "Z"
|
||||
).then((response) => {
|
||||
console.log("Token: ", token ?? profileData?.microsoftToken)
|
||||
fetchMicrosoftCalendarEvents(
|
||||
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
|
||||
@ -359,7 +367,7 @@ const CalendarSettingsPage = (props: {
|
||||
label={profileData?.googleToken ? `Disconnect ${profileData.googleMail}` : "Connect Google"}
|
||||
labelStyle={styles.addCalLbl}
|
||||
labelProps={{
|
||||
numberOfLines:2
|
||||
numberOfLines: 2
|
||||
}}
|
||||
iconSource={() => (
|
||||
<View marginR-15>
|
||||
@ -374,7 +382,7 @@ const CalendarSettingsPage = (props: {
|
||||
label="Connect Apple"
|
||||
labelStyle={styles.addCalLbl}
|
||||
labelProps={{
|
||||
numberOfLines:2
|
||||
numberOfLines: 2
|
||||
}}
|
||||
iconSource={() => (
|
||||
<View marginR-15>
|
||||
@ -390,7 +398,7 @@ const CalendarSettingsPage = (props: {
|
||||
label={profileData?.microsoftToken ? `Disconnect ${profileData.outlookMail}` : "Connect Outlook"}
|
||||
labelStyle={styles.addCalLbl}
|
||||
labelProps={{
|
||||
numberOfLines:2
|
||||
numberOfLines: 2
|
||||
}}
|
||||
iconSource={() => (
|
||||
<View marginR-15>
|
||||
@ -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/>
|
||||
|
||||
Reference in New Issue
Block a user