import { GET_ALL_LISTINGS, GET_ALL_LISTINGS_SUCCESS, FETCHING_LISTINGS, CHANGE_SEARCH_FIELD, CHANGE_FILTER_CATEGORY, CHANGE_DISTANCE_RADIUS } from '../constants';
import { changeCenter, addMapMarkers } from './googleMapActions';
import { fetchAllListings } from './api';

const startFetchListings = () =>
  ({
    type: FETCHING_LISTINGS,
  });

export const getAllListingsSuccess = payload =>
  ({
    type: GET_ALL_LISTINGS_SUCCESS,
    payload,
  });

export const getAllListings = () =>
  (dispatch, getState) => {
    dispatch(startFetchListings());
    fetchAllListings()
    .then((res) => {
      res.json()
      .then((data) => {
        if (getState().auth.loggedInUser.role === 'professional') {
          dispatch(changeCenter(getState().auth.loggedInUser.business));
        } else {
          dispatch(changeCenter(getState().auth.loggedInUser));
        }
        dispatch(addMapMarkers(data));
      });
    });
  };

export const changeSearchField = value =>
  ({
    type: CHANGE_SEARCH_FIELD,
    value,
  });

export const changeFilterCategory = category =>
  ({
    type: CHANGE_FILTER_CATEGORY,
    category,
  });

export const changeDistanceRadius = distance =>
  ({
    type: CHANGE_DISTANCE_RADIUS,
    distance,
  });

@@ -132,16 +132,36 @@
 E_RADIUS
+, CHANGE_SORT_FILTER
  %7D from 
@@ -1251,16 +1251,108 @@
 distance,%0A  %7D);%0A
+%0Aexport const changeSortFilter = sort =%3E%0A  (%7B%0A    type: CHANGE_SORT_FILTER,%0A    sort,%0A  %7D);%0A

add action for change sort