+
Place Order
+
+ {isError &&
Error: {error.message}
}
+ {data &&
Order ID: {data.id}
}
+
+ )
+}
+
+export default OrderComponent
diff --git a/src/queryClient.js b/src/queryClient.js
new file mode 100644
index 0000000..f47e2c9
--- /dev/null
+++ b/src/queryClient.js
@@ -0,0 +1,45 @@
+import { QueryClient, MutationCache } from '@tanstack/react-query'
+import { createSyncStoragePersister } from '@tanstack/query-sync-storage-persister'
+import {ToastAndroid} from 'react-native';
+
+// Create a persister using localStorrage
+const persister = createSyncStoragePersister({
+ storage: window.localStorage,
+})
+
+// Create a QueryClient instance
+const queryClient = new QueryClient({
+ defaultOptions: {
+ mutations: {
+ retry: true, // Retry indefinitely
+ },
+ },
+ mutationCache: new MutationCache({
+ onSuccess: (data) => {
+ ToastAndroid.show('Order placed successfully',ToastAndroid.SHORT)
+ },
+ onError: (error) => {
+ ToastAndroid.show('Order placement failed, retrying...',ToastAndroid.SHORT)
+ },
+ }),
+})
+
+// Set default mutation function to handle orders
+queryClient.setMutationDefaults(['placeOrder'], {
+ mutationFn: async (orderData) => {
+ const response = await fetch('https://jsonplaceholder.typicode.com/posts', {
+ method: 'POST',
+ headers: { 'Content-Type': 'application/json' },
+ body: JSON.stringify(orderData),
+ })
+
+ if (!response.ok) {
+ throw new Error('Order placement failed')
+ }
+
+ return response.json()
+ },
+ retry: true, // Retry indefinitely until it succeeds
+})
+
+export { persister, queryClient }
diff --git a/src/usePlaceOrder.js b/src/usePlaceOrder.js
new file mode 100644
index 0000000..dcae604
--- /dev/null
+++ b/src/usePlaceOrder.js
@@ -0,0 +1,5 @@
+import { useMutation } from '@tanstack/react-query'
+
+export function usePlaceOrder() {
+ return useMutation(['placeOrder'])
+}