This commit is contained in:
1 2024-05-24 17:16:54 +02:00
parent a2ec812ba7
commit e8e3cc70ab
5 changed files with 32 additions and 50 deletions

0
android/gradlew vendored Normal file → Executable file
View File

View File

@ -1,51 +1,26 @@
import React, { useState } from 'react' import React, {useState} from 'react';
import { usePlaceOrder } from './usePlaceOrder' import {ToastAndroid, Text, View, Button} from 'react-native';
import {ToastAndroid} from 'react-native'; import {useMutation} from '@tanstack/react-query';
function OrderComponent() { function OrderComponent() {
const { mutate, isLoading, isError, error, data } = usePlaceOrder() const mutation = useMutation(['placeOrder']);
const [orderData, setOrderData] = useState({ title: '', body: '', userId: 1 }) const [orderData, setOrderData] = useState({title: '', body: '', userId: 1});
const handlePlaceOrder = () => { const handlePlaceOrder = id => {
mutate(orderData) mutation.mutate({title: '', body: '', userId: id});
} };
return ( return (
<div> <View>
<h1>Place Order</h1> <Text>Place Order</Text>
<form <Button
onSubmit={(e) => { title="Make order"
e.preventDefault() onPress={handlePlaceOrder(Math.floor(Math.random() * 100))}
handlePlaceOrder() />
}} {mutation.isError && <Text>Error: {mutation.error.message}</Text>}
> {mutation.data && <Text>Order ID: {mutation.data.id}</Text>}
<div> </View>
<label> );
Title:
<input
type="text"
value={orderData.title}
onChange={(e) => setOrderData({ ...orderData, title: e.target.value })}
/>
</label>
</div>
<div>
<label>
Body:
<textarea
value={orderData.body}
onChange={(e) => setOrderData({ ...orderData, body: e.target.value })}
/>
</label>
</div>
<button type="submit" disabled={isLoading}>
{isLoading ? 'Placing Order...' : 'Place Order'}
</button>
</form>
{isError && <p>Error: {error.message}</p>}
{data && <p>Order ID: {data.id}</p>}
</div>
)
} }
export default OrderComponent export default OrderComponent;

View File

@ -1,10 +1,11 @@
import { QueryClient, MutationCache } from '@tanstack/react-query' import { QueryClient, MutationCache } from '@tanstack/react-query';
import { createSyncStoragePersister } from '@tanstack/query-sync-storage-persister' import { createAsyncStoragePersister } from '@tanstack/query-async-storage-persister';
import AsyncStorage from '@react-native-async-storage/async-storage';
import {ToastAndroid} from 'react-native'; import {ToastAndroid} from 'react-native';
// Create a persister using localStorrage // Create a persister using localStorrage
const persister = createSyncStoragePersister({ const persister = createAsyncStoragePersister({
storage: window.localStorage, storage: AsyncStorage,
}) })
// Create a QueryClient instance // Create a QueryClient instance
@ -13,6 +14,9 @@ const queryClient = new QueryClient({
mutations: { mutations: {
retry: true, // Retry indefinitely retry: true, // Retry indefinitely
}, },
queries: {
gcTime: 1000 * 60 * 60 * 24, // 24 hours
},
}, },
mutationCache: new MutationCache({ mutationCache: new MutationCache({
onSuccess: (data) => { onSuccess: (data) => {

View File

@ -1,5 +1,5 @@
import { useMutation } from '@tanstack/react-query' import { useMutation } from '@tanstack/react-query'
export function usePlaceOrder() { export function usePlaceOrder() {
return useMutation(['placeOrder']) return useMutation(['placeOrder']);
} }

View File

@ -1,3 +1,6 @@
{ {
"extends": "@react-native/typescript-config/tsconfig.json" "extends": "@react-native/typescript-config/tsconfig.json",
"compilerOptions": {
"allowJs": true
}
} }