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>
@endforeach
</ol>
@if ($trip->is_active)
<p><a id="allCheckinsLink" href="#">Show all checkins</a></p>
@if (!$showAllCheckins)
<p><a id="allCheckinsLink" href="?show=all">Show all checkins</a></p>
@endif
</section>
</main>
@ -141,11 +141,6 @@
).addTo(map).setZIndexOffset(1000);
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.
*/

View file

@ -1,38 +1,34 @@
<?php
use GuzzleHttp\Client;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Route;
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 () {
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');
$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.
*/
$tripFileName = $tripId ?? config('app.current_trip_id') . '.json';
$tripFileName = ( $tripId ?? config('app.current_trip_id') ) . '.json';
if (Storage::disk('local')->exists($tripFileName)) {
$cachedData = json_decode(Storage::disk('local')->get($tripFileName));
$cachedDataUpdatedAt = new DateTime($cachedData->trip->updated_at);
$now = new DateTime();
if (intval(($now->getTimestamp() - $cachedDataUpdatedAt->getTimestamp()) / 3600) <= 5) {
return view('tracker', ['trip' => $cachedData->trip, 'showAllCheckins' => ($showAll === 'all')]);
if ($cachedData->trip->is_active) {
$cachedDataUpdatedAt = new DateTime($cachedData->trip->updated_at);
$now = new DateTime();
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 {
return 'Something went wrong';
}
});
})->where('tripId', '[0-9a-z]{20}');