const isFirstRender = useRef(true);
useEffect(() => {
if (isFirstRender.current) {
isFirstRender.current = false;
return;
}
const runSomeBs = async () => {
if (isOffline) {
Toast.show({
type: "info",
text1: "Starting round",
text2: "You are now in offline mode",
});
onlineManager.setOnline(false);
} else {
Toast.show({
type: "info",
text1: "Ending round",
text2: "You are now in online mode. Data is syncing...",
});
console.log("CALL MUTATION STUFF");
onlineManager.setOnline(true);
console.log(queryClient.getMutationCache().getAll());
const what = await queryClient.resumePausedMutations();
console.log(what);
}
};
runSomeBs();
}, [isOffline]);
// const backgroundTask = async () => {
// if (networkContext.isOffline) {
// queryClient.resumePausedMutations();
// }
// };
// useEffect(() => {
// BackgroundFetch.configure(
// {
// minimumFetchInterval: 15,
// },
// async (taskId) => {
// console.log("[js] RNBackgroundFetch starting");
// await backgroundTask();
// BackgroundFetch.finish(taskId);
// },
// (error) => {
// console.log("[js] RNBackgroundFetch failed to start");
// }
// );
// return () => {
// BackgroundFetch.stop();
// };
// }, []);
useEffect(() => {
const loadAuthToken = async () => {
const authToken = await AsyncStorage.getItem("authToken");
const authTokenExpirationTime = await AsyncStorage.getItem(
"authTokenExpirationTime"
);
const currentTime = new Date().getTime();
if (
authToken &&
authTokenExpirationTime &&
currentTime < Number(authTokenExpirationTime)
) {
// If the auth token is not expired, set it in the auth context
authContext?.setAuthToken(authToken);
} else {
// If the auth token is expired, remove it from AsyncStorage
await AsyncStorage.removeItem("authToken");
await AsyncStorage.removeItem("authTokenExpirationTime");
}
};
loadAuthToken();
}, []);
const colorScheme = useColorScheme();
if (!authContext?.authToken) {
return (
);
}
const persister = createAsyncStoragePersister({
storage: AsyncStorage,
});
const queryClient = new QueryClient({
defaultOptions: {
queries: {
gcTime: 1000 * 60 * 60 * 24, // 24 hours
staleTime: 2000,
retry: 0,
},
mutations: {
networkMode: isOffline ? "offlineFirst" :
}
},
mutationCache: new MutationCache({}),
});
queryClient.setMutationDefaults(["createStationMeasurement"], {
mutationFn: async ({ options, url, authToken }) => {
return fetch(url, {
...options,
method: "POST",
headers: {
"Content-Type": "application/json",
Accept: "application/json",
Authorization: `${authToken}`,
},
body: JSON.stringify(options.body),
});
},
});
queryClient.setMutationDefaults(["createLogsheetComment"], {
mutationFn: async ({ options, url, authToken }) => {
return fetch(url, {
...options,
method: "POST",
headers: {
"Content-Type": "application/json",
Accept: "application/json",
Authorization: `${authToken}`,
},
body: JSON.stringify(options.body),
});
},
});
queryClient.setMutationDefaults(["createStationComment"], {
mutationFn: async ({ options, url, authToken }) => {
return fetch(url, {
...options,
method: "POST",
headers: {
"Content-Type": "application/json",
Accept: "application/json",
Authorization: `${authToken}`,
},
body: JSON.stringify(options.body),
});
},
});
return (
{
queryClient.resumePausedMutations().then(() => {
queryClient.invalidateQueries();
});
}}
>
);