Refactor error handling and enhance API interactions across Filament pages
- Introduced `ApiErrorTranslator` to normalize and translate API error messages, providing clearer feedback in French. - Updated all Filament pages (Articles, Documents, Divers, Journaux, Tiers, TablesExplorer) to utilize the new error translation mechanism, improving user experience during API interactions. - Added validation for required fields before API calls, ensuring users receive immediate feedback when mandatory inputs are missing. - Implemented tracking properties to distinguish between "never searched" and "searched without results," enhancing the user interface. - Removed the obsolete `$results` property from the Articles page and added a new `$barcode` property to align with API requirements. - Updated documentation to reflect changes in API behavior and error handling, including new metadata returned by the `art_list` endpoint. - Added new tests to verify the functionality of the barcode handling and validation logic.
This commit is contained in:
@@ -4,6 +4,7 @@ namespace App\Filament\Pages;
|
||||
|
||||
use App\Exceptions\LogisticsApiException;
|
||||
use App\Services\LogisticsService;
|
||||
use App\Support\ApiErrorTranslator;
|
||||
use Filament\Pages\Page;
|
||||
use Filament\Support\Icons\Heroicon;
|
||||
|
||||
@@ -23,9 +24,9 @@ class Articles extends Page
|
||||
|
||||
public string $search = '';
|
||||
|
||||
public string $select = 'artid,artname';
|
||||
public string $select = 'artid,name1';
|
||||
|
||||
public int $results = 10;
|
||||
public string $barcode = '';
|
||||
|
||||
public string $stockArticleId = '';
|
||||
|
||||
@@ -37,48 +38,62 @@ class Articles extends Page
|
||||
|
||||
public ?string $errorMessage = null;
|
||||
|
||||
public bool $hasSearched = false;
|
||||
|
||||
public bool $hasCheckedStock = false;
|
||||
|
||||
public function searchArticles(): void
|
||||
{
|
||||
$this->errorMessage = null;
|
||||
$this->hasSearched = true;
|
||||
|
||||
try {
|
||||
$service = app(LogisticsService::class);
|
||||
|
||||
$params = array_filter([
|
||||
'select' => $this->select,
|
||||
'results' => $this->results,
|
||||
'search' => $this->search,
|
||||
'barcode' => $this->barcode,
|
||||
]);
|
||||
|
||||
$response = $service->artList($params);
|
||||
|
||||
$this->data = $response['data'] ?? [];
|
||||
$this->metadata = $response['metadata'] ?? null;
|
||||
$this->errorMessage = $response['error'] ?? null;
|
||||
$this->errorMessage = ApiErrorTranslator::translate($response['error'] ?? null);
|
||||
|
||||
} catch (LogisticsApiException $e) {
|
||||
$this->errorMessage = $e->getMessage();
|
||||
$this->errorMessage = ApiErrorTranslator::translate($e->getMessage());
|
||||
$this->data = [];
|
||||
} catch (\Throwable $e) {
|
||||
$this->errorMessage = "Erreur inattendue : {$e->getMessage()}";
|
||||
$this->errorMessage = ApiErrorTranslator::translate($e->getMessage());
|
||||
$this->data = [];
|
||||
}
|
||||
}
|
||||
|
||||
public function getStock(): void
|
||||
{
|
||||
$this->errorMessage = null;
|
||||
|
||||
if (blank($this->stockArticleId)) {
|
||||
$this->errorMessage = 'Le champ identifiant article (ARTID) est obligatoire pour verifier le stock.';
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
$this->hasCheckedStock = true;
|
||||
|
||||
try {
|
||||
$service = app(LogisticsService::class);
|
||||
$response = $service->artGetStock($this->stockArticleId);
|
||||
|
||||
$this->stockData = $response['data'] ?? [];
|
||||
$this->errorMessage = $response['error'] ?? null;
|
||||
$this->errorMessage = ApiErrorTranslator::translate($response['error'] ?? null);
|
||||
} catch (LogisticsApiException $e) {
|
||||
$this->errorMessage = $e->getMessage();
|
||||
$this->errorMessage = ApiErrorTranslator::translate($e->getMessage());
|
||||
$this->stockData = [];
|
||||
} catch (\Throwable $e) {
|
||||
$this->errorMessage = "Erreur inattendue : {$e->getMessage()}";
|
||||
$this->errorMessage = ApiErrorTranslator::translate($e->getMessage());
|
||||
$this->stockData = [];
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user