import * as React from 'react'; import { useState } from 'react'; import { QueryClient, useIsMutating } from '@tanstack/react-query'; import { CoreAdminContext } from '../core'; import { useTakeUndoableMutation } from './undo'; import { useCreate } from './useCreate'; import { useGetOne } from './useGetOne'; export default { title: 'ra-core/dataProvider/useCreate/undoable' }; export const SuccessCase = ({ timeout = 1000 }) => { const posts = [{ id: 1, title: 'Hello', author: 'John Doe' }]; const dataProvider = { getOne: (resource, params) => { return new Promise((resolve, reject) => { const data = posts.find(p => p.id === params.id); setTimeout(() => { if (!data) { reject(new Error('nothing yet')); } resolve({ data }); }, timeout); }); }, create: (resource, params) => { return new Promise(resolve => { setTimeout(() => { posts.push(params.data); resolve({ data: params.data }); }, timeout); }); }, } as any; return ( ); }; const SuccessCore = () => { const isMutating = useIsMutating(); const [notification, setNotification] = useState(false); const [success, setSuccess] = useState(); const takeMutation = useTakeUndoableMutation(); const { data, error, refetch } = useGetOne('posts', { id: 2 }); const [create, { isPending }] = useCreate(); const handleClick = () => { create( 'posts', { data: { id: 2, title: 'Hello World' }, }, { mutationMode: 'undoable', onSuccess: () => setSuccess('success'), } ); setNotification(true); }; return ( <> {error ? (

{error.message}

) : (
id
{data?.id}
title
{data?.title}
author
{data?.author}
)}
{notification ? ( <>   ) : ( )}  
{success &&
{success}
} {isMutating !== 0 &&
mutating
} ); }; export const ErrorCase = ({ timeout = 1000 }) => { const posts = [{ id: 1, title: 'Hello', author: 'John Doe' }]; const dataProvider = { getOne: (resource, params) => { return new Promise((resolve, reject) => { const data = posts.find(p => p.id === params.id); setTimeout(() => { if (!data) { reject(new Error('nothing yet')); } resolve({ data }); }, timeout); }); }, create: () => { return new Promise((resolve, reject) => { setTimeout(() => { reject(new Error('something went wrong')); }, timeout); }); }, } as any; return ( ); }; const ErrorCore = () => { const isMutating = useIsMutating(); const [notification, setNotification] = useState(false); const [success, setSuccess] = useState(); const [error, setError] = useState(); const takeMutation = useTakeUndoableMutation(); const { data, error: getOneError, refetch } = useGetOne('posts', { id: 2 }); const [create, { isPending }] = useCreate(); const handleClick = () => { create( 'posts', { data: { id: 2, title: 'Hello World' }, }, { mutationMode: 'undoable', onSuccess: () => setSuccess('success'), onError: e => { setError(e); setSuccess(''); }, } ); setNotification(true); }; return ( <> {getOneError ? (

{getOneError.message}

) : (
id
{data?.id}
title
{data?.title}
author
{data?.author}
)}
{notification ? ( <>   ) : ( )}  
{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 new Promise((resolve, reject) => { const data = posts.find(p => p.id === params.id); setTimeout(() => { if (!data) { reject(new Error('nothing yet')); } resolve({ data }); }, timeout); }); }, create: (resource, params) => { return new Promise(resolve => { setTimeout(() => { posts.push(params.data); resolve({ data: params.data }); }, timeout); }); }, } as any; return ( ); }; const WithMiddlewaresCore = () => { const isMutating = useIsMutating(); const [notification, setNotification] = useState(false); const [success, setSuccess] = useState(); const takeMutation = useTakeUndoableMutation(); const { data, error, refetch } = useGetOne('posts', { id: 2 }); const [create, { isPending }] = useCreate( 'posts', { data: { id: 2, title: 'Hello World' }, }, { mutationMode: 'undoable', // @ts-ignore getMutateWithMiddlewares: mutate => async (resource, params) => { return mutate(resource, { ...params, data: { ...params.data, title: `${params.data.title} from middleware`, }, }); }, } ); const handleClick = () => { create( 'posts', { data: { id: 2, title: 'Hello World' }, }, { onSuccess: () => setSuccess('success'), } ); setNotification(true); }; return ( <> {error ? (

{error.message}

) : (
id
{data?.id}
title
{data?.title}
author
{data?.author}
)}
{notification ? ( <>   ) : ( )}  
{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 new Promise((resolve, reject) => { const data = posts.find(p => p.id === params.id); setTimeout(() => { if (!data) { reject(new Error('nothing yet')); } resolve({ data }); }, timeout); }); }, create: () => { return new Promise((resolve, reject) => { setTimeout(() => { reject(new Error('something went wrong')); }, timeout); }); }, } as any; return ( ); }; const WithMiddlewaresErrorCore = () => { const isMutating = useIsMutating(); const [notification, setNotification] = useState(false); const [success, setSuccess] = useState(); const [error, setError] = useState(); const takeMutation = useTakeUndoableMutation(); const { data, error: getOneError, refetch } = useGetOne('posts', { id: 2 }); const [create, { isPending }] = useCreate( 'posts', { data: { id: 2, title: 'Hello World' }, }, { mutationMode: 'undoable', // @ts-ignore getMutateWithMiddlewares: mutate => async (resource, params) => { return mutate(resource, { ...params, data: { ...params.data, title: `${params.data.title} from middleware`, }, }); }, } ); const handleClick = () => { create( 'posts', { data: { id: 2, title: 'Hello World' }, }, { onSuccess: () => setSuccess('success'), onError: e => { setError(e); setSuccess(''); }, } ); setNotification(true); }; return ( <> {getOneError ? (

{getOneError.message}

) : (
id
{data?.id}
title
{data?.title}
author
{data?.author}
)}
{notification ? ( <>   ) : ( )}  
{success &&
{success}
} {error &&
{error.message}
} {isMutating !== 0 &&
mutating
} ); };