Skip to content

Commit

Permalink
Add city filter to Adoption page
Browse files Browse the repository at this point in the history
  • Loading branch information
kellynvd committed Jan 21, 2020
1 parent c322a8b commit 51dbd62
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 4 deletions.
1 change: 1 addition & 0 deletions app/controllers/v1/pets_for_adoption_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ def index
pets = Pet.active
pets = pets.by_sex(params[:sex]) if params[:sex].present?
pets = pets.by_description(params[:description]) if params[:description].present?
pets = pets.by_city(params[:city]) if params[:city].present?

render json: {
pets: ListPets.new.all(pets, params[:user_email])
Expand Down
8 changes: 7 additions & 1 deletion app/javascript/actions/adoptionFilters.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
export const types = {
SET_SEX_FILTER: 'ADOPTION_FILTERS/SET_SEX_FILTER',
SET_DESCRIPTION_FILTER: 'ADOPTION_FILTERS/SET_DESCRIPTION_FILTER'
SET_DESCRIPTION_FILTER: 'ADOPTION_FILTERS/SET_DESCRIPTION_FILTER',
SET_CITY_FILTER: 'ADOPTION_FILTERS/SET_CITY_FILTER'
};

export const setSexFilter = (sex = '') => ({
Expand All @@ -12,3 +13,8 @@ export const setDescriptionFilter = (description = '') => ({
type: types.SET_DESCRIPTION_FILTER,
description
});

export const setCityFilter = (city = '') => ({
type: types.SET_CITY_FILTER,
city
});
5 changes: 5 additions & 0 deletions app/javascript/containers/AdoptionList/AdoptionList.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ export function fetchPetsForAdoption() {
const userEmail = getState().app.user.email;
const sex = getState().adoptionFilters.sex;
const description = getState().adoptionFilters.description;
const city = getState().adoptionFilters.city;
dispatch({type: GET_ADOPTION_REQUEST});

let params = [];
Expand All @@ -28,6 +29,10 @@ export function fetchPetsForAdoption() {
params.push(`description=${description}`);
}

if (city) {
params.push(`city=${city}`);
}

const urlParams = params.join('&');

return (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import TextInput from "../../../components/TextInput/TextInput";
import Button from "../../../components/Button/Button";
import {createStructuredSelector} from "reselect";
import {connect} from "react-redux";
import { setSexFilter, setDescriptionFilter } from '../../../actions/adoptionFilters';
import { setSexFilter, setDescriptionFilter, setCityFilter } from '../../../actions/adoptionFilters';
import { fetchPetsForAdoption } from '../AdoptionList'

const GET_NGO_CITIES_REQUEST = 'GET_NGO_CITIES_REQUEST';
Expand Down Expand Up @@ -42,6 +42,10 @@ class AdoptionFilterBox extends React.Component {
this.props.setDescriptionFilter(e.target.value);
};

onCityChange = (e) => {
this.props.setCityFilter(e.target.value);
};

render() {
const cities = this.props;
return (
Expand All @@ -52,6 +56,8 @@ class AdoptionFilterBox extends React.Component {
width='200px'
marginRight='20px'
options={cities.cities}
value={this.props.adoptionFilters.city}
onChange={this.onCityChange}
/>
<SelectInput
label='ONG'
Expand Down Expand Up @@ -92,7 +98,8 @@ const mapDispatchToProps = {
fetchPetsForAdoption,
fetchCitiesForAdoption,
setSexFilter,
setDescriptionFilter
setDescriptionFilter,
setCityFilter
};

export default connect(mapStateToProps, mapDispatchToProps)(AdoptionFilterBox);
8 changes: 7 additions & 1 deletion app/javascript/reducers/adoptionFilters.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ import { types } from '../actions/adoptionFilters';

const adoptionFiltersReducerDefaultState = {
sex: '',
description: ''
description: '',
city: ''
};

export default (state = adoptionFiltersReducerDefaultState, action) => {
Expand All @@ -17,6 +18,11 @@ export default (state = adoptionFiltersReducerDefaultState, action) => {
...state,
description: action.description
};
case types.SET_CITY_FILTER:
return {
...state,
city: action.city
};
default:
return state;
}
Expand Down
1 change: 1 addition & 0 deletions app/models/pet.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ class Pet < ApplicationRecord
scope :active, -> { where(active: true) }
scope :by_sex, ->(sex) { where(sex: sex) }
scope :by_description, ->(description) { where('description LIKE ?', "%#{description}%") }
scope :by_city, ->(city) { includes(:ngo).where(ngos: { city: city }) }

def days_ago
created_at
Expand Down

0 comments on commit 51dbd62

Please sign in to comment.