|
|
@@ -4,7 +4,9 @@ namespace App\Filament\Pages;
|
|
|
|
|
|
use App\Services\KnowledgeServiceApi;
|
|
|
use BackedEnum;
|
|
|
+use Filament\Actions\Action;
|
|
|
use Filament\Pages\Page;
|
|
|
+use Illuminate\Http\Request;
|
|
|
use Illuminate\Support\Collection;
|
|
|
use Illuminate\Support\Str;
|
|
|
use UnitEnum;
|
|
|
@@ -29,48 +31,52 @@ class KnowledgePoints extends Page
|
|
|
|
|
|
public int $perPage = 8;
|
|
|
|
|
|
- protected ?array $selectedPoint = null;
|
|
|
-
|
|
|
protected bool $loading = false;
|
|
|
|
|
|
protected ?KnowledgeServiceApi $knowledgeService = null;
|
|
|
|
|
|
- public function mount(): void
|
|
|
+ protected ?string $selectedKpCode = null;
|
|
|
+
|
|
|
+ // Private property to store loaded data
|
|
|
+ private ?array $selectedPointData = null;
|
|
|
+
|
|
|
+ public function mount(Request $request): void
|
|
|
{
|
|
|
- $this->hydratePoints();
|
|
|
+ // Only set properties that need to be available immediately
|
|
|
+ // The search and filter properties will be set via getter
|
|
|
+ $this->selectedKpCode = $request->query('selected');
|
|
|
+ $this->page = (int) $request->query('page', 1);
|
|
|
}
|
|
|
|
|
|
- protected function getKnowledgeService(): KnowledgeServiceApi
|
|
|
+ public function getSelectedPointProperty(): ?array
|
|
|
{
|
|
|
- if (!$this->knowledgeService) {
|
|
|
- $this->knowledgeService = app(KnowledgeServiceApi::class);
|
|
|
+ if ($this->selectedPointData) {
|
|
|
+ return $this->selectedPointData;
|
|
|
}
|
|
|
|
|
|
- return $this->knowledgeService;
|
|
|
- }
|
|
|
+ // If we have a specific kp code to load
|
|
|
+ if ($this->selectedKpCode) {
|
|
|
+ $this->selectedPointData = $this->getKnowledgeService()->getKnowledgePointDetail($this->selectedKpCode);
|
|
|
+ return $this->selectedPointData;
|
|
|
+ }
|
|
|
|
|
|
- public function updatedPhaseFilter(): void
|
|
|
- {
|
|
|
- $this->page = 1;
|
|
|
- $this->hydratePoints();
|
|
|
- }
|
|
|
+ // Get first point from paginated results
|
|
|
+ $points = $this->paginatedPoints;
|
|
|
+ if (!empty($points['data']) && count($points['data']) > 0) {
|
|
|
+ $kpCode = $points['data'][0]['kp_code'];
|
|
|
+ $this->selectedPointData = $this->getKnowledgeService()->getKnowledgePointDetail($kpCode);
|
|
|
+ }
|
|
|
|
|
|
- public function updatedCategoryFilter(): void
|
|
|
- {
|
|
|
- $this->page = 1;
|
|
|
- $this->hydratePoints();
|
|
|
+ return $this->selectedPointData;
|
|
|
}
|
|
|
|
|
|
- public function updatedSearch(): void
|
|
|
+ protected function getKnowledgeService(): KnowledgeServiceApi
|
|
|
{
|
|
|
- $this->page = 1;
|
|
|
- $this->hydratePoints();
|
|
|
- }
|
|
|
+ if (!$this->knowledgeService) {
|
|
|
+ $this->knowledgeService = app(KnowledgeServiceApi::class);
|
|
|
+ }
|
|
|
|
|
|
- public function changePage(int $page): void
|
|
|
- {
|
|
|
- $this->page = max($page, 1);
|
|
|
- $this->hydratePoints();
|
|
|
+ return $this->knowledgeService;
|
|
|
}
|
|
|
|
|
|
public function selectPoint(string $kpCode): void
|
|
|
@@ -119,52 +125,57 @@ class KnowledgePoints extends Page
|
|
|
|
|
|
public function getPaginatedPointsProperty(): array
|
|
|
{
|
|
|
+ // Get filters from URL
|
|
|
+ $request = request();
|
|
|
+ $phaseFilter = $request->query('phase');
|
|
|
+ $categoryFilter = $request->query('category');
|
|
|
+ $searchTerm = trim((string) $request->query('search'));
|
|
|
+
|
|
|
+ // Get all points
|
|
|
$filters = array_filter([
|
|
|
- 'phase' => $this->phaseFilter,
|
|
|
- 'category' => $this->categoryFilter,
|
|
|
+ 'phase' => $phaseFilter,
|
|
|
+ 'category' => $categoryFilter,
|
|
|
]);
|
|
|
|
|
|
- $response = $this->getKnowledgeService()->paginateKnowledgePoints(
|
|
|
- page: $this->page,
|
|
|
- perPage: $this->perPage,
|
|
|
+ $allPoints = $this->getKnowledgeService()->listKnowledgePoints(
|
|
|
+ perPage: 200,
|
|
|
filters: $filters
|
|
|
);
|
|
|
|
|
|
- $records = collect($response['data'] ?? []);
|
|
|
-
|
|
|
- if (filled($this->search)) {
|
|
|
- $search = Str::lower($this->search);
|
|
|
- $records = $records->filter(function (array $record) use ($search): bool {
|
|
|
- return Str::contains(Str::lower($record['cn_name'] ?? ''), $search)
|
|
|
- || Str::contains(Str::lower($record['kp_code'] ?? ''), $search);
|
|
|
+ // Apply search filter on the client side
|
|
|
+ if ($searchTerm !== '') {
|
|
|
+ $allPoints = $allPoints->filter(function (array $record) use ($searchTerm): bool {
|
|
|
+ return Str::contains(Str::lower($record['cn_name'] ?? ''), Str::lower($searchTerm))
|
|
|
+ || Str::contains(Str::lower($record['kp_code'] ?? ''), Str::lower($searchTerm))
|
|
|
+ || Str::contains(Str::lower($record['description'] ?? ''), Str::lower($searchTerm));
|
|
|
});
|
|
|
}
|
|
|
|
|
|
- $meta = $response['meta'] ?? [];
|
|
|
+ // Paginate the results
|
|
|
+ $total = $allPoints->count();
|
|
|
+ $totalPages = (int) ceil($total / $this->perPage);
|
|
|
+ $offset = ($this->page - 1) * $this->perPage;
|
|
|
+ $records = $allPoints->slice($offset, $this->perPage)->values();
|
|
|
|
|
|
return [
|
|
|
- 'data' => $records->values(),
|
|
|
- 'total' => $meta['total'] ?? $records->count(),
|
|
|
- 'page' => $meta['page'] ?? $this->page,
|
|
|
- 'total_pages' => $meta['total_pages'] ?? 1,
|
|
|
- 'has_prev' => $meta['has_prev'] ?? ($this->page > 1),
|
|
|
- 'has_next' => $meta['has_next'] ?? false,
|
|
|
+ 'data' => $records,
|
|
|
+ 'total' => $total,
|
|
|
+ 'page' => $this->page,
|
|
|
+ 'total_pages' => $totalPages,
|
|
|
+ 'has_prev' => $this->page > 1,
|
|
|
+ 'has_next' => $this->page < $totalPages,
|
|
|
];
|
|
|
}
|
|
|
|
|
|
public function getSelectedSkillsProperty(): Collection
|
|
|
{
|
|
|
- return collect($this->selectedPoint['skills'] ?? []);
|
|
|
- }
|
|
|
-
|
|
|
- public function getSelectedPointProperty(): ?array
|
|
|
- {
|
|
|
- return $this->selectedPoint;
|
|
|
+ $selectedPoint = $this->selectedPointData;
|
|
|
+ return collect($selectedPoint['skills'] ?? []);
|
|
|
}
|
|
|
|
|
|
public function getRelatedNodesProperty(): array
|
|
|
{
|
|
|
- $point = $this->selectedPoint;
|
|
|
+ $point = $this->selectedPointData;
|
|
|
|
|
|
if (! $point) {
|
|
|
return ['parents' => [], 'children' => []];
|
|
|
@@ -187,16 +198,4 @@ class KnowledgePoints extends Page
|
|
|
return ['parents' => $parents, 'children' => $children];
|
|
|
}
|
|
|
|
|
|
- protected function hydratePoints(): void
|
|
|
- {
|
|
|
- $this->loading = true;
|
|
|
- $this->selectedPoint = null;
|
|
|
- $points = $this->paginatedPoints['data'];
|
|
|
-
|
|
|
- if ($points->isNotEmpty()) {
|
|
|
- $this->selectPoint($points->first()['kp_code']);
|
|
|
- }
|
|
|
-
|
|
|
- $this->loading = false;
|
|
|
- }
|
|
|
}
|