Skip to content

Commit

Permalink
Updated results to include filters per #14
Browse files Browse the repository at this point in the history
  • Loading branch information
cw00dw0rd committed Mar 18, 2021
1 parent 4f06e00 commit 88b4743
Show file tree
Hide file tree
Showing 6 changed files with 221 additions and 44 deletions.
70 changes: 56 additions & 14 deletions workspaces/backend/src/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,20 +40,62 @@ ApiRouter.get("/api/results", async (ctx) => {
})

ApiRouter.post("/api/mapResults", async (ctx) => {
const result = [];
const [north, east, south, west] = ctx.request.body.mapArea;
const poly = [[west, north], [east, north], [east, south], [west, south], [west, north]];
const cursor = await ctx.db.query(aql`
FOR listing IN arangobnb
SEARCH ANALYZER(GEO_CONTAINS(GEO_POLYGON(${poly}), listing.location), "geo")
LIMIT 20
RETURN listing
`);
for await (const c of cursor){
result.push(c);
}
sendResponse(ctx, result);
})
try {

const result = [];
const body = ctx.request.body;
const [north, east, south, west] = body.mapArea;
const poly = [[west, north], [east, north], [east, south], [west, south], [west, north]];
const additionalCriteria = []

body.roomType ?
(body.roomType.length ? additionalCriteria.push(aql`
AND
ANALYZER(${body.roomType} ANY IN listing.room_type, "identity")
`) : ``) : ''

body.amenities ?
(body.amenities.length ? additionalCriteria.push(aql`
AND
ANALYZER(${body.amenities} ALL IN listing.amenities, "identity")
`) : ``) : ''

body.priceRange ?
( body.priceRange.length ? additionalCriteria.push(aql`
AND
IN_RANGE(listing.price, ${body.priceRange[0]}, ${body.priceRange[1]}, true, true)
`) : ``) : ''


const cursor = await ctx.db.query(aql`
LET listings = (
FOR listing IN arangobnb
SEARCH ANALYZER(GEO_CONTAINS(GEO_POLYGON(${poly}), listing.location), "geo")
${aql.join(additionalCriteria)}
SORT listing.number_of_reviews DESC, listing.review_scores_rating DESC
LIMIT 100
RETURN listing
)
Let amenities = (
FOR doc in listings
FOR amenity in doc.amenities
COLLECT item = amenity with COUNT into c
SORT c DESC
LIMIT 100
RETURN item
)
RETURN {listings, amenities}
`);
for await (const c of cursor){
result.push(c);
}
sendResponse(ctx, result);
} catch (e) {
sendResponse(e.message)
}
})

ApiRouter.get("/api/filters", async (ctx) => {
const result = [];
Expand Down
87 changes: 87 additions & 0 deletions workspaces/frontend-vue/src/components/Filters.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
<template>
<div>
<md-button>
<strong v-on:click="showHide">
Filter Results
</strong>
</md-button>
<md-card class="md-layout-item md-size-100 md-small-size-100" v-if="showFilters">
<md-card-content>
<h3>Room Types</h3>
<md-checkbox
v-for="room in roomTypes"
:key="room"
v-model="filters.roomType"
v-bind:value="room"
@change="updateFilters">
{{room}}
</md-checkbox>
</md-card-content>
<md-card-content>
<h3>Amenities</h3>
<div>
<md-checkbox
v-for="amenity in currentAmenities.slice(0,10)"
:key="amenity"
v-model="filters.amenities"
v-bind:value="amenity"
@change="updateFilters">
{{amenity}}
</md-checkbox>
</div>
</md-card-content>
<md-card-content>
<h3>Pricing</h3>
<div>
<label for="fname">Set Max Price</label>
<input
type="range"
min="0"
max="1000"
step="1"
v-model.number="maxPrice"
@change="updateFilters">
${{ maxPrice }}
</div>
</md-card-content>
</md-card>
</div>
</template>

<script>
import { mapState } from 'vuex'
export default {
name: "Filters",
data: () => ({
showFilters: false,
filters: {"roomType":[], "amenities": [], "priceRange": []},
roomTypes: [
"Entire home/apt",
"Private room",
"Shared room",
"Hotel room"
],
maxPrice: 1000
}),
computed:
mapState({
currentAmenities: state => state.map.currentAmenities,
}),
methods: {
showHide: function() {
this.showFilters = !this.showFilters;
},
updateFilters: function() {
this.filters.priceRange = [0, this.maxPrice]
this.$store.dispatch("map/setFilters", this.filters);
}
}
}
</script>

<style lang="scss" scoped>
.md-checkbox {
display: inline-flex;
}
</style>
22 changes: 22 additions & 0 deletions workspaces/frontend-vue/src/components/LogEvents.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<template>
<div>
<md-button>
<strong v-on:click="showHide">
{{ showEvents ? "Hide" : "Show" }} log events:
</strong>
</md-button>
<transition name="fade" v-if=showEvents>
<pre>{{moveEvents.join('\n')}}</pre>
</transition>
</div>
</template>

<script>
export default {
}
</script>

<style>
</style>
10 changes: 6 additions & 4 deletions workspaces/frontend-vue/src/components/Map.vue
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ export default {
mapState({
listings: state => state.map.listings,
mapPosition: state => state.map.mapPosition,
filters: state => state.map.filters
}),
watch: {
mapPosition: function() {
Expand All @@ -36,7 +37,10 @@ export default {
this.showEvents = !this.showEvents;
},
logEvent: function (type, text) {
this.$store.commit("map/setEvents", { type, text });
// Logs move events that the Log Event component relies on
// leaving for use in tech component
// TODO: Add tech component
this.$store.commit("map/setEvents", { type, text });
},
getCardinalDirectionsFromBounds: function (bounds) {
return [
Expand Down Expand Up @@ -70,7 +74,7 @@ export default {
function addMarkers(listings) {
const arangoIcon = new mapIcon({ iconUrl: mapMarker });
if (markersKeys.length >= 100) {
if (markersKeys.length >= 200) {
markerLayer.clearLayers(); // Would be nice to only clear "old" markers
markersKeys = [];
}
Expand Down Expand Up @@ -123,8 +127,6 @@ export default {
});
mymap.on("movestart", (e) => {
console.log(L.marker)
const bounds = e.target.getBounds();
const boundsString = [
Expand Down
13 changes: 4 additions & 9 deletions workspaces/frontend-vue/src/components/ResultsInfo.vue
Original file line number Diff line number Diff line change
@@ -1,13 +1,6 @@
<template>
<div class="md-layout-item md-scrollbar resultsContainer">
<md-button>
<strong v-on:click="showHide">
{{ showEvents ? "Hide" : "Show" }} log events:
</strong>
</md-button>
<transition name="fade" v-if=showEvents>
<pre>{{moveEvents.join('\n')}}</pre>
</transition>
<Filters />
<listingsCard class="resultCard" v-for="listing in listings" :key="listing._key" v-bind:listing=listing />
</div>

Expand All @@ -16,11 +9,13 @@
<script>
import { mapState } from 'vuex'
import listingsCard from './listingsCard'
import Filters from './Filters'
export default {
name: "ResultsInfo",
components: {
listingsCard
listingsCard,
Filters
},
data: () => ({
showEvents: true,
Expand Down
63 changes: 46 additions & 17 deletions workspaces/frontend-vue/src/store/modules/map.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,14 @@ const API = process.env.VUE_APP_API_ENDPOINT
export const state = () => ({
moveEvents: [],
//default map area
mapArea: [[13.3733650830416,52.513472114606735],[13.380215444865636,52.513472114606735],[13.387065806689671,52.513472114606735],[13.387065806689671,52.51550916176831],[13.387065806689671,52.51754620892988],[13.380215444865636,52.51754620892988],[13.3733650830416,52.51754620892988],[13.3733650830416,52.51550916176831],[13.3733650830416,52.513472114606735]],
mapArea: [52.52038609931014, 13.387355804443361, 52.51223805957966, 13.373665809631348],
listings: [],
markers:[],
mapPosition: {},
filters: []
currentAmenities: [],
amenities: [],
roomType: [],
priceRange: []
});

// getters
Expand All @@ -33,8 +36,14 @@ const actions = {
getResults: async ({commit, state}, payload) => {
payload ? (
payload.mapArea ? await commit('setMapArea', payload.mapArea) : console.log('no mapArea')) : console.log('no payload')


let data = JSON.stringify({"mapArea": state.mapArea});
let data = JSON.stringify({
mapArea: state.mapArea,
amenities: state.amenities,
roomType: state.roomType,
priceRange: state.priceRange
});

let config = {
method: 'post',
Expand All @@ -46,8 +55,11 @@ const actions = {
};
await axios(config)
.then( (response) => {
commit("setResults", { listings: response.data });
let data = response.data[0]

commit("setResults", data);
commit("setMarkers");
commit("setAmenities", data);
})
},
setDestination: ({commit}, payload) => {
Expand All @@ -57,16 +69,12 @@ const actions = {
console.log(e)
}
},
getFilters: ({commit}) => {
let config = {
method: 'get',
url: API + '/api/filters'
};
axios(config)
.then( (response) => {
console.log(response.data)
commit("setFilters", { filters: response.data });
})
setFilters: ({commit, dispatch}, payload) => {
payload.roomType ? commit("setSelectedRoomTypes", payload) : ''
payload.amenities ? commit("setSelectedAmmenities", payload) : ''
payload.priceRange ? commit("setSelectedPriceRange", payload) : ''
dispatch("getResults")

}
};

Expand Down Expand Up @@ -98,13 +106,34 @@ const mutations = {
console.log(e);
}
},
setFilters(state, filters) {
setAmenities(state, payload) {
try {
Vue.set(state, 'filters', filters);
Vue.set(state, 'currentAmenities', payload.amenities);
} catch(e) {
console.log(e);
}
}
},
setSelectedRoomTypes(state, payload) {
try {
Vue.set(state, 'roomType', payload.roomType)
} catch(e) {
console.log(e)
}
},
setSelectedAmmenities(state, payload) {
try {
Vue.set(state, 'amenities', payload.amenities)
} catch(e) {
console.log(e)
}
},
setSelectedPriceRange(state, payload) {
try {
Vue.set(state, 'priceRange', payload.priceRange)
} catch(e) {
console.log(e)
}
}
};


Expand Down

0 comments on commit 88b4743

Please sign in to comment.