From 395444b67ed6486e89c43c23d5ce6ac60a89a1b1 Mon Sep 17 00:00:00 2001 From: Edin Dazdarevic Date: Thu, 6 Apr 2017 02:05:06 +0200 Subject: [PATCH] Save seen listings in localStorage --- web/components/Main.js | 62 ++++++++++++++++++++++++++++++++++++++---- web/lib/api.js | 13 +++++++-- web/lib/handlers.js | 19 +++++++++++-- 3 files changed, 84 insertions(+), 10 deletions(-) diff --git a/web/components/Main.js b/web/components/Main.js index 9b01f5a..ab3d246 100644 --- a/web/components/Main.js +++ b/web/components/Main.js @@ -3,8 +3,8 @@ import Filters from './Filters'; import Listings from './Listings'; import ListingDetails from './ListingDetails'; import { pacSelectFirst } from '../helpers/googleMaps'; -import {loadProperties} from '../lib/api' -import {handleMessage} from '../lib/handlers' +import { loadProperties, loadSeen } from '../lib/api' +import { handleMessage } from '../lib/handlers' class Main extends React.Component { constructor(props) { @@ -121,11 +121,18 @@ class Main extends React.Component { return this.markers[index]; } + isSeen (id) { + const seen = loadSeen(); + return seen.findIndex(s => s === id) !== -1 + } + /* * Refreshes search */ refreshListings() { console.log('refreshListings'); + + const map = this.map; const { rooms, @@ -184,19 +191,27 @@ class Main extends React.Component { position : myLatLng, map : map, title : prop.title, - icon : this.defaultMarkerIcon(), + icon : this.isSeen(prop._id) ? this.visitedMarkerIcon() : this.defaultMarkerIcon(), id : prop._id }); marker.addListener('mouseover', () => { if (marker.id !== this.state.listingId) { - marker.setIcon(this.hoveredMarkerIcon()); + if (this.isSeen(marker.id)) { + marker.setIcon(this.visitedHoveredMarkerIcon()); + } else { + marker.setIcon(this.hoveredMarkerIcon()); + } } }); marker.addListener('mouseout', () => { if (marker.id !== this.state.listingId) { - marker.setIcon(this.defaultMarkerIcon()); + if (this.isSeen(marker.id)) { + marker.setIcon(this.visitedMarkerIcon()); + } else { + marker.setIcon(this.defaultMarkerIcon()); + } } }); @@ -206,7 +221,8 @@ class Main extends React.Component { const prevSelected = this.findMarker(this.state.listingId); if (prevSelected) { console.log('prevselected', prevSelected); - prevSelected.marker.setIcon(this.defaultMarkerIcon()); + //prevSelected.marker.setIcon(this.defaultMarkerIcon()); + prevSelected.marker.setIcon(this.visitedMarkerIcon()); } } @@ -262,6 +278,40 @@ class Main extends React.Component { return icon; } + /* + * Get visited marker icon + */ + visitedMarkerIcon () { + const sf = 0.5; + const width = 48; + const height = 64; + const icon = { + url : "static/images/pins_sprite.png", + size : new google.maps.Size(width * sf, height * sf), + scaledSize : new google.maps.Size(730 * sf, 102 * sf), + origin : new google.maps.Point(152 * sf, 36 * sf) + } + + return icon; + } + + /* + * Visited hovered marker icon + */ + visitedHoveredMarkerIcon () { + const sf = 0.5; + const width = 61; + const height = 82; + const icon = { + url : "static/images/pins_sprite.png", + size : new google.maps.Size(width * sf, height * sf), + scaledSize : new google.maps.Size(730 * sf, 102 * sf), + origin : new google.maps.Point(480 * sf, 18 * sf) + } + + return icon; + } + /* * Hovered marker icon */ diff --git a/web/lib/api.js b/web/lib/api.js index 439d1b6..198427c 100644 --- a/web/lib/api.js +++ b/web/lib/api.js @@ -25,7 +25,16 @@ export const loadProperties = ({ //credentials: 'include' }); - //const body = await res.text(); - //return JSON.parse(body); } +export const markSeen = (id) => { + const seen = JSON.parse(window.localStorage.getItem('seen') || '[]'); + seen.push(id); + window.localStorage.setItem('seen', JSON.stringify(seen)); +} + +export const loadSeen = (id) => { + const seen = JSON.parse(window.localStorage.getItem('seen') || '[]'); + return seen; + //return seen.findIndex(s => s === id) !== -1; +} diff --git a/web/lib/handlers.js b/web/lib/handlers.js index 7b2c13d..6a63296 100644 --- a/web/lib/handlers.js +++ b/web/lib/handlers.js @@ -1,3 +1,5 @@ +import { markSeen } from './api'; + const setMaxPrice = ({ type, action }, component) => { const maxPrice = parseFloat(action.maxPrice); component.setState({ @@ -48,6 +50,8 @@ const viewListingDetails = ({ type, action }, component) => { listingId: action.id, descriptionExpanded: false, imageIndex: 0 + }, () => { + markSeen(action.id); }); }; @@ -156,11 +160,22 @@ const setCategory = ({type, action}, component) => { const onListingMouseOver = ({type, action}, component) => { const marker = component.findMarker(action.id); if (marker) { - marker.marker.setIcon(component.hoveredMarkerIcon()); + const seen = component.isSeen(action.id); + if (seen) { + marker.marker.setIcon(component.visitedHoveredMarkerIcon()); + } else { + marker.marker.setIcon(component.hoveredMarkerIcon()); + } + marker.marker.setAnimation(google.maps.Animation.BOUNCE); setTimeout(() => { marker.marker.setAnimation(null); - marker.marker.setIcon(component.defaultMarkerIcon()); + + if (seen) { + marker.marker.setIcon(component.visitedMarkerIcon()); + } else { + marker.marker.setIcon(component.defaultMarkerIcon()); + } } , 710); }