converting /src/sagas files to php laravel, done

This commit is contained in:
armiejean 2025-04-11 10:42:41 +08:00
parent 59aaa3d5ad
commit 6825ce3c30
17 changed files with 365 additions and 4 deletions

View File

@ -0,0 +1,30 @@
<?php
namespace App\Http\Middleware;
use Closure;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Session;
class CheckApiErrors
{
public function handle(Request $request, Closure $next)
{
$response = $next($request);
if ($response->exception) {
$error = $response->exception;
if ($error->getCode() === 401) {
Session::forget('token');
return redirect()->route('login')->with('error', 'Session expired. Please login again.');
} elseif ($error->getCode() === 404) {
return redirect('/404');
} else {
return redirect()->back()->with('error', $error->getMessage());
}
}
return $response;
}
}

View File

@ -0,0 +1,26 @@
<?php
namespace App\Livewire;
use Livewire\Component;
use Illuminate\Support\Facades\Session;
class ErrorHandler extends Component
{
public function handleError($status, $message = null)
{
if ($status == 401) {
Session::forget('token');
return redirect()->route('login')->with('error', 'Session expired. Please login again. ' . ($message ? '- ' . $message : ''));
} elseif ($status == 404) {
return redirect('/404')->with('error', 'Page not found.');
} else {
Session::flash('error', 'Something went wrong! ' . ($message ? '- ' . $message : ''));
}
}
public function render()
{
return view('livewire.error-handler');
}
}

View File

@ -0,0 +1,44 @@
<?php
namespace App\Livewire;
use Livewire\Component;
use Illuminate\Support\Facades\Http;
use Illuminate\Support\Facades\Session;
class FetchData extends Component
{
public $data = [];
public $loading = false;
public $url;
public function mount($url)
{
$this->url = $url;
$this->fetchData();
}
public function fetchData()
{
$this->loading = true;
try {
$response = Http::get(config('app.api_base_url') . $this->url);
if ($response->successful()) {
$this->data = $response->json();
} else {
Session::flash('error', 'Failed to fetch data: ' . $response->body());
}
} catch (\Exception $e) {
Session::flash('error', 'An error occurred while fetching data.');
} finally {
$this->loading = false;
}
}
public function render()
{
return view('livewire.fetch-data');
}
}

27
app/Livewire/Logout.php Normal file
View File

@ -0,0 +1,27 @@
<?php
namespace App\Livewire;
use Livewire\Component;
use Illuminate\Support\Facades\Http;
use Illuminate\Support\Facades\Session;
class Logout extends Component
{
public function logout()
{
try {
Http::withHeaders(['Authorization' => 'Bearer ' . Session::get('token')])->get(config('app.api_base_url') . '/logout');
Session::forget('token');
return redirect()->route('login')->with('success', 'Logged out successfully.');
} catch (\Exception $e) {
return redirect()->back()->with('error', 'Logout failed: ' . $e->getMessage());
}
}
public function render()
{
return view('livewire.logout');
}
}

View File

@ -7,6 +7,7 @@ use Illuminate\Foundation\Configuration\Middleware;
return Application::configure(basePath: dirname(__DIR__)) return Application::configure(basePath: dirname(__DIR__))
->withRouting( ->withRouting(
web: __DIR__.'/../routes/web.php', web: __DIR__.'/../routes/web.php',
commands: __DIR__.'/../routes/console.php', commands: __DIR__.'/../routes/console.php',
health: '/up', health: '/up',
) )

View File

@ -7,12 +7,15 @@
"license": "MIT", "license": "MIT",
"require": { "require": {
"php": "^8.2", "php": "^8.2",
"guzzlehttp/guzzle": "^7.9",
"laravel/framework": "^11.31", "laravel/framework": "^11.31",
"laravel/tinker": "^2.9", "laravel/tinker": "^2.9",
"laravel/ui": "^4.6",
"livewire/livewire": "^3.6" "livewire/livewire": "^3.6"
}, },
"require-dev": { "require-dev": {
"fakerphp/faker": "^1.23", "fakerphp/faker": "^1.23",
"laravel/breeze": "^2.3",
"laravel/pail": "^1.1", "laravel/pail": "^1.1",
"laravel/pint": "^1.13", "laravel/pint": "^1.13",
"laravel/sail": "^1.26", "laravel/sail": "^1.26",

126
composer.lock generated
View File

@ -4,7 +4,7 @@
"Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies",
"This file is @generated automatically" "This file is @generated automatically"
], ],
"content-hash": "3f2b8489b8613c8a63271dd2fed1a745", "content-hash": "285c8ee44334e3e198f19e9bd78c85e5",
"packages": [ "packages": [
{ {
"name": "brick/math", "name": "brick/math",
@ -1455,6 +1455,69 @@
}, },
"time": "2025-01-27T14:24:01+00:00" "time": "2025-01-27T14:24:01+00:00"
}, },
{
"name": "laravel/ui",
"version": "v4.6.1",
"source": {
"type": "git",
"url": "https://github.com/laravel/ui.git",
"reference": "7d6ffa38d79f19c9b3e70a751a9af845e8f41d88"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/laravel/ui/zipball/7d6ffa38d79f19c9b3e70a751a9af845e8f41d88",
"reference": "7d6ffa38d79f19c9b3e70a751a9af845e8f41d88",
"shasum": ""
},
"require": {
"illuminate/console": "^9.21|^10.0|^11.0|^12.0",
"illuminate/filesystem": "^9.21|^10.0|^11.0|^12.0",
"illuminate/support": "^9.21|^10.0|^11.0|^12.0",
"illuminate/validation": "^9.21|^10.0|^11.0|^12.0",
"php": "^8.0",
"symfony/console": "^6.0|^7.0"
},
"require-dev": {
"orchestra/testbench": "^7.35|^8.15|^9.0|^10.0",
"phpunit/phpunit": "^9.3|^10.4|^11.5"
},
"type": "library",
"extra": {
"laravel": {
"providers": [
"Laravel\\Ui\\UiServiceProvider"
]
},
"branch-alias": {
"dev-master": "4.x-dev"
}
},
"autoload": {
"psr-4": {
"Laravel\\Ui\\": "src/",
"Illuminate\\Foundation\\Auth\\": "auth-backend/"
}
},
"notification-url": "https://packagist.org/downloads/",
"license": [
"MIT"
],
"authors": [
{
"name": "Taylor Otwell",
"email": "taylor@laravel.com"
}
],
"description": "Laravel UI utilities and presets.",
"keywords": [
"laravel",
"ui"
],
"support": {
"source": "https://github.com/laravel/ui/tree/v4.6.1"
},
"time": "2025-01-28T15:15:29+00:00"
},
{ {
"name": "league/commonmark", "name": "league/commonmark",
"version": "2.6.1", "version": "2.6.1",
@ -6048,6 +6111,67 @@
}, },
"time": "2020-07-09T08:09:16+00:00" "time": "2020-07-09T08:09:16+00:00"
}, },
{
"name": "laravel/breeze",
"version": "v2.3.6",
"source": {
"type": "git",
"url": "https://github.com/laravel/breeze.git",
"reference": "390cbc433cb72fa6050965000b2d56c9ba6fd713"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/laravel/breeze/zipball/390cbc433cb72fa6050965000b2d56c9ba6fd713",
"reference": "390cbc433cb72fa6050965000b2d56c9ba6fd713",
"shasum": ""
},
"require": {
"illuminate/console": "^11.0|^12.0",
"illuminate/filesystem": "^11.0|^12.0",
"illuminate/support": "^11.0|^12.0",
"illuminate/validation": "^11.0|^12.0",
"php": "^8.2.0",
"symfony/console": "^7.0"
},
"require-dev": {
"laravel/framework": "^11.0|^12.0",
"orchestra/testbench-core": "^9.0|^10.0",
"phpstan/phpstan": "^2.0"
},
"type": "library",
"extra": {
"laravel": {
"providers": [
"Laravel\\Breeze\\BreezeServiceProvider"
]
}
},
"autoload": {
"psr-4": {
"Laravel\\Breeze\\": "src/"
}
},
"notification-url": "https://packagist.org/downloads/",
"license": [
"MIT"
],
"authors": [
{
"name": "Taylor Otwell",
"email": "taylor@laravel.com"
}
],
"description": "Minimal Laravel authentication scaffolding with Blade and Tailwind.",
"keywords": [
"auth",
"laravel"
],
"support": {
"issues": "https://github.com/laravel/breeze/issues",
"source": "https://github.com/laravel/breeze"
},
"time": "2025-03-06T14:02:32+00:00"
},
{ {
"name": "laravel/pail", "name": "laravel/pail",
"version": "v1.2.2", "version": "v1.2.2",

12
package-lock.json generated
View File

@ -6,7 +6,8 @@
"": { "": {
"dependencies": { "dependencies": {
"bootstrap": "^5.3.5", "bootstrap": "^5.3.5",
"bootstrap-icons": "^1.11.3" "bootstrap-icons": "^1.11.3",
"sweetalert2": "^11.17.2"
}, },
"devDependencies": { "devDependencies": {
"autoprefixer": "^10.4.20", "autoprefixer": "^10.4.20",
@ -2579,6 +2580,15 @@
"url": "https://github.com/sponsors/ljharb" "url": "https://github.com/sponsors/ljharb"
} }
}, },
"node_modules/sweetalert2": {
"version": "11.17.2",
"resolved": "https://registry.npmjs.org/sweetalert2/-/sweetalert2-11.17.2.tgz",
"integrity": "sha512-HKxDr1IyV3Lxr3W6sb61qm/p2epFIEdr5EKwteRFHnIg6f8nHFl2kX++DBVz16Mac+fFiU3hMpjq1L6yE2Ge5w==",
"funding": {
"type": "individual",
"url": "https://github.com/sponsors/limonte"
}
},
"node_modules/tailwindcss": { "node_modules/tailwindcss": {
"version": "3.4.17", "version": "3.4.17",
"resolved": "https://registry.npmjs.org/tailwindcss/-/tailwindcss-3.4.17.tgz", "resolved": "https://registry.npmjs.org/tailwindcss/-/tailwindcss-3.4.17.tgz",

View File

@ -16,6 +16,7 @@
}, },
"dependencies": { "dependencies": {
"bootstrap": "^5.3.5", "bootstrap": "^5.3.5",
"bootstrap-icons": "^1.11.3" "bootstrap-icons": "^1.11.3",
"sweetalert2": "^11.17.2"
} }
} }

View File

@ -1,3 +1,4 @@
@tailwind base; @tailwind base;
@tailwind components; @tailwind components;
@tailwind utilities; @tailwind utilities;
@import 'bootstrap/dist/css/bootstrap.min.css';

View File

@ -1 +1,3 @@
import './bootstrap'; import './bootstrap';
import 'bootstrap/dist/js/bootstrap.bundle.min.js';
import 'sweetalert2/dist/sweetalert2.min.js';

View File

@ -0,0 +1,5 @@
@extends('layouts.app')
@section('content')
<div class="alert alert-danger">Page Not Found (404)</div>
@endsection

View File

@ -0,0 +1,12 @@
<div>
@if(session('error'))
<script src="https://cdn.jsdelivr.net/npm/sweetalert2@11"></script>
<script>
Swal.fire({
icon: 'error',
title: 'Error',
text: '{{ session('error') }}',
});
</script>
@endif
</div>

View File

@ -0,0 +1,25 @@
<div>
@if($loading)
<div class="alert alert-info">Loading...</div>
@else
<div class="card">
<h1>Data</h1>
<ul class="list-group">
@foreach($data as $item)
<li class="list-group-item">{{ $item['name'] ?? 'No name' }}</li>
@endforeach
</ul>
</div>
@endif
@if(session('error'))
<script src="https://cdn.jsdelivr.net/npm/sweetalert2@11"></script>
<script>
Swal.fire({
icon: 'error',
title: 'Error',
text: '{{ session('error') }}',
});
</script>
@endif
</div>

View File

@ -0,0 +1,25 @@
<div>
<button wire:click="logout" class="btn btn-danger">Logout</button>
@if(session('success'))
<script src="https://cdn.jsdelivr.net/npm/sweetalert2@11"></script>
<script>
Swal.fire({
icon: 'success',
title: 'Success',
text: '{{ session('success') }}',
});
</script>
@endif
@if(session('error'))
<script src="https://cdn.jsdelivr.net/npm/sweetalert2@11"></script>
<script>
Swal.fire({
icon: 'error',
title: 'Error',
text: '{{ session('error') }}',
});
</script>
@endif
</div>

View File

@ -19,3 +19,16 @@
['path' => 'dashboard/settings', 'name' => 'Settings'], ['path' => 'dashboard/settings', 'name' => 'Settings'],
['path' => 'dashboard/settings/details', 'name' => 'Details', 'params' => true], ['path' => 'dashboard/settings/details', 'name' => 'Details', 'params' => true],
]" /> ]" />
@extends('layouts.app')
@section('content')
<div class="card">
<div class="card-body">
<h1>Welcome</h1>
<a href="{{ route('fetch.data', 'data') }}" class="btn btn-primary">Fetch Data</a>
<a href="{{ route('logout') }}" class="btn btn-danger">Logout</a>
</div>
</div>
@endsection

View File

@ -43,8 +43,20 @@ use App\Livewire\ValidationDisplay;
use App\Livewire\AuthStatus; use App\Livewire\AuthStatus;
use App\Livewire\FetchDataStatus; use App\Livewire\FetchDataStatus;
use App\Livewire\LogoutStatus; use App\Livewire\LogoutStatus;
use App\Livewire\FetchData;
use App\Livewire\Logout;
use App\Livewire\ErrorHandler;
Route::get('/fetch-data/{url}', FetchData::class)->name('fetch.data');
Route::get('/logout', Logout::class)->name('logout');
Route::get('/error-handler', ErrorHandler::class)->name('error.handler');
Route::get('/404', function () {
return view('errors.404');
})->name('404');
Route::get('/auth-status', function () { Route::get('/auth-status', function () {
return view('auth-status'); return view('auth-status');
})->name('auth.status'); })->name('auth.status');