2024-05-31 16:19:51 +02:00
|
|
|
import React from "react";
|
2024-06-28 13:07:29 +02:00
|
|
|
import {SafeAreaView, View, Button, } from "react-native";
|
2024-05-31 16:19:51 +02:00
|
|
|
|
2024-06-28 13:07:29 +02:00
|
|
|
import NetInfo from "@react-native-community/netinfo";
|
|
|
|
import {useMutation, QueryClient, onlineManager} from "@tanstack/react-query";
|
|
|
|
import {PersistQueryClientProvider} from "@tanstack/react-query-persist-client";
|
|
|
|
import {createAsyncStoragePersister} from "@tanstack/query-async-storage-persister";
|
2024-05-31 16:19:51 +02:00
|
|
|
import AsyncStorage from "@react-native-async-storage/async-storage";
|
|
|
|
|
|
|
|
const queryClient = new QueryClient({
|
|
|
|
defaultOptions: {
|
|
|
|
queries: {
|
|
|
|
gcTime: 1000 * 60 * 60 * 24, // 24 hours
|
|
|
|
},
|
2024-05-31 16:57:13 +02:00
|
|
|
mutations: {
|
2024-06-28 13:07:29 +02:00
|
|
|
cacheTime: 1000 * 60 * 60 * 24 * 2, // 2 days
|
2024-05-31 16:57:13 +02:00
|
|
|
},
|
2024-05-31 16:19:51 +02:00
|
|
|
},
|
|
|
|
});
|
|
|
|
|
2024-05-31 17:15:02 +02:00
|
|
|
onlineManager.setEventListener((setOnline) => {
|
|
|
|
return NetInfo.addEventListener((state) => {
|
2024-06-28 13:07:29 +02:00
|
|
|
setOnline(!!state.isConnected);
|
2024-05-31 17:15:02 +02:00
|
|
|
if (state.isConnected)
|
2024-06-28 13:07:29 +02:00
|
|
|
queryClient.resumePausedMutations();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2024-05-31 17:15:02 +02:00
|
|
|
|
2024-05-31 16:19:51 +02:00
|
|
|
const asyncStoragePersister = createAsyncStoragePersister({
|
|
|
|
storage: AsyncStorage,
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
// Define the "addTodo" mutation
|
|
|
|
queryClient.setMutationDefaults(["addTodo"], {
|
|
|
|
mutationFn: ({ page }) => {
|
|
|
|
return makeCall(page);
|
|
|
|
},
|
2024-05-31 17:15:02 +02:00
|
|
|
retry: Infinity,
|
2024-06-28 13:07:29 +02:00
|
|
|
networkMode: "offline",
|
2024-05-31 16:19:51 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
function makeCall(page) {
|
2024-06-28 13:07:29 +02:00
|
|
|
console.log('mutations: ' + queryClient.getMutationCache().getAll().length);
|
|
|
|
console.log(queryClient.getMutationCache().getAll());
|
|
|
|
return fetch(`http://192.168.178.42:8080/${page}`)
|
2024-05-31 16:19:51 +02:00
|
|
|
.then(response => {
|
|
|
|
return response;
|
|
|
|
})
|
|
|
|
.catch(error => {
|
2024-06-28 13:07:29 +02:00
|
|
|
throw error;
|
2024-05-31 16:19:51 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function App() {
|
|
|
|
return (
|
|
|
|
<PersistQueryClientProvider
|
|
|
|
client={queryClient}
|
|
|
|
persistOptions={{ persister: asyncStoragePersister }}
|
2024-05-31 16:58:16 +02:00
|
|
|
onSuccess={() => {
|
2024-06-28 13:07:29 +02:00
|
|
|
// resume mutations after initial restore from localStorage was successful
|
|
|
|
queryClient.resumePausedMutations()
|
2024-05-31 16:58:16 +02:00
|
|
|
}}
|
2024-05-31 16:19:51 +02:00
|
|
|
>
|
|
|
|
<SafeAreaView>
|
|
|
|
<View>
|
|
|
|
<ReqButton />
|
|
|
|
</View>
|
|
|
|
</SafeAreaView>
|
|
|
|
</PersistQueryClientProvider>
|
|
|
|
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
const ReqButton = () => {
|
|
|
|
const mutation = useMutation({ mutationKey: ["addTodo"] });
|
2024-05-31 16:49:47 +02:00
|
|
|
|
2024-05-31 16:19:51 +02:00
|
|
|
return (
|
2024-05-31 16:49:47 +02:00
|
|
|
<>
|
|
|
|
<Button
|
|
|
|
title={"Request"}
|
|
|
|
onPress={() => mutation.mutate({ page: "a" })}
|
|
|
|
/>
|
2024-06-28 13:07:29 +02:00
|
|
|
<Button title={'Resume'} onPress={() => queryClient.resumePausedMutations()}/>
|
|
|
|
<Button title={'clear'} onPress={() => queryClient.getMutationCache().clear()}/>
|
2024-05-31 16:49:47 +02:00
|
|
|
</>
|
2024-05-31 16:19:51 +02:00
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
export default App;
|