Created
June 1, 2018 20:07
-
-
Save Ayyagaries/df33af4795c8eeeb1310a4773ab64390 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { call, put, select, take, fork, takeEvery } from "redux-saga/effects"; | |
import axios from "axios"; | |
import Config from "react-native-config"; | |
import _ from "lodash"; | |
import querystring from "query-string"; | |
import { | |
requestRepsCurrentlyCheckedIn, | |
requestNegativeEvents, | |
getRepsCurrentlyCheckedIn, | |
getNegativeEvents | |
} from "../../../actions/homescreen"; | |
import endpoints from "../../endpoints/endpoints"; | |
let REPS_CHECKEDIN_URL = Config.BASE_URL + endpoints.REPS_CHECKEDIN; | |
const FACILITY_NEGATIVE_EVENTS_URL = | |
Config.BASE_URL + endpoints.NEGATIVE_EVENTS_FOR_FACILITY; | |
export const getRepsCheckedIn = url => axios.get(url); | |
export const getFacilityVisits = (token, facilityId, url) => | |
axios.post( | |
url, | |
querystring.stringify({ | |
token, | |
facility_id: facilityId | |
}), | |
{ | |
"Content-Type": "application/x-www-form-urlencoded" | |
} | |
); | |
export function* homescreenCalls() { | |
while (true) { | |
const token = yield select(state => { | |
if (state.login.token !== null) { | |
return state.login.token.token; | |
} | |
return null; | |
}); | |
console.log(token); | |
yield take(requestRepsCurrentlyCheckedIn); | |
yield take(requestNegativeEvents); | |
const pickedFacilityId = yield select( | |
state => state.pickFacility.pickedFacilityID | |
); | |
REPS_CHECKEDIN_URL = _.replace(REPS_CHECKEDIN_URL, "<token>", token); | |
if (pickedFacilityId !== "") { | |
REPS_CHECKEDIN_URL = _.replace( | |
REPS_CHECKEDIN_URL, | |
"<facility_id>", | |
pickedFacilityId | |
); | |
} | |
try { | |
const responseRepscheckin = yield call( | |
getRepsCheckedIn, | |
REPS_CHECKEDIN_URL | |
); | |
const responsefacilityNegativeEvents = yield call( | |
getFacilityVisits, | |
token, | |
pickedFacilityId, | |
FACILITY_NEGATIVE_EVENTS_URL | |
); | |
if (responseRepscheckin.data.length > 0) { | |
yield put(getRepsCurrentlyCheckedIn(responseRepscheckin.data)); | |
} | |
if (responsefacilityNegativeEvents.data.length > 0) { | |
yield put(getNegativeEvents(responsefacilityNegativeEvents.data)); | |
} | |
REPS_CHECKEDIN_URL = _.replace( | |
REPS_CHECKEDIN_URL, | |
pickedFacilityId, | |
"<facility_id>" | |
); | |
REPS_CHECKEDIN_URL = _.replace(REPS_CHECKEDIN_URL, token, "<token>"); | |
console.log(`THE NEW URL IS ${REPS_CHECKEDIN_URL}`); | |
} catch (error) {} | |
} | |
} | |
export default function* homeScreenSaga() { | |
yield fork(homescreenCalls); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment