Can't figure out how to get optimistic updates to work properly when I'm creating a new item rather than updating the existing one.
The optimistic updates do work but if I switch to Offline mode, the patchResult.undo() isn't removing the added element.
It would also be nice to have an example of deleting items optimistically
Here is my code
createMessagesItem: builder.mutation<void, Omit<IMessagesItem, '_id'>>({
query: (payload) => ({
url: `messages`,
method: 'POST',
body: payload,
}),
async onQueryStarted(patch, { dispatch, queryFulfilled }) {
const patchResult = dispatch(
storageApi.util.updateQueryData('getMessages', undefined, (draft) => {
draft.push({ ...patch, _id: '' });
})
);
try {
await queryFulfilled;
} catch {
patchResult.undo();
}
},
invalidatesTags: ['Messages'],
}),