Update routes to use query params, fix caching

This commit is contained in:
Ben Goldsworthy 2022-10-20 15:53:32 +00:00
parent 1cf3c3004b
commit 3e62eb8d37
2 changed files with 20 additions and 29 deletions

View file

@ -65,8 +65,8 @@
</li> </li>
@endforeach @endforeach
</ol> </ol>
@if ($trip->is_active) @if (!$showAllCheckins)
<p><a id="allCheckinsLink" href="#">Show all checkins</a></p> <p><a id="allCheckinsLink" href="?show=all">Show all checkins</a></p>
@endif @endif
</section> </section>
</main> </main>
@ -141,11 +141,6 @@
).addTo(map).setZIndexOffset(1000); ).addTo(map).setZIndexOffset(1000);
currLocation.bindPopup('<p>Current location</p>').openPopup(); currLocation.bindPopup('<p>Current location</p>').openPopup();
/*
* Add all checkins link href.
*/
document.getElementById("allCheckinsLink").href = window.location.pathname + '/all';
/* /*
* Changes the currently-selected popup. * Changes the currently-selected popup.
*/ */

View file

@ -1,38 +1,34 @@
<?php <?php
use GuzzleHttp\Client;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Route; use Illuminate\Support\Facades\Route;
use Illuminate\Support\Facades\Storage; use Illuminate\Support\Facades\Storage;
use GuzzleHttp\Client;
/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/
Route::get('/past-trips', function () { Route::get('/past-trips', function () {
return view('past-trips'); return view('past-trips');
}); });
Route::get('/{tripId?}/{showAll?}', function ($tripId = null, $showAll = false) { Route::get('/{tripId?}', function (Request $request, $tripId = null) {
if (!$tripId && !config('app.current_trip_id')) return view('no-trip'); if (!$tripId && !config('app.current_trip_id')) return view('no-trip');
$viewMode = $request->input('show', null);
/* /*
* If there is a file in the local cache that is less than 5 hours old, * If there is a file in the local cache that is less than 3 hours old,
* use that. * use that.
*/ */
$tripFileName = $tripId ?? config('app.current_trip_id') . '.json'; $tripFileName = ( $tripId ?? config('app.current_trip_id') ) . '.json';
if (Storage::disk('local')->exists($tripFileName)) { if (Storage::disk('local')->exists($tripFileName)) {
$cachedData = json_decode(Storage::disk('local')->get($tripFileName)); $cachedData = json_decode(Storage::disk('local')->get($tripFileName));
$cachedDataUpdatedAt = new DateTime($cachedData->trip->updated_at); if ($cachedData->trip->is_active) {
$now = new DateTime(); $cachedDataUpdatedAt = new DateTime($cachedData->trip->updated_at);
if (intval(($now->getTimestamp() - $cachedDataUpdatedAt->getTimestamp()) / 3600) <= 5) { $now = new DateTime();
return view('tracker', ['trip' => $cachedData->trip, 'showAllCheckins' => ($showAll === 'all')]); if (intval(($now->getTimestamp() - $cachedDataUpdatedAt->getTimestamp()) / 3600) <= 3) {
return view('tracker', ['trip' => $cachedData->trip, 'showAllCheckins' => ($viewMode === 'all')]);
}
} else {
return view('tracker', ['trip' => $cachedData->trip, 'showAllCheckins' => ($viewMode === 'all')]);
} }
} }
@ -61,8 +57,8 @@ Route::get('/{tripId?}/{showAll?}', function ($tripId = null, $showAll = false)
} }
} }
return view('tracker', ['trip' => $data->trip, 'showAllCheckins' => ($showAll === 'all')]); return view('tracker', ['trip' => $data->trip, 'showAllCheckins' => ($viewMode === 'all')]);
} else { } else {
return 'Something went wrong'; return 'Something went wrong';
} }
}); })->where('tripId', '[0-9a-z]{20}');