location.js 4.98 KB
import {all, call, fork, delay, put, takeEvery, takeLatest} from 'redux-saga/effects';
import axios from 'axios';
import {coordAPIKEY, host} from '../env';


import {
    SET_ELOC_REQUEST,
    SET_SLOC_REQUEST,
    SET_SLOC_SUCCESS,
    SET_ELOC_SUCCESS,
    SET_SLOC_FAILURE,
    SET_ELOC_FAILURE,
    SET_OPTROUTE_REQUEST,
    SET_OPTROUTE_SUCCESS,
    SET_OPTROUTE_FAILURE,
} from "../reducers/location";

function setStartLocationAPI(data) {
    const {startTextLocation} = data;
    console.log(startTextLocation);
    return axios.get(`http://api.vworld.kr/req/address?service=address&request=getcoord&version=1.0&crs=epsg:4326&address=${startTextLocation}&refine=true&simple=false&format=json&type=road&key=${coordAPIKEY}`);
}

function setEndLocationAPI(data) {
    const {endTextLocation} = data;
    console.log(endTextLocation);
    return axios.get(`http://api.vworld.kr/req/address?service=address&request=getcoord&version=1.0&crs=epsg:4326&address=${endTextLocation}&refine=true&simple=false&format=json&type=road&key=${coordAPIKEY}`);
}

function setOptRouteAPI(data) {
    const {startLocation, endLocation, endTime, personalVelocity} = data;
    console.log('제발 좀 되라', startLocation, endLocation, endTime, personalVelocity);
    return axios.post(`http://${host}:4001/api/setOptRoute`, {
        startLocation,
        endLocation,
        endTime,
        personalVelocity
    }, {withCredentials: true});
}

function* setStartLocation(action) {
    try {
        console.log('saga의 setLocation', action.data);
        let res = yield call(setStartLocationAPI, action.data);
        let longitude, latitude = null;

        if (res.data.response.status === "OK") {
            longitude = parseFloat(res.data.response.result.point.x);
            latitude = parseFloat(res.data.response.result.point.y);
        }
        //
        // if (res.data.status === "OK") {
        //     latitude = res.data.results[0].geometry.location.lat;
        //     longitude = res.data.results[0].geometry.location.lng;
        //     console.log(latitude, longitude)
        // }

        console.log('startRes: ', longitude, latitude);

        yield put({
            type: SET_SLOC_SUCCESS,
            data: {
                startLocation: {
                    title: action.data.startTextLocation,
                    description: 'start point',
                    longitude: longitude,
                    latitude: latitude,
                    latitudeDelta: 1.2,
                    longitudeDelta: 1.2

                }
            }
        })
    } catch (e) {
        console.error(e);
        yield put({
            type: SET_SLOC_FAILURE,
            data: {
                info: e.response.data.info
            }
        });
    }
}

function* setEndLocation(action) {
    try {
        let res = yield call(setEndLocationAPI, action.data);
        let longitude, latitude = null;

        //
        // if (res.data.status === "OK") {
        //     latitude = res.data.results[0].geometry.location.lat;
        //     longitude = res.data.results[0].geometry.location.lng;
        //     console.log(latitude, longitude)
        // }

        if (res.data.response.status === "OK") {
            longitude = parseFloat(res.data.response.result.point.x);
            latitude = parseFloat(res.data.response.result.point.y);
        }

        console.log('finishRes: ', longitude, latitude);

        yield put({
            type: SET_ELOC_SUCCESS,
            data: {
                endLocation: {
                    title: action.data.endTextLocation,
                    description: 'end point',
                    longitude: longitude,
                    latitude: latitude,
                    latitudeDelta: 1.2,
                    longitudeDelta: 1.2
                }
            }
        });
    } catch (e) {
        console.error(e);
        yield put({
            type: SET_ELOC_FAILURE,
            data: {
                info: e.response.data.info
            }
        })

    }
}

function* setOptRoute(action) {
    try {
        let res = yield call(setOptRouteAPI, action.data);
        const {optRoute} = res.data;
        yield put({
            type: SET_OPTROUTE_SUCCESS,
            data: {
                optRoute: optRoute
            }
        });

    } catch (e) {
        console.error(e);
        yield put({
            type: SET_OPTROUTE_FAILURE,
            data: {
                info: e.response.data.info
            }
        })
    }
}


function* watchSetStartLocation() {
    console.log('watchSetStartLocation');
    yield takeLatest(SET_SLOC_REQUEST, setStartLocation);
}

function* watchSetEndLocation() {
    console.log('watchSetEndLocation');
    yield takeLatest(SET_ELOC_REQUEST, setEndLocation)
}

function* watchSetOptRoute() {
    console.log('watchSetOptimalRoute');
    yield takeLatest(SET_OPTROUTE_REQUEST, setOptRoute)
}

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