import * as React from 'react'; import { useState } from 'react'; import { QueryClient, useIsMutating } from '@tanstack/react-query'; import { CoreAdminContext } from '../core'; import { useUpdate } from './useUpdate'; import { useGetOne } from './useGetOne'; export default { title: 'ra-core/dataProvider/useUpdate/optimistic' }; export const SuccessCase = ({ timeout = 1000 }) => { const posts = [{ id: 1, title: 'Hello', author: 'John Doe' }]; const dataProvider = { getOne: (resource, params) => { return Promise.resolve({ data: posts.find(p => p.id === params.id), }); }, update: (resource, params) => { return new Promise(resolve => { setTimeout(() => { const post = posts.find(p => p.id === params.id); if (post) { post.title = params.data.title; } resolve({ data: post }); }, timeout); }); }, } as any; return ( ); }; const SuccessCore = () => { const isMutating = useIsMutating(); const [success, setSuccess] = useState(); const { data, refetch } = useGetOne('posts', { id: 1 }); const [update, { isPending }] = useUpdate(); const handleClick = () => { update( 'posts', { id: 1, data: { title: 'Hello World' }, }, { mutationMode: 'optimistic', onSuccess: () => setSuccess('success'), } ); }; return ( <>
title
{data?.title}
author
{data?.author}
 
{success &&
{success}
} {isMutating !== 0 &&
mutating
} ); }; export const UndefinedValues = () => { const data = { id: 1, title: 'Hello' }; const dataProvider = { getOne: async () => ({ data }), update: () => new Promise(() => {}), // never resolve to see only optimistic update } as any; return ( ); }; const UndefinedValuesCore = () => { const { data } = useGetOne('posts', { id: 1 }); const [update, { isPending }] = useUpdate(); const handleClick = () => { update( 'posts', { id: 1, data: { id: undefined, title: 'world' } }, { mutationMode: 'optimistic' } ); }; return ( <>
{JSON.stringify(data)}
); }; export const ErrorCase = ({ timeout = 1000 }) => { const posts = [{ id: 1, title: 'Hello', author: 'John Doe' }]; const dataProvider = { getOne: (resource, params) => { return Promise.resolve({ data: posts.find(p => p.id === params.id), }); }, update: () => { return new Promise((resolve, reject) => { setTimeout(() => { reject(new Error('something went wrong')); }, timeout); }); }, } as any; return ( ); }; const ErrorCore = () => { const isMutating = useIsMutating(); const [success, setSuccess] = useState(); const [error, setError] = useState(); const { data, refetch } = useGetOne('posts', { id: 1 }); const [update, { isPending }] = useUpdate(); const handleClick = () => { update( 'posts', { id: 1, data: { title: 'Hello World' }, }, { mutationMode: 'optimistic', onSuccess: () => setSuccess('success'), onError: e => { setError(e); setSuccess(''); }, } ); }; return ( <>
title
{data?.title}
author
{data?.author}
 
{success &&
{success}
} {error &&
{error.message}
} {isMutating !== 0 &&
mutating
} ); }; export const WithMiddlewaresSuccess = ({ timeout = 1000 }) => { const posts = [{ id: 1, title: 'Hello', author: 'John Doe' }]; const dataProvider = { getOne: (resource, params) => { return Promise.resolve({ data: posts.find(p => p.id === params.id), }); }, update: (resource, params) => { return new Promise(resolve => { setTimeout(() => { const post = posts.find(p => p.id === params.id); if (post) { post.title = params.data.title; } resolve({ data: post }); }, timeout); }); }, } as any; return ( ); }; const WithMiddlewaresSuccessCore = () => { const isMutating = useIsMutating(); const [success, setSuccess] = useState(); const { data, refetch } = useGetOne('posts', { id: 1 }); const [update, { isPending }] = useUpdate( 'posts', { id: 1, data: { title: 'Hello World' }, }, { mutationMode: 'optimistic', // @ts-ignore getMutateWithMiddlewares: mutate => async (resource, params) => { return mutate(resource, { ...params, data: { title: `${params.data.title} from middleware` }, }); }, } ); const handleClick = () => { update( 'posts', { id: 1, data: { title: 'Hello World' }, }, { onSuccess: () => setSuccess('success'), } ); }; return ( <>
title
{data?.title}
author
{data?.author}
 
{success &&
{success}
} {isMutating !== 0 &&
mutating
} ); }; export const WithMiddlewaresError = ({ timeout = 1000 }) => { const posts = [{ id: 1, title: 'Hello', author: 'John Doe' }]; const dataProvider = { getOne: (resource, params) => { return Promise.resolve({ data: posts.find(p => p.id === params.id), }); }, update: () => { return new Promise((resolve, reject) => { setTimeout(() => { reject(new Error('something went wrong')); }, timeout); }); }, } as any; return ( ); }; const WithMiddlewaresErrorCore = () => { const isMutating = useIsMutating(); const [success, setSuccess] = useState(); const [error, setError] = useState(); const { data, refetch } = useGetOne('posts', { id: 1 }); const [update, { isPending }] = useUpdate( 'posts', { id: 1, data: { title: 'Hello World' }, }, { mutationMode: 'optimistic', // @ts-ignore mutateWithMiddlewares: mutate => async (resource, params) => { return mutate(resource, { ...params, data: { title: `${params.data.title} from middleware` }, }); }, } ); const handleClick = () => { setError(undefined); update( 'posts', { id: 1, data: { title: 'Hello World' }, }, { onSuccess: () => setSuccess('success'), onError: e => { setError(e); setSuccess(''); }, } ); }; return ( <>
title
{data?.title}
author
{data?.author}
 
{error &&
{error.message}
} {success &&
{success}
} {isMutating !== 0 &&
mutating
} ); };