Skip to content

Commit d2cdc1b

Browse files
committed
feat(redux-saga): typings for request
1 parent 28f4d7a commit d2cdc1b

6 files changed

Lines changed: 29 additions & 22 deletions

File tree

src/app/containers/Stars.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import * as React from "react";
22
import {IStars, IStarsAction} from "../models/starsModel";
3-
import {STARS_REQUEST} from "../redux/modules/starsModule";
3+
import {actionType} from "../redux/modules/starsModule";
44
const {connect} = require("react-redux");
55

66
interface IProps {
@@ -11,7 +11,7 @@ interface IProps {
1111
@connect(
1212
(state) => ({stars: state.stars}),
1313
(dispatch) => ({
14-
getStars: () => dispatch({type: STARS_REQUEST})
14+
getStars: () => dispatch({type: actionType.REQUEST})
1515
})
1616
)
1717
class Stars extends React.Component<IProps, {}> {

src/app/helpers/promiseReducer.ts

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,24 @@
11
import IBaseAction from "../models/IBaseAction";
22

3-
export default function promiseReducer<TState, TAction>(baseAction: string, state: TState, action: IBaseAction & TAction): TState {
3+
export interface IRequestType {
4+
REQUEST: string;
5+
SUCCESS: string;
6+
FAILURE: string;
7+
}
8+
9+
export default function promiseReducer<TState, TAction>(actionType: IRequestType, state: TState, action: IBaseAction & TAction): TState {
410
switch (action.type) {
5-
case baseAction + "_REQUEST":
11+
case actionType.REQUEST:
612
return Object.assign({}, state, {
713
payload: null
814
});
915

10-
case baseAction + "_SUCCESS":
16+
case actionType.SUCCESS:
1117
return Object.assign({}, state, {
1218
payload: action.payload
1319
});
1420

15-
case baseAction + "_FAILURE":
21+
case actionType.FAILURE:
1622
return Object.assign({}, state, {
1723
error: true,
1824
message: action.message

src/app/redux/modules/starsModule.test.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
import {STARS_FAILURE, STARS_REQUEST, STARS_SUCCESS, starsReducer} from "./starsModule";
1+
import {actionType, starsReducer} from "./starsModule";
22

33
describe("Stars Reducer", () => {
44

55
it("handles action of type STARS_REQUEST", () => {
6-
const action = { type: STARS_REQUEST };
6+
const action = { type: actionType.REQUEST };
77
const stateBefore = {};
88
const stateAfter = { payload: null };
99
expect(starsReducer(stateBefore, action)).toEqual(stateAfter);
@@ -14,7 +14,7 @@ describe("Stars Reducer", () => {
1414
payload: {
1515
stargazers_count: 99
1616
},
17-
type: STARS_SUCCESS
17+
type: actionType.SUCCESS
1818
};
1919
const stateBefore = {};
2020
const stateAfter = {
@@ -26,7 +26,7 @@ describe("Stars Reducer", () => {
2626
});
2727

2828
it("handles action of type STARS_FAILURE", () => {
29-
const action = { type: STARS_FAILURE };
29+
const action = { type: actionType.FAILURE };
3030
const stateBefore = {};
3131
const stateAfter = { error: true};
3232
expect(starsReducer(stateBefore, action)).toEqual(stateAfter);
Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
import {IStars, IStarsAction} from "models/starsModel";
2-
import promiseReducer from "../../helpers/promiseReducer";
2+
import promiseReducer, {IRequestType} from "../../helpers/promiseReducer";
33

4-
/** Action Types */
5-
export const STARS_REQUEST: string = "stars/STARS_REQUEST";
6-
export const STARS_SUCCESS: string = "stars/STARS_SUCCESS";
7-
export const STARS_FAILURE: string = "stars/STARS_FAILURE";
4+
export const actionType: IRequestType = {
5+
FAILURE: "STARS_FAILURE",
6+
REQUEST: "STARS_REQUEST",
7+
SUCCESS: "STARS_SUCCESS"
8+
};
89

910
/** Initial State */
1011
const initialState: IStars = {
@@ -13,5 +14,5 @@ const initialState: IStars = {
1314

1415
/** Reducer */
1516
export function starsReducer(state: IStars = initialState, action: IStarsAction): IStars {
16-
return promiseReducer<IStars, IStarsAction>("stars/STARS", state, action);
17+
return promiseReducer<IStars, IStarsAction>(actionType, state, action);
1718
}

src/app/sagas/starsSaga.test.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import {call, takeLatest} from "redux-saga/effects";
2-
import {STARS_FAILURE, STARS_REQUEST, STARS_SUCCESS} from "../redux/modules/starsModule";
2+
import {actionType} from "../redux/modules/starsModule";
33
import makeRequest from "./makeRequest";
44
import {fetchStars, watchStarsLoad} from "./starsSaga";
55
import TestApi from "./TestApi";
@@ -10,7 +10,7 @@ describe("starsSaga", () => {
1010

1111
it("must call makeRequest of api.getStars", () => {
1212
const api = new TestApi();
13-
expect(gen.next().value).toEqual(call(makeRequest, api.getStars, STARS_SUCCESS, STARS_FAILURE));
13+
expect(gen.next().value).toEqual(call(makeRequest, api.getStars, actionType.SUCCESS, actionType.FAILURE));
1414
});
1515

1616
it("must be done", () => {
@@ -22,7 +22,7 @@ describe("starsSaga", () => {
2222
const gen = watchStarsLoad();
2323

2424
it("must call takeLatest of STARS_REQUEST", () => {
25-
expect(gen.next().value).toEqual(takeLatest(STARS_REQUEST, fetchStars));
25+
expect(gen.next().value).toEqual(takeLatest(actionType.REQUEST, fetchStars));
2626
});
2727

2828
it("must be done", () => {

src/app/sagas/starsSaga.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
import {call, takeLatest} from "redux-saga/effects";
2-
import {STARS_FAILURE, STARS_REQUEST, STARS_SUCCESS} from "../redux/modules/starsModule";
2+
import {actionType} from "../redux/modules/starsModule";
33
import makeRequest from "./makeRequest";
44
import TestApi from "./TestApi";
55

66
export function* fetchStars(): any {
77
const api = new TestApi();
8-
yield call(makeRequest, api.getStars, STARS_SUCCESS, STARS_FAILURE);
8+
yield call(makeRequest, api.getStars, actionType.SUCCESS, actionType.FAILURE);
99
}
1010

1111
export function* watchStarsLoad(): any {
12-
yield takeLatest(STARS_REQUEST, fetchStars);
12+
yield takeLatest(actionType.REQUEST, fetchStars);
1313
}

0 commit comments

Comments
 (0)