Skip to content

Commit

Permalink
Merge branch 'shiolovesgod-enhance-savepin' into development
Browse files Browse the repository at this point in the history
  • Loading branch information
jkwening committed Mar 10, 2018
2 parents 06f9d9a + 129a692 commit d8b7b44
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 28 deletions.
54 changes: 41 additions & 13 deletions client/src/components/LeafletOsmMap/index.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import { h, Component } from 'preact';
import style from './style';
import { h, Component } from "preact";
import { route } from "preact-router";
import style from "./style";
import MapPane from './MapPane';
import Search from '../../components/Search';
import SearchResults from '../../components/SearchResults';
import { makeRequest } from '../../js/server-requests-utils';
import {fetchAndDropUserPins} from '../../js/saved-places';
import {fetchAndDropUserPins, makePinMarkers, dropPin} from '../../js/saved-places';

/**
* Leaflet related imports: leaflet, pouchdb module, and routing machine module
Expand Down Expand Up @@ -174,19 +175,46 @@ export default class LeafletOSMMap extends Component {
const saveBtn = createButton('Save', container);
const deleteBtn = createButton('Remove', container);

L.DomEvent.on(saveBtn, 'click', function() {
L.DomEvent.on(saveBtn, 'click', function () {
makeRequest('POST', 'savedPins', '', {
lat: event.latlng.lat,
lng: event.latlng.lng
lng: event.latlng.lng,
}).then((response) => {

//remove old icon
droppedPin.remove()

//add a new one
const savedMarker = makePinMarkers([response.data.pin], L);
dropPin(savedMarker, event.target);

// alert(`Succes: Saved pin at ${event.latlng} to db`);

}).catch((err) => {

switch (err.response.status){
case 400: //duplicate pin
const origPin = err.response.data;
alert('This pin is already on your map.');
/*TO DO:
Convert popup alert to toast message
*/
break;
case 403: //user not signed in
/*TO DO:
Convert to overlay/lightbox window to sign up form
*/
if (confirm("Would you like to sign in to save places?")) {
route('/signin', true);
}

break;
default:
console.log(err); //error saving pin

}

})
.then(response => {
alert(`Succes: Saved pin at ${event.latlng} to db`);
console.log('Success - saved: ', response);
})
.catch(err => {
alert('Error saving pin: ', err);
console.log('Error saving pin: ', err);
});
});

L.DomEvent.on(deleteBtn, 'click', function() {
Expand Down
8 changes: 4 additions & 4 deletions client/src/js/saved-places.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,13 @@ const FAV_MARKER_OPTIONS = {

//Get and drop pins from any user on any map (all arguments are optional)
const fetchAndDropUserPins = (user_id, mapObj, L) => {

const favMarker = L ? L.icon(FAV_MARKER_OPTIONS) : undefined;

getSavedPins(user_id)
.then(savedPins => {

if (!savedPins) return;

const pinMarkers = makePinMarkers(savedPins, favMarker);
const pinMarkers = makePinMarkers(savedPins, L);
if (mapObj != null) dropPin(pinMarkers, mapObj);

})
Expand All @@ -46,8 +44,10 @@ const getSavedPins = (user_id) => {
}

//This function generates markers and drops them on a map (if specified)
const makePinMarkers = (pinArray = [], icon) => {
const makePinMarkers = (pinArray = [], L, markerOptions=FAV_MARKER_OPTIONS) => {

const icon = L ? L.icon(FAV_MARKER_OPTIONS) : undefined;

let pinMarkers = [];

for (const pin of pinArray) {
Expand Down
42 changes: 31 additions & 11 deletions controllers/saved-pins-controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,18 +60,38 @@ exports.getSavedPinsById = (appReq, appRes) => {
* @param {string} appReq.body.place_id - name of pin location
*/
exports.postSavedPins = (appReq, appRes) => {
const savedPin = new SavedPins({
lat: appReq.body.lat,
lng: appReq.body.lng,
place_id: appReq.body.place_id,
user: appReq.userId, // authenticated user's id
});

savedPin.save().then((pin) => {
appRes.send({ pin });
}, (e) => {
appRes.status(500).send(e);
});
//ensure not duplicate first
SavedPins.find({ user: appReq.userId })
.then(savedPins => {
//check to see if the pin already exists
return savedPins.filter(pin => {
return (pin.lat == appReq.body.lat) && (pin.lng == appReq.body.lng)
});

})
.then(duplicatePins => {

if (duplicatePins.length > 0) {
appRes.status(400).send({ duplicatePin: duplicatePins, message: 'Duplicate found. Pin not saved.' });
return
}

const newPin = new SavedPins({
lat: appReq.body.lat,
lng: appReq.body.lng,
place_id: appReq.body.place_id,
user: appReq.userId, // authenticated user's id
});

newPin.save().then((pin) => {
appRes.send({ pin });
}, (e) => {
appRes.status(500).send(e);
});


})
};

/**
Expand Down

0 comments on commit d8b7b44

Please sign in to comment.