import { FETCHING_LISTINGS, GET_ALL_LISTINGS_SUCCESS, CHANGE_SEARCH_FIELD, CHANGE_DISTANCE_RADIUS, CHANGE_FILTER_CATEGORY } from '../constants';


export const initialState = {
  allListings: [],
  isFetching: false,
  searchField: '',
  filters: {
    categories: {
      'Home Improvement': false,
      Technology: false,
    },
    distance: false,
  },
};

export const listings = (state = initialState, action) => {
  switch (action.type) {
    case GET_ALL_LISTINGS_SUCCESS:
      return ({ ...state, allListings: action.payload, isFetching: false });
    case FETCHING_LISTINGS:
      return ({ ...state, isFetching: true });
    case CHANGE_SEARCH_FIELD:
      return ({ ...state, searchField: action.value });
    case CHANGE_FILTER_CATEGORY:
      return ({ ...state, filters: { ...state.filters, categories: {...state.filters.categories, [action.category]: !state.filters.categories[action.category] } } });
    case CHANGE_DISTANCE_RADIUS:
      return ({ ...state, filters: { ...state.filters, distance: action.distance } });
    default:
      return state;
  }
};

import { FETCHING_LISTINGS, GET_ALL_LISTINGS_SUCCESS, CHANGE_SEARCH_FIELD, CHANGE_DISTANCE_RADIUS, CHANGE_FILTER_CATEGORY, CHANGE_SORT_FILTER } from '../constants';


export const initialState = {
  allListings: [],
  isFetching: false,
  searchField: '',
  filters: {
    categories: {
      'Home Improvement': false,
      Technology: false,
    },
    distance: false,
    sort: false,
  },
};

export const listings = (state = initialState, action) => {
  switch (action.type) {
    case GET_ALL_LISTINGS_SUCCESS:
      return ({ ...state, allListings: action.payload, isFetching: false });
    case FETCHING_LISTINGS:
      return ({ ...state, isFetching: true });
    case CHANGE_SEARCH_FIELD:
      return ({ ...state, searchField: action.value });
    case CHANGE_FILTER_CATEGORY:
      return ({ ...state, filters: { ...state.filters, categories: {...state.filters.categories, [action.category]: !state.filters.categories[action.category] } } });
    case CHANGE_DISTANCE_RADIUS:
      return ({ ...state, filters: { ...state.filters, distance: action.distance } });
    case CHANGE_SORT_FILTER:
      return ({ ...state, filters: { ...state.filters, sort: action.sort } });
    default:
      return state;
  }
};

add change sort to listing reducer