From cdd3086eddbf1305adecaa7dbf056f65e06974e3 Mon Sep 17 00:00:00 2001 From: Bart Akeley Date: Fri, 3 Apr 2020 22:21:26 -0500 Subject: [PATCH] fix search --- js/apis/FoodItemsApi.js | 62 +++++++++++++++++++---------------- js/components/TopToolbar.js | 4 +-- js/constants/AppConstants.js | 2 +- js/streams/FoodItemsStream.js | 2 +- 4 files changed, 38 insertions(+), 32 deletions(-) diff --git a/js/apis/FoodItemsApi.js b/js/apis/FoodItemsApi.js index b5c6383..6564a9a 100644 --- a/js/apis/FoodItemsApi.js +++ b/js/apis/FoodItemsApi.js @@ -1,14 +1,14 @@ // @flow -import { memoizeWith, identity } from "ramda"; -import FilterRecord from "../records/FilterRecord"; -import FoodItemRecord from "../records/FoodItemRecord"; -import AuthManager from "../AuthManager"; -import { map, path, nth } from "ramda"; -import { addImage } from "./ImagesApi"; -import { fetchRequest } from "./FetchApi"; +import { memoizeWith, identity } from 'ramda'; +import FilterRecord from '../records/FilterRecord'; +import FoodItemRecord from '../records/FoodItemRecord'; +import AuthManager from '../AuthManager'; +import { map, path, nth } from 'ramda'; +import { addImage } from './ImagesApi'; +import { fetchRequest } from './FetchApi'; export type FoodItemsFilter = { - radius?: number + radius?: number, }; export type RawFoodItem = { @@ -21,27 +21,33 @@ export type RawFoodItem = { latitude: number, longitude: number, distance: number, - lastupdated: number + lastupdated: number, }; export type FoodItemsForLocation = { orderby: string, filter: FoodItemsFilter, - fooditems: ?Array + fooditems: ?Array, }; export const getFoodItems = memoizeWith( - identity, - async ({ loc, filter }: { loc: Position, filter: FilterRecord }): Promise => { + ({ loc, filter }) => `${loc.coords.latitude + loc.coords.longitude}:${filter.toString()}`, + async ({ + loc, + filter, + }: { + loc: Position, + filter: FilterRecord, + }): Promise => { const { - coords: { latitude: lat, longitude: lng } + coords: { latitude: lat, longitude: lng }, } = loc; const { orderby, categories, radius, search } = filter; try { return fetchRequest({ - endpoint: "/fooditems", - method: "POST", + endpoint: '/fooditems', + method: 'POST', body: { lat, lng, @@ -49,22 +55,22 @@ export const getFoodItems = memoizeWith( filter: { ...(categories ? { categories } : {}), radius, - search - } - } - }).then(json => ({ + search, + }, + }, + }).then((json) => ({ ...json, loading: false, - error: null + error: null, })); } catch (error) { console.error(error); // eslint-disable-line no-console return { - orderby: "distance", + orderby: 'distance', filter: {}, fooditems: [], loading: false, - error: error + error: error, }; } } @@ -72,24 +78,24 @@ export const getFoodItems = memoizeWith( export const createFoodItem = async (foodItem: FoodItemRecord) => { if (!AuthManager.user) { - throw new Error("You must be logged in to create food items"); + throw new Error('You must be logged in to create food items'); } const username = AuthManager.user.name; const res = await fetchRequest({ - endpoint: "/addfooditem", - method: "POST", - body: foodItem + endpoint: '/addfooditem', + method: 'POST', + body: foodItem, }); const addImageUri = (imageUri: string) => addImage({ foodItemId: res.id, imageUri, username }); - const images = map(path(["url"]), await Promise.all(map(addImageUri, foodItem.images.toArray()))); + const images = map(path(['url']), await Promise.all(map(addImageUri, foodItem.images.toArray()))); return { ...res, images, - thumbimage: nth(0, images) + thumbimage: nth(0, images), }; }; diff --git a/js/components/TopToolbar.js b/js/components/TopToolbar.js index f55020e..9bbce0a 100644 --- a/js/components/TopToolbar.js +++ b/js/components/TopToolbar.js @@ -53,12 +53,12 @@ class TopToolbar extends Component { onSearchCleared = () => { const { setFilter, filter } = this.props; - setFilter(filter.update((prevFilter) => prevFilter.set('search', ''))); + setFilter(filter.set('search', '')); }; onChangeText = (search) => { const { setFilter, filter } = this.props; - setFilter(filter.update((prevFilter) => prevFilter.set('search', search))); + setFilter(filter.set('search', search)); }; getRightElement = () => { diff --git a/js/constants/AppConstants.js b/js/constants/AppConstants.js index 11b0a80..6a504b8 100644 --- a/js/constants/AppConstants.js +++ b/js/constants/AppConstants.js @@ -4,7 +4,7 @@ export const BASE_URL = 'aretherecookies.herokuapp.com'; // @home -// export const BASE_URL = '192.168.1.8:3000'; +// export const BASE_URL = '192.168.1.247:3000'; // @stouthaus // export const BASE_URL = '192.168.1.181:3000'; diff --git a/js/streams/FoodItemsStream.js b/js/streams/FoodItemsStream.js index 07ea592..c5f4ee7 100644 --- a/js/streams/FoodItemsStream.js +++ b/js/streams/FoodItemsStream.js @@ -32,7 +32,7 @@ const fetchedFoodItems$ = Filter$.combineLatest(location$) if (!loc) { return Promise.resolve({}); } - return Observable.merge(Promise.resolve({}), getFoodItems({ filter, loc })); + return getFoodItems({ filter, loc }); }) .map(({ fooditems }: FoodItemsForLocation) => { if (fooditems) {