location.js 1.4 KB
import {all, call, fork, delay, put, takeEvery, takeLatest} from 'redux-saga/effects';
import axios from 'axios';
import {coordAPIKEY} from '../env';
import {
    SET_LOC_REQUEST,
    SET_LOC_SUCCESS,
    SET_LOC_FAILURE,
} from "../reducers/location";

function setLocationAPI(data) {
    const {startTextLocation, finishTextLocation} = data;
    console.log(startTextLocation, finishTextLocation);
    const startRes = axios.get(`http://api.vworld.kr/req/address?service=address&request=getcoord&version=2.0&crs=epsg:4326&address=${startTextLocation}&refine=true&simple=false&format=xml&type=road&key=${coordAPIKEY}`);
    const finishRes = axios.get(`http://api.vworld.kr/req/address?service=address&request=getcoord&version=2.0&crs=epsg:4326&address=${finishTextLocation}&refine=true&simple=false&format=xml&type=road&key=${coordAPIKEY}`);

    return {startRes, finishRes};
}


function* setLocation(action) {
    try {
        console.log('saga의 setLocation', action.data);
        const res = yield call(setLocationAPI, action.data);
        console.log(res.startRes, res.finishRes);
        const {result} = res.startRes;
        console.log(result);
    } catch (e) {
        console.error(e);
    }
};

function* watchSetLocation() {
    console.log('watchSetLocation');
    yield takeLatest(SET_LOC_REQUEST, setLocation);
}

export default function* locationSaga() {
    yield all([
        fork(watchSetLocation),
    ]);
};