cms-laravel/app/Livewire/NotificationTable.php

148 lines
4.3 KiB
PHP

<?php
namespace App\Livewire;
use Livewire\Component;
use Illuminate\Support\Facades\Http;
class NotificationTable extends Component
{
public $data = [];
public $total = 0;
public $loading = false;
public $selectedRowKeys = [];
public $searchFilter = '';
public $mounted = false;
public $updating = false;
public $columns = [];
public $currentPage = 1;
public $perPage = 10;
public $sortBy = '';
public $sortOrder = '';
public $filters = [];
public $props = [];
protected $queryString = ['searchFilter', 'currentPage', 'perPage', 'sortBy', 'sortOrder', 'filters'];
public function mount($props)
{
$this->props = $props;
$this->mounted = true;
$this->columns = $props['columns'] ?? [];
$this->fetchData();
}
public function updatedSearchFilter()
{
$this->resetPage();
$this->fetchData();
}
public function setSort($field)
{
if ($this->sortBy === $field) {
$this->sortOrder = $this->sortOrder === 'asc' ? 'desc' : 'asc';
} else {
$this->sortBy = $field;
$this->sortOrder = 'asc';
}
$this->fetchData();
}
public function clearAll()
{
$this->reset(['searchFilter', 'filters', 'sortBy', 'sortOrder', 'selectedRowKeys']);
$this->fetchData();
}
public function updatePage($page)
{
$this->currentPage = $page;
$this->fetchData();
}
public function delete($uuid)
{
try {
$api = env('REACT_APP_STATION_API'); // Ensure this is set in your .env file
$path = substr(request()->path(), 1); // Get current path
$response = Http::delete("{$api}{$path}/{$uuid}");
if ($response->successful()) {
session()->flash('success', 'Record was successfully deleted.');
$this->fetchData();
} else {
session()->flash('error', 'Something went wrong deleting the record.');
}
} catch (\Exception $e) {
session()->flash('error', 'An error occurred: ' . $e->getMessage());
}
}
public function handleBatchDelete()
{
if (empty($this->selectedRowKeys)) return;
try {
$response = Http::delete("your-external-api-endpoint/{$this->props['url']['apiDelete']}", [
'data' => [$this->props['keyValue'] => $this->selectedRowKeys]
]);
if ($response->successful()) {
session()->flash('success', 'Records were successfully deleted.');
$this->selectedRowKeys = [];
$this->fetchData();
} else {
session()->flash('error', 'Something went wrong deleting records.');
}
} catch (\Exception $e) {
session()->flash('error', 'An error occurred: ' . $e->getMessage());
}
}
public function fetchData()
{
$this->loading = true;
$params = array_filter([
'page' => $this->currentPage,
'page_size' => $this->perPage,
'search' => $this->searchFilter,
'sort_by' => $this->sortBy,
'sort_order' => $this->sortOrder,
...$this->filters,
]);
try {
$defaultUrl = $this->props['url']['default'] ?? 'notification';
$response = Http::get("your-external-api-endpoint/{$defaultUrl}", $params);
$data = $response->json();
$this->data = $data['data'] ?? [];
$this->total = $data['total'] ?? 0;
if (empty($this->data) && $this->props['isEmptyMessagePopUp']) {
session()->flash('info', 'No records found.');
}
} catch (\Exception $e) {
session()->flash('error', 'An error occurred while fetching data: ' . $e->getMessage());
$this->data = [];
$this->total = 0;
} finally {
$this->loading = false;
}
}
public function render()
{
return view('livewire.notification-table', [
'columns' => $this->columns,
'keyValue' => $this->props['keyValue'] ?? 'id',
'apiDelete' => $this->props['url']['apiDelete'] ?? false,
'url' => $this->props['url'] ?? [],
]);
}
}