Combine current and past trip views

This commit is contained in:
Ben Goldsworthy 2022-10-19 15:48:11 -07:00
parent fac44484b4
commit ff3c265968
3 changed files with 23 additions and 37 deletions

View file

@ -16,8 +16,7 @@
</head>
<body>
<header>
<header class="page-header">
<h1 class="page-title">Past Trips</h1>
<nav class="other-links">
<ul>
@ -26,12 +25,10 @@
</nav>
</header>
<main class="main--list">
<main id="pastTripsList">
<ul>
<li><a href="/past-trip/47xj8go928ql9mq3r20p">Europespedition III: Leds Free or Die Hard</a></li>
<li><a href="/47xj8go928ql9mq3r20p">Europespedition III: Leds Free or Die Hard</a></li>
</ul>
</main>
</body>
</html>

View file

@ -16,9 +16,8 @@
</head>
<body>
<header class="page-header">
<h1 class="page-title">Where in the World is Ben?</h1>
<h1 class="page-title">@if ($trip->is_active) Where in the World is Ben? @else {{ $trip->title }} @endif</h1>
<nav class="other-links">
<ul>
<li><p><a href="/past-trips">Past trips</a></p></li>

View file

@ -15,13 +15,16 @@ use GuzzleHttp\Client;
|
*/
Route::get('/', function () {
if (!config('app.current_trip_id')) return view('no-trip');
Route::get('/{tripId?}', function ($tripId = null) {
if (!$tripId && !config('app.current_trip_id')) return view('no-trip');
$tripFile = config('app.current_trip_id') . '.json';
if (Storage::disk('local')->exists($tripFile)) {
$cachedData = json_decode(Storage::disk('local')->get($tripFile));
/*
* If there is a file in the local cache that is less than 5 hours old,
* use that.
*/
$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) {
@ -29,22 +32,25 @@ Route::get('/', function () {
}
}
/*
* Otherwise, download the latest tracking data.
*/
$client = new Client([
'base_uri' => 'https://app.wayward.travel/',
'timeout' => 3.0
]);
$response = $client->get('trip/'.config('app.current_trip_id').'/user/zmld8ko6qy7d9j3xvq10/json');
$response = $client->get('trip/'.($tripId ?? config('app.current_trip_id')).'/user/zmld8ko6qy7d9j3xvq10/json');
if ($response->getStatusCode() == 200) {
$data = json_decode($response->getBody());
if (Storage::disk('local')->missing($tripFile)) {
Storage::disk('local')->put($tripFile, json_encode($data));
// Cache the downloaded file if it does not exist locally.
if (Storage::disk('local')->missing($tripFileName)) {
Storage::disk('local')->put($tripFileName, json_encode($data));
} else {
$cachedData = json_decode(Storage::disk('local')->get($tripFile));
$cachedData = json_decode(Storage::disk('local')->get($tripFileName));
if ($data->trip->updated_at !== $cachedData->trip->updated_at) {
Storage::disk('local')->put($tripFile, json_encode($data));
Storage::disk('local')->put($tripFileName, json_encode($data));
// TODO: Cache photos locally
} else {
$data = $cachedData;
@ -60,19 +66,3 @@ Route::get('/', function () {
Route::get('/past-trips', function () {
return view('past-trips');
});
Route::get('/past-trip/{tripId}', function ($tripId) {
$client = new Client([
'base_uri' => 'https://app.wayward.travel/',
'timeout' => 3.0
]);
$response = $client->get('trip/'.$tripId.'/user/zmld8ko6qy7d9j3xvq10/json');
if ($response->getStatusCode() == 200) {
$data = json_decode($response->getBody());
return view('past-trip', ['trip' => $data->trip]);
} else {
return 'Something went wrong';
}
});