();
const takeMutation = useTakeUndoableMutation();
const { data, refetch } = useGetOne('posts', { id: 1 });
const [update, { isPending }] = useUpdate();
const handleClick = () => {
update(
'posts',
{
id: 1,
data: { title: 'Hello World' },
},
{
mutationMode: 'undoable',
onSuccess: () => setSuccess('success'),
}
);
setNotification(true);
};
return (
<>
- 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 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 [notification, setNotification] = useState(false);
const [success, setSuccess] = useState();
const [error, setError] = useState();
const takeMutation = useTakeUndoableMutation();
const { data, refetch } = useGetOne('posts', { id: 1 });
const [update, { isPending }] = useUpdate();
const handleClick = () => {
update(
'posts',
{
id: 1,
data: { title: 'Hello World' },
},
{
mutationMode: 'undoable',
onSuccess: () => setSuccess('success'),
onError: e => {
setError(e);
setSuccess('');
},
}
);
setNotification(true);
};
return (
<>
- 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 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 WithMiddlewaresCore = () => {
const isMutating = useIsMutating();
const [notification, setNotification] = useState(false);
const [success, setSuccess] = useState();
const takeMutation = useTakeUndoableMutation();
const { data, refetch } = useGetOne('posts', { id: 1 });
const [update, { isPending }] = useUpdate(
'posts',
{
id: 1,
data: { title: 'Hello World' },
},
{
mutationMode: 'undoable',
// @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'),
}
);
setNotification(true);
};
return (
<>
- 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 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 [notification, setNotification] = useState(false);
const [success, setSuccess] = useState();
const [error, setError] = useState();
const takeMutation = useTakeUndoableMutation();
const { data, refetch } = useGetOne('posts', { id: 1 });
const [update, { isPending }] = useUpdate(
'posts',
{
id: 1,
data: { title: 'Hello World' },
},
{
mutationMode: 'undoable',
// @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'),
onError: e => {
setError(e);
setSuccess('');
},
}
);
setNotification(true);
};
return (
<>
- title
- {data?.title}
- author
- {data?.author}
{notification ? (
<>
>
) : (
)}
{success && {success}
}
{error && {error.message}
}
{isMutating !== 0 && mutating
}
>
);
};