Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

create a container for Api integration for recognition listing #167

Open
wants to merge 35 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
35 commits
Select commit Hold shift + click to select a range
4b4be35
create reducer for store recognition list
jitendrasbunde May 26, 2020
1ee0089
update recognition Reducer
jitendrasbunde May 26, 2020
5464f6e
add recognitionReducer in root combineReducers
jitendrasbunde May 26, 2020
709c5df
create recognitionSaga file for run sagaMiddleware for Api call
jitendrasbunde May 26, 2020
8bc9d70
add code in redux-store for run recognitionSaga at sagaMiddleware
jitendrasbunde May 26, 2020
b23825f
add RecognitionListContainer and add store.dispatch code for saga Api…
jitendrasbunde May 26, 2020
41e4847
change the position to use of recognitionSaga middleware
jitendrasbunde May 27, 2020
e86e4d1
add some error related rendering code
jitendrasbunde May 27, 2020
3cf2b24
restructure default state of recognition reducer
jitendrasbunde May 27, 2020
ede91cd
adjust code as per default state of recognition reducer
jitendrasbunde May 27, 2020
da84186
add SessionTimeoutComponent and UnauthorisedErrorComponent for render…
jitendrasbunde May 27, 2020
6910d53
add test case file for recognitionReducer
jitendrasbunde May 27, 2020
1a840db
add one test case in recognitionReducer.test.js file action at failure
jitendrasbunde May 27, 2020
2e30634
add test case file for recognitionSaga middleware
jitendrasbunde May 28, 2020
b4cc2c0
rename and recogntionReducer file name and use actionGenerator utils …
jitendrasbunde May 28, 2020
a8b1c22
update test case of listRecognition reducer and use actionGenerator u…
jitendrasbunde May 28, 2020
1db2012
change the name of recognitionReducer to listRecognitionReducer in ro…
jitendrasbunde May 28, 2020
8138af0
update recognitionSaga by use utils actionGenerator function
jitendrasbunde May 28, 2020
a57181d
update RecognitionContainer because of change file name of recognitio…
jitendrasbunde May 28, 2020
6610c83
add some test case for recognitionSaga
jitendrasbunde May 28, 2020
ccdd298
update test case message as per action possiblity
jitendrasbunde May 28, 2020
0584c66
update test case of recognitionSaga test.js
jitendrasbunde May 29, 2020
100381c
adjust proper indentation in recognitionSaga and listRecognitionReduc…
jitendrasbunde May 29, 2020
7e1bcf4
add recognition saga test cases for success full get list call
jitendrasbunde Jun 10, 2020
8ecf64c
add mock response files for get list api call
jitendrasbunde Jun 10, 2020
fe600d2
update defaulte state of reducer as per get list api response and upd…
jitendrasbunde Jun 10, 2020
e8ef2df
add action file for list recognition container for provide action object
jitendrasbunde Jun 11, 2020
5b788d6
update recognition list container and get action object from action/l…
jitendrasbunde Jun 11, 2020
c5dc7d3
use actionGenerator in saga and update there test cases
jitendrasbunde Jun 11, 2020
c3a4e2a
use actionObjectGenerator function for action object in recognition r…
jitendrasbunde Jun 11, 2020
f3361f5
create actionConstants.js file for add actions names
jitendrasbunde Jun 11, 2020
71e1197
use the action name into recognition saga and test case file and also…
jitendrasbunde Jun 11, 2020
a840e5b
use actionObjectGentrator in saga and there test cases
jitendrasbunde Jun 11, 2020
edbc0e9
update success mockResponse json file
jitendrasbunde Jun 11, 2020
949a943
correct some code before ready to review
jitendrasbunde Jun 11, 2020
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"error": {
"code": "invalid_token",
"message": "unauthorized user"
}
}
54 changes: 54 additions & 0 deletions mock-responses/recognitions/get_api_call_success_response.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
{
"data": [
{
"id": 1,
"core_value_id": 2,
"text": "good work",
"given_for": 4,
"given_by": 6,
"given_at": "1588924868",
"givenFor": {
"id": 4,
"first_name": "Avinash",
"last_name": "mane",
"profile_image_url": null
},
"givenBy": {
"id": 6,
"first_name": "Avinash",
"last_name": "mane",
"profile_image_url": null
},
"core_value": {
"id": 2,
"text": "good work",
"description": "good working in peerly"
}
},
{
"id": 2,
"core_value_id": 2,
"text": "good work",
"given_for": 4,
"given_by": 6,
"given_at": "1588924868",
"givenFor": {
"id": 4,
"first_name": "Avinash",
"last_name": "mane",
"profile_image_url": null
},
"givenBy": {
"id": 6,
"first_name": "Avinash",
"last_name": "mane",
"profile_image_url": null
},
"core_value": {
"id": 2,
"text": "good work",
"description": "good working in peerly"
}
}
]
}
6 changes: 6 additions & 0 deletions react-frontend/src/actions/listRecognitionAction.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
export default (type, payload) => {
return {
type: type,
payload: payload,
};
};
3 changes: 3 additions & 0 deletions react-frontend/src/constants/actionConstants.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export const LIST_RECOGNITION = "LIST_RECOGNITION";

export const LIST_RECOGNITION_API = "LIST_RECOGNITION_API";
34 changes: 34 additions & 0 deletions react-frontend/src/recognition-list/RecognitionListContainer.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import React, { useEffect } from "react";
import { useSelector, useDispatch } from "react-redux";

import SessionTimeoutComponent from "shared-components/SessionTimeoutComponent";
import UnauthorisedErrorComponent from "shared-components/UnauthorisedErrorComponent";
import actionObjectGenrator from "actions/listRecognitionAction";
import actionGenrator from "utils/actionGenerator";
import { LIST_RECOGNITION_API } from "constants/actionConstants";

const RecognnitionListContainer = () => {
const recognitionList = useSelector((state) => state.listRecognitionReducer);
const dispatch = useDispatch();
const status = actionGenrator(LIST_RECOGNITION_API);

useEffect(() => {
dispatch(actionObjectGenrator(status.success));
}, [dispatch, status.success]);

if (recognitionList.error.code === "invalid_token") {
return <SessionTimeoutComponent />;
} else if (recognitionList.error.code === "access_denied") {
return <UnauthorisedErrorComponent />;
}

return (
<div>
{recognitionList.list.map((el, key) => (
<h3 key={key}>{el.text}</h3>
))}
</div>
);
};

export default RecognnitionListContainer;
26 changes: 26 additions & 0 deletions react-frontend/src/reducers/listRecognitionReducer.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import actionGenerator from "utils/actionGenerator";

export const defaultState = {
list: [
{
core_value: {},
givenFor: {},
givenBy: {},
},
],
error: {
fields: {},
},
};
const status = actionGenerator("LIST_RECOGNITION");

export default (state = defaultState, action) => {
switch (action.type) {
case status.success:
return { ...state, list: action.payload };
case status.failure:
return { ...state, error: action.payload };
default:
return state;
}
};
52 changes: 52 additions & 0 deletions react-frontend/src/reducers/listRecognitionReducer.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import reducer, { defaultState } from "reducers/listRecognitionReducer";
import actionGenerator from "utils/actionGenerator";
import actionObjectGenerator from "actions/listRecognitionAction";

describe("list recognition reducer", () => {
const status = actionGenerator("LIST_RECOGNITION");

it("list recognition reducer should return the initial state", () => {
expect(reducer(defaultState, {})).toEqual({
list: [
{
core_value: {},
givenFor: {},
givenBy: {},
},
],
error: {
fields: {},
},
});
});

it("list recognition reducer should handle success action", () => {
let reducerData = reducer(
defaultState,
actionObjectGenerator(status.success, [{ id: 1 }])
);
expect(reducerData).toEqual({
list: [{ id: 1 }],
error: {
fields: {},
},
});
});

it("list recognition reducer should handle failure action", () => {
let reducerData = reducer(
defaultState,
actionObjectGenerator(status.failure, { code: "invalid token" })
);
expect(reducerData).toEqual({
list: [
{
core_value: {},
givenFor: {},
givenBy: {},
},
],
error: { code: "invalid token" },
});
});
});
2 changes: 2 additions & 0 deletions react-frontend/src/reducers/rootReducer.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import { combineReducers } from "redux";

import appReducer from "reducers/appReducer.js";
import listRecognitionReducer from "reducers/listRecognitionReducer";

export default combineReducers({
appReducer,
listRecognitionReducer,
});
36 changes: 36 additions & 0 deletions react-frontend/src/sagas/recognitionSaga.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import { put, takeEvery, spawn, call } from "redux-saga/effects";

import getJson from "utils/getJson";
import actionObjectGenerator from "actions/listRecognitionAction";
import actionGenerator from "utils/actionGenerator";
import {
LIST_RECOGNITION,
LIST_RECOGNITION_API,
} from "constants/actionConstants";

export function* getRecognitionList() {
const status = actionGenerator(LIST_RECOGNITION);
try {
const response = yield call(getJson, {
path: "recognitions",
apiToken: "",
});
const responseObj = yield response.json();
if (responseObj.data) {
yield put(actionObjectGenerator(status.success, responseObj.data));
} else {
yield put(actionObjectGenerator(status.failure, responseObj.error));
}
} catch (error) {
yield put(actionObjectGenerator(status.failure, error));
}
}

export function* recognitionApi() {
const status = actionGenerator(LIST_RECOGNITION_API);
yield takeEvery(status.success, getRecognitionList);
}

export default function* rootRecognitionSaga() {
yield spawn(recognitionApi);
}
60 changes: 60 additions & 0 deletions react-frontend/src/sagas/recognitionSaga.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import { takeEvery, call, put } from "redux-saga/effects";

import { recognitionApi, getRecognitionList } from "sagas/recognitionSaga";
import getJson from "utils/getJson";
import actionGenerator from "utils/actionGenerator";
import success_mockResponse from "../../../mock-responses/recognitions/get_api_call_success_response.json";
import failure_mockResponse from "../../../mock-responses/recognitions/get_api_call_failure_response.json";
import {
LIST_RECOGNITION_API,
LIST_RECOGNITION,
} from "constants/actionConstants.js";
import actionObjectGenerator from "actions/listRecognitionAction";

describe("RECOGNITION SAGAS", () => {
const status = actionGenerator(LIST_RECOGNITION);
const apiStatus = actionGenerator(LIST_RECOGNITION_API);
const response = { json: () => "success" };

it("should dispatch action 'LIST_RECOGNITION_API' for recognition saga", () => {
const generator = recognitionApi();
expect(generator.next().value).toEqual(
takeEvery(apiStatus.success, getRecognitionList)
);
const status = generator.next().done;
expect(status).toEqual(true);
});

it("should dispatch action 'LIST_RECOGNITION_API' for fetch list with 200 status", () => {
const generator = getRecognitionList();
const callFunctionDefination = generator.next().value;

expect(callFunctionDefination).toEqual(
call(getJson, {
path: "recognitions",
apiToken: "",
})
);
generator.next(response).value;
expect(generator.next(success_mockResponse).value).toEqual(
put(actionObjectGenerator(status.success, success_mockResponse.data))
);
expect(generator.next().done).toEqual(true);
});

it("should dispatch action 'LIST_RECOGNITION_API' for fetch list with 401 error response", () => {
const generator = getRecognitionList();
const callFunctionDefination = generator.next().value;
expect(callFunctionDefination).toEqual(
call(getJson, {
path: "recognitions",
apiToken: "",
})
);
expect(generator.next(response).value).toEqual("success");
expect(generator.next(failure_mockResponse).value).toEqual(
put(actionObjectGenerator(status.failure, failure_mockResponse.error))
);
expect(generator.next().done).toEqual(true);
});
});
2 changes: 2 additions & 0 deletions react-frontend/src/sagas/rootSaga.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { spawn } from "redux-saga/effects";
import recognitionSaga from "sagas/recognitionSaga";

export function* helloSaga() {
const msg = yield "Hello Sagas!";
Expand All @@ -8,4 +9,5 @@ export function* helloSaga() {

export default function* rootSaga() {
yield spawn(helloSaga);
yield spawn(recognitionSaga);
}