Combine current and past trip views
This commit is contained in:
parent
fac44484b4
commit
ff3c265968
3 changed files with 23 additions and 37 deletions
|
@ -16,8 +16,7 @@
|
||||||
</head>
|
</head>
|
||||||
|
|
||||||
<body>
|
<body>
|
||||||
|
<header class="page-header">
|
||||||
<header>
|
|
||||||
<h1 class="page-title">Past Trips</h1>
|
<h1 class="page-title">Past Trips</h1>
|
||||||
<nav class="other-links">
|
<nav class="other-links">
|
||||||
<ul>
|
<ul>
|
||||||
|
@ -26,12 +25,10 @@
|
||||||
</nav>
|
</nav>
|
||||||
</header>
|
</header>
|
||||||
|
|
||||||
<main class="main--list">
|
<main id="pastTripsList">
|
||||||
<ul>
|
<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>
|
</ul>
|
||||||
</main>
|
</main>
|
||||||
|
|
||||||
</body>
|
</body>
|
||||||
|
|
||||||
</html>
|
</html>
|
||||||
|
|
|
@ -16,9 +16,8 @@
|
||||||
</head>
|
</head>
|
||||||
|
|
||||||
<body>
|
<body>
|
||||||
|
|
||||||
<header class="page-header">
|
<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">
|
<nav class="other-links">
|
||||||
<ul>
|
<ul>
|
||||||
<li><p><a href="/past-trips">Past trips</a></p></li>
|
<li><p><a href="/past-trips">Past trips</a></p></li>
|
||||||
|
|
|
@ -15,13 +15,16 @@ use GuzzleHttp\Client;
|
||||||
|
|
|
|
||||||
*/
|
*/
|
||||||
|
|
||||||
Route::get('/', function () {
|
Route::get('/{tripId?}', function ($tripId = null) {
|
||||||
if (!config('app.current_trip_id')) return view('no-trip');
|
if (!$tripId && !config('app.current_trip_id')) return view('no-trip');
|
||||||
|
|
||||||
$tripFile = config('app.current_trip_id') . '.json';
|
/*
|
||||||
|
* If there is a file in the local cache that is less than 5 hours old,
|
||||||
if (Storage::disk('local')->exists($tripFile)) {
|
* use that.
|
||||||
$cachedData = json_decode(Storage::disk('local')->get($tripFile));
|
*/
|
||||||
|
$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);
|
$cachedDataUpdatedAt = new DateTime($cachedData->trip->updated_at);
|
||||||
$now = new DateTime();
|
$now = new DateTime();
|
||||||
if (intval(($now->getTimestamp() - $cachedDataUpdatedAt->getTimestamp()) / 3600) <= 5) {
|
if (intval(($now->getTimestamp() - $cachedDataUpdatedAt->getTimestamp()) / 3600) <= 5) {
|
||||||
|
@ -29,22 +32,25 @@ Route::get('/', function () {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Otherwise, download the latest tracking data.
|
||||||
|
*/
|
||||||
$client = new Client([
|
$client = new Client([
|
||||||
'base_uri' => 'https://app.wayward.travel/',
|
'base_uri' => 'https://app.wayward.travel/',
|
||||||
'timeout' => 3.0
|
'timeout' => 3.0
|
||||||
]);
|
]);
|
||||||
|
$response = $client->get('trip/'.($tripId ?? config('app.current_trip_id')).'/user/zmld8ko6qy7d9j3xvq10/json');
|
||||||
$response = $client->get('trip/'.config('app.current_trip_id').'/user/zmld8ko6qy7d9j3xvq10/json');
|
|
||||||
|
|
||||||
if ($response->getStatusCode() == 200) {
|
if ($response->getStatusCode() == 200) {
|
||||||
$data = json_decode($response->getBody());
|
$data = json_decode($response->getBody());
|
||||||
|
|
||||||
if (Storage::disk('local')->missing($tripFile)) {
|
// Cache the downloaded file if it does not exist locally.
|
||||||
Storage::disk('local')->put($tripFile, json_encode($data));
|
if (Storage::disk('local')->missing($tripFileName)) {
|
||||||
|
Storage::disk('local')->put($tripFileName, json_encode($data));
|
||||||
} else {
|
} 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) {
|
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
|
// TODO: Cache photos locally
|
||||||
} else {
|
} else {
|
||||||
$data = $cachedData;
|
$data = $cachedData;
|
||||||
|
@ -60,19 +66,3 @@ Route::get('/', function () {
|
||||||
Route::get('/past-trips', function () {
|
Route::get('/past-trips', function () {
|
||||||
return view('past-trips');
|
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';
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
Loading…
Reference in a new issue