export const currentTestApi = createApi({
reducerPath: 'currentTest',
baseQuery,
tagTypes: ['currentTest'],
endpoints: (builder) => ({
getCurrentTest: builder.query<Test, number | string | undefined>({
query: (id) => `test/${id}/edit`,
providesTags: ['currentTest'],
}),
}),
});
export const testsApi = createApi({
reducerPath: 'tests',
baseQuery,
tagTypes: ['tests'],
endpoints: (builder) => ({
toggleTestStatus: builder.mutation<void, {
id: number
test: Test,
}>({
query: ({ id, test }) => ({
url: `tests/${id}`,
method: 'PATCH',
body: { ...test },
}),
invalidatesTags: (_, error) => (error ? [] : ['tests']),
async onQueryStarted({ ...patch }, { dispatch, queryFulfilled }) {
const patchResult = dispatch(
currentTestApi.util.updateQueryData('getCurrentTest', patch.id, (draft) => {
Object.assign(draft, {
...draft,
title: 'lorem ipsum',
});
}),
);
try {
await queryFulfilled;
} catch {
patchResult.undo();
}
},
}),
}),
});