Skip to content

Commit

Permalink
🐛 undo middleware was throwing an exception when ctx.response wasnt p…
Browse files Browse the repository at this point in the history
…resent
  • Loading branch information
neurosnap committed Jun 20, 2021
1 parent a864b74 commit 414544e
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 12 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
"dependencies": {
"redux-batched-actions": "^0.5.0",
"redux-saga-creator": "^2.0.1",
"robodux": "^11.0.2"
"robodux": "^11.0.3"
},
"ava": {
"extensions": [
Expand Down
43 changes: 42 additions & 1 deletion src/middleware.test.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import test from 'ava';
import createSagaMiddleware, { SagaIterator } from 'redux-saga';
import { takeLatest, put, select } from 'redux-saga/effects';
import { takeLatest, put, select, delay } from 'redux-saga/effects';
import {
createReducerMap,
MapEntity,
createTable,
createLoaderTable,
defaultLoadingItem,
} from 'robodux';

import { Next } from './types';
Expand All @@ -15,6 +16,9 @@ import {
queryCtx,
requestParser,
requestMonitor,
UndoCtx,
doIt,
undo,
} from './middleware';
import { FetchCtx } from './fetch';
import { setupStore } from './util';
Expand All @@ -24,6 +28,7 @@ import {
createQueryState,
selectLoaderById,
} from './slice';
import { undoer } from './';

interface User {
id: string;
Expand Down Expand Up @@ -283,3 +288,39 @@ test('overriding default loader behavior', (t) => {
},
});
});

test('undo', (t) => {
const users = createTable<User>({ name: 'users' });

const api = createApi<UndoCtx>();
api.use(requestMonitor());
api.use(api.routes());
api.use(requestParser());
api.use(undoer());

api.use(function* fetchApi(ctx, next) {
yield delay(500);
ctx.response = {
status: 200,
ok: true,
data: {
users: [mockUser],
},
};
yield next();
});

const createUser = api.post('/users', function* (ctx, next) {
ctx.undoable = true;
yield next();
});

const store = setupStore(api.saga());
store.dispatch(createUser());
store.dispatch(undo());
t.deepEqual(store.getState(), {
...createQueryState({
[LOADERS_NAME]: { [`${createUser}`]: defaultLoadingItem() },
}),
});
});
18 changes: 13 additions & 5 deletions src/middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import {
setLoaderStart,
setLoaderError,
setLoaderSuccess,
resetLoaderById,
addData,
} from './slice';

Expand Down Expand Up @@ -100,7 +101,7 @@ export function* dispatchActions<Ctx extends ApiCtx = ApiCtx>(
}

export function loadingMonitor<Ctx extends ApiCtx = ApiCtx>(
errorFn: (ctx: Ctx) => string = (ctx) => ctx.response.data.message,
errorFn: (ctx: Ctx) => string = (ctx) => ctx.response?.data?.message || '',
) {
return function* trackLoading(ctx: Ctx, next: Next) {
const id = ctx.name;
Expand All @@ -109,6 +110,11 @@ export function loadingMonitor<Ctx extends ApiCtx = ApiCtx>(

yield next();

if (typeof ctx.response.ok === 'undefined') {
ctx.actions.push(resetLoaderById(id));
return;
}

const payload = ctx.loader || {};
if (!ctx.response.ok) {
ctx.actions.push(
Expand All @@ -121,7 +127,7 @@ export function loadingMonitor<Ctx extends ApiCtx = ApiCtx>(
};
}

interface UndoCtx<R = any, P = any> extends ApiCtx {
export interface UndoCtx<R = any, P = any> extends ApiCtx {
undoable: boolean;
}

Expand Down Expand Up @@ -237,9 +243,11 @@ export function* simpleCache<Ctx extends ApiCtx = ApiCtx>(
}

export function requestParser<Ctx extends ApiCtx = ApiCtx>() {
return compose<Ctx>([queryCtx, urlParser, simpleCache]);
return compose<Ctx>([urlParser, simpleCache]);
}

export function requestMonitor<Ctx extends ApiCtx = ApiCtx>() {
return compose<Ctx>([dispatchActions, loadingMonitor()]);
export function requestMonitor<Ctx extends ApiCtx = ApiCtx>(
errorFn?: (ctx: Ctx) => string,
) {
return compose<Ctx>([queryCtx, dispatchActions, loadingMonitor(errorFn)]);
}
2 changes: 1 addition & 1 deletion src/slice.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ export const DATA_NAME = `@@saga-query/data`;
export const data = createTable<any>({ name: DATA_NAME });
export const { selectTable: selectData, selectById: selectDataById } =
data.getSelectors((s: any) => s[DATA_NAME] || {});
export const { add: addData } = data.actions;
export const { add: addData, reset: resetData } = data.actions;

export const reducers = createReducerMap(loaders, data);

Expand Down
8 changes: 4 additions & 4 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1866,10 +1866,10 @@ rimraf@^3.0.2:
dependencies:
glob "^7.1.3"

robodux@^11.0.2:
version "11.0.2"
resolved "https://registry.yarnpkg.com/robodux/-/robodux-11.0.2.tgz#91b4407a8881be102641f65a6038537adb963c16"
integrity sha512-bZYTlCCCz8CHeczBg13vqiz7pjnINp5PdoUMORLlndxXZzS6xVatrzLat+v0amFWp0eERs1k1yCg9lkQjRLZRQ==
robodux@^11.0.3:
version "11.0.3"
resolved "https://registry.yarnpkg.com/robodux/-/robodux-11.0.3.tgz#0ff6a8f0afa7e8499562f09b9f6dec9bd1ef5873"
integrity sha512-Xg91iUR0yos2QHNXvpN0H4IkZNptjeIyKknx6UY4Js4zkXwunb9eNXBVyrw9NTrKeIyb4jxFUODMmc2Y11NUrg==
dependencies:
immer "^8.0.1"

Expand Down

0 comments on commit 414544e

Please sign in to comment.