civictechdc / districthousing

District Housing lets caseworkers help clients apply for Section 8 housing by automatically filling out multiple PDF applications using one online form.
http://codefordc.github.io/districthousing/
MIT License
40 stars 64 forks source link

Precaching Geodata (Read last comment) #336

Closed tingaloo closed 9 years ago

tingaloo commented 9 years ago

PLEASE READ: Precaching technically works, a few concerns.

This wasn't as easy as I thought. I'm pushing this out because maybe someone knows how to work with AJAX better than me. Also not following the rails way of ajax, building upon existing code.

First Concern

If longitude and latitude aren't defined, housing form's coordinates immediately update on modal display. However there is a nasty redirect ON UPDATE from the server side controller that I do not know how to get around. And even that was confusing to read.

Please see housing_forms_controller changes.

And on the server side, it looks something like this.

screenshot

It will continuously patch/redirect until it reaches this error.

PATCH http://localhost:3000/housing_forms/53 net::ERR_TOO_MANY_REDIRECTS

But it successfully updates the coordinates. Therefore the next call will retrieve the coordinates from the database, SKIPPING THE UPDATE ENTIRELY

SECOND CONCERN

Since the update happens asynchronously, the updated location and the success notification DO NOT DISPLAY UNTIL PAGE REFRESH. I think the fix would be an ajax refresh of the list. And whatever changes necessary for redirection.

BUT WE HAVE PRECACHING SO I'LL STOP HERE.

emanuelfeld commented 9 years ago

Just glancing at this, but why are we geocoding upon map button click? Doesn't it make more sense to integrate it into the edit/add new location functionality?

tingaloo commented 9 years ago

Oh man you're absolutely right. I went tunnel vision on this fix, and went straight for ajax instead of doing it the Rails way. I'll give this a go later.

emanuelfeld commented 9 years ago

Great! Looking forward to this functionality.

tingaloo commented 9 years ago
  1. Moved precaching to #CREATE and #UPDATE.
  2. Removed encoding from location display.
  3. IT WORKS.

Concerns

  1. On updating a location, it sends TWO patch requests, one for the initial patch, followed by the AJAX patch for location.
  2. UPDATE has NO VALIDATIONS, by simply clicking Update Housing Form, Google's geoencoder is called.

My head hurts because #UPDATE immediately PATCHES the changes, so at any given time, @housing_form.location_changed? will return false. And I cannot do a check on whether location has changed specifically.

Someone with a solid understanding of ajax/callbacks can get this working I believe. I can explain this in more detail to whoever wants a greater explanation.

So...

You can test all this yourself by literally clicking on UPDATE HOUSING FORM on each pdf and watch the latitude and longitude forms fill. Also got some awesome debugging gems to play with.

emanuelfeld commented 9 years ago

Doesn't the map popup button only show if there is a lat/lon? If so, displayHousingLocationMap and the null check could be removed.

tingaloo commented 9 years ago

You're right. Removed those checks, ready for pulling.