{"size": 4481, "ext": "js", "max_stars_repo_path": "src/actions/listingActions.js", "max_stars_repo_name": "Build-Wweek-AirBnB-Optimal-Price-1/Front-End", "max_stars_repo_stars_event_min_datetime": null, "max_stars_repo_stars_event_max_datetime": null, "max_issues_repo_path": "src/actions/listingActions.js", "max_issues_repo_name": "Build-Wweek-AirBnB-Optimal-Price-1/Front-End", "max_issues_repo_issues_event_min_datetime": "2020-03-02T17:57:19.000Z", "max_issues_repo_issues_event_max_datetime": "2022-02-27T00:29:44.000Z", "max_forks_repo_path": "src/actions/listingActions.js", "max_forks_repo_name": "Build-Wweek-AirBnB-Optimal-Price-1/Front-End", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": null, "max_issues_count": 44, "max_forks_count": null, "avg_line_length": 34.4692307692, "max_line_length": 152, "alphanum_fraction": 0.5556795358}


import { axiosWithAuth } from '../utils/axiosWithAuth'
import { axiosData, formatQuery } from '../utils/axiosData';

export const FETCH_LISTINGS = "FETCH_LISTINGS";
export const FETCH_LISTINGS_SUCCESS = "FETCH_LISTINGS_SUCCESS";
export const FETCH_LISTINGS_ERROR = "FETCH_LISTING_ERROR";


export const ADD_LISTING = "ADD_LISTING";
export const ADD_LISTING_SUCCESS = "ADD_LISTING_SUCCESS";
export const ADD_LISTING_ERROR = "ADD_LISTING_ERROR";


export const EDIT_LISTING = "EDIT_LISTING";
export const EDIT_LISTING_SUCCESS = "EDIT_LISTING_SUCCESS";
export const EDIT_LISTING_ERROR = "EDIT_LISTING_ERROR";


export const DELETE_LISTING = "DELETE_LISTING";
export const DELETE_LISTING_SUCCESS = "DELETE_LISTING_SUCCESS";
export const DELETE_LISTING_ERROR = "DELETE_LISTING_ERROR";





export const getListings = () => dispatch => {
    dispatch({ type: FETCH_LISTINGS })
    axiosWithAuth()
        //Write in correct endpoint here
        .get('/listing')
        .then(res => {
            console.log(res)
            //Write in correct data property here
            dispatch({ type: FETCH_LISTINGS_SUCCESS, payload: res.data })
        })
        .catch(err => {
            //Write in correct error property here
            console.log(err)
            dispatch({ type: FETCH_LISTINGS_ERROR, payload: err })
        })
}

export const addListing = (newListing, history) => dispatch => {
    console.log(newListing)
    dispatch({ type: ADD_LISTING })
    axiosData()
        .get(formatQuery(newListing))
        .then((dataRes) => {
            console.log(dataRes)
            const newListingWithPrice = {
                ...newListing,
                price: dataRes.data[0].toFixed(2),
                id: Math.random().toString(36).substring(2, 4) + Math.random().toString(36).substring(2, 4) + Math.random().toString(36).substring(2, 4)
            }
            console.log(newListingWithPrice)
            dispatch({ type: ADD_LISTING_SUCCESS, payload: newListingWithPrice})
            history.push('/listings')

        //     axiosWithAuth()
        //         .post('/listing', newListingWithPrice)
        //         .then(backendRes => {
        //                 console.log(backendRes)
        //             // dispatch({ type: ADD_LISTING_SUCCESS, payload: backendRes.data })
        //                 // Make sure history pushes to id of new listing
        //                 // history.push(`/listings/details/${backendRes.data.id}`)
        //         })
        //         .catch(err => {
        //             console.log(err);

        //             // dispatch({ type: ADD_LISTING_ERROR, payload: err.data.message })
        //         })
        })
        .catch(err => {
            dispatch({ type: ADD_LISTING_ERROR, payload: err.data.message })
        });


}

export const editListing = (editedListing, history) => dispatch => {

    dispatch({ type: EDIT_LISTING })


    axiosData()
        .get(formatQuery(editedListing))
        .then((dataRes) => {
                    const editedListingWithPrice = {
                        ...editedListing,
                        price: dataRes.data[0].toFixed(2)
                    }
                    dispatch({ type: EDIT_LISTING_SUCCESS, payload: editedListingWithPrice})
                    history.push('/listings')
                    

            //  axiosWithAuth()
            //     .put(`/listing/${editedListing.id}`, editedListingWithPrice)
            //     .then(backendRes => {
            //         // dispatch({ type: EDIT_LISTING_SUCCESS, payload: backendRes.data })

            //             // Make sure history pushes to id of Edited listing
            //             // history.push(`/listings/details/${backendRes.data.id}`)
            //     })
            //     .catch(err => {
            //         // dispatch({ type: EDIT_LISTING_ERROR, payload: err })
            //     })
        })
        .catch(err => {
            dispatch({ type: EDIT_LISTING_ERROR, payload: err.data.message })
        });


}

export const deleteListing = (id) => dispatch => {
    dispatch({ type: DELETE_LISTING })
            dispatch({ type: DELETE_LISTING_SUCCESS, payload: id })

    // axiosWithAuth()
    //     .delete(`/listing/${id}`)
    //     .then(res => {
    //         dispatch({ type: DELETE_LISTING_SUCCESS, payload: id })
    //     })
    //     .catch(err => {
    //         dispatch({ type: DELETE_LISTING_ERROR, payload: err })
    //     })
}