actions

mail@pastecode.io avatar
unknown
javascript
2 years ago
4.5 kB
2
Indexable
Never
import axios from "axios";

import {
    SHOW_LOADING_OVERLAY,
    HIDE_LOADING_OVERLAY,
    GEOCODED_GPS_COORDINATES,
    CLEAR_GEOCODED_GPS_COORDINATES,
    GEOCODED_PLANT_GPS_COORDINATES,
    CLEAR_GEOCODED_PLANT_GPS_COORDINATES,
    SHOW_ERROR_MESSAGE
} from "../types";

const URL = "/api/v1/geocodeAddress";

export const geocodeAddress = ({ province, city }) => async dispatch => {
    try {
        // Dispatch to Show Loading Overlay Spinner
        dispatch({ type: SHOW_LOADING_OVERLAY, payload: "Geocoding Address" });
        // Initialize Form Data
        const formData = new FormData();
        // Append Form Data
        formData.append("province", province);
        formData.append("city", city);
        // Make API Request to Geocode Address
        const { data } = await axios({
            url: URL,
            method: "POST",
            data: formData,
            headers: {
                Accept: "application/json",
                "Content-Type": "multipart/form-data"
            }
        });
        // Destructure Data
        const { lat, lon } = data[0];
        // Create Payload Data
        const payload = [parseFloat(lat), parseFloat(lon)];
        // Dispatch Geocoded GPS Coordinates
        dispatch({ type: GEOCODED_GPS_COORDINATES, payload });
        // Dispatch to Hide Loading Overlay Spinner
        dispatch({ type: HIDE_LOADING_OVERLAY });
    } catch (e) {
        // Dispatch to Hide Loading Overlay Spinner
        dispatch({ type: HIDE_LOADING_OVERLAY });
        // Dispatch to Show Error Message
        dispatch({ type: SHOW_ERROR_MESSAGE });
    }
};

export const plantGeocodeAddress = ({
    plantProvince,
    plantCity
}) => async dispatch => {
    try {
        // Dispatch to Show Loading Overlay Spinner
        dispatch({
            type: SHOW_LOADING_OVERLAY,
            payload: "Geocoding Plant Address"
        });
        // Initialize Form Data
        const formData = new FormData();
        // Append Form Data
        formData.append("province", plantProvince);
        formData.append("city", plantCity);
        // Make API Request to Geocode Address
        const { data } = await axios({
            url: URL,
            method: "POST",
            data: formData,
            headers: {
                Accept: "application/json",
                "Content-Type": "multipart/form-data"
            }
        });
        // Destructure Data
        const { lat, lon } = data[0];
        // Create Payload Data
        const payload = [parseFloat(lat), parseFloat(lon)];
        // Dispatch Geocoded GPS Coordinates
        dispatch({ type: GEOCODED_PLANT_GPS_COORDINATES, payload });
        // Dispatch to Hide Loading Overlay Spinner
        dispatch({ type: HIDE_LOADING_OVERLAY });
        return [parseFloat(lat), parseFloat(lon)];
    } catch (e) {
        // Dispatch to Hide Loading Overlay Spinner
        dispatch({ type: HIDE_LOADING_OVERLAY });
        // Dispatch to Show Error Message
        dispatch({ type: SHOW_ERROR_MESSAGE });
    }
};

export const warehouseGeocodeAddress = ({
    province,
    city
}) => async dispatch => {
    try {
        // Dispatch to Show Loading Overlay Spinner
        dispatch({ type: SHOW_LOADING_OVERLAY, payload: "Geocoding Address" });
        // Initialize Form Data
        const formData = new FormData();
        // Append Form Data
        formData.append("province", province);
        formData.append("city", city);
        // Make API Request to Geocode Address
        const { data } = await axios({
            url: URL,
            method: "POST",
            data: formData,
            headers: {
                Accept: "application/json",
                "Content-Type": "multipart/form-data"
            }
        });
        // Destructure Data
        const { lat, lon } = data[0];
        // Dispatch to Hide Loading Overlay Spinner
        dispatch({ type: HIDE_LOADING_OVERLAY });
        // Return GPS Coordinates
        return [parseFloat(lat), parseFloat(lon)];
    } catch (e) {
        // Dispatch to Hide Loading Overlay Spinner
        dispatch({ type: HIDE_LOADING_OVERLAY });
        // Dispatch to Show Error Message
        dispatch({ type: SHOW_ERROR_MESSAGE });
    }
};

export const clearGeocodedGPS = () => ({
    type: CLEAR_GEOCODED_GPS_COORDINATES,
    CLEAR_GEOCODED_PLANT_GPS_COORDINATES
});