Skip to content

Instantly share code, notes, and snippets.

@Ayyagaries
Created May 16, 2018 16:38
Show Gist options
  • Save Ayyagaries/941542ef2759dad6914ba9b7c68eea81 to your computer and use it in GitHub Desktop.
Save Ayyagaries/941542ef2759dad6914ba9b7c68eea81 to your computer and use it in GitHub Desktop.
import { call, put, select, take, fork } 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,
RESPONSE_REPS_CURRENTLY_CHECKED_IN,
RESPONSE_NEGATIVE_EVENTS,
SHOWLOADING,
showLoading
} 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) {
yield take(showLoading);
yield put({
type: SHOWLOADING,
showLoading: true
});
yield take(requestRepsCurrentlyCheckedIn);
yield take(requestNegativeEvents);
const token = yield select(state => state.login.token);
const pickedFacilityId = yield select(
state => state.pickFacility.pickedFacilityID
);
console.log(`URL BEFORE REPLACE IS ${REPS_CHECKEDIN_URL}`);
console.log(`${token}------${pickedFacilityId}`);
REPS_CHECKEDIN_URL = _.replace(REPS_CHECKEDIN_URL, "<token>", token);
if (pickedFacilityId !== "") {
REPS_CHECKEDIN_URL = _.replace(
REPS_CHECKEDIN_URL,
"<facility_id>",
pickedFacilityId
);
}
console.log(`URL IS ${REPS_CHECKEDIN_URL}`);
console.log(
`URL FOR FACITY NEGATIVE EVENTS${FACILITY_NEGATIVE_EVENTS_URL}`
);
try {
const responseRepscheckin = yield call(
getRepsCheckedIn,
REPS_CHECKEDIN_URL
);
const responsefacilityNegativeEvents = yield call(
getFacilityVisits,
token,
pickedFacilityId,
FACILITY_NEGATIVE_EVENTS_URL
);
console.log("HERE IS THE RESPONSE FOR REPC CURRENLTY CHECKEDIN");
console.log(JSON.stringify(responseRepscheckin.data));
console.log("HERE IS THE RESPONSE FOR FACILITY_NEGATIVE_EVENTSURL");
console.log(responsefacilityNegativeEvents.data);
yield put({
type: RESPONSE_NEGATIVE_EVENTS,
negative_events_for_facility: responsefacilityNegativeEvents.data
});
yield put({
type: RESPONSE_REPS_CURRENTLY_CHECKED_IN,
reps_currently_checkedIn: responseRepscheckin.data
});
yield put({
type: SHOWLOADING,
showLoading: false
});
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