Files
mytodos/resources/views/pages/admin/⚡notes-limits.blade.php
ecdown 177f122c10
tests / ci (push) Canceled after 0s
added roles and organizations
2026-10-15 20:59:32 -07:00

202 lines
6.9 KiB
PHP

<?php
use App\Models\User;
use Flux\Flux;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Pagination\LengthAwarePaginator;
use Illuminate\Support\Facades\Gate;
use Livewire\Attributes\Computed;
use Livewire\Attributes\Locked;
use Livewire\Attributes\Title;
use Livewire\Component;
use Livewire\WithPagination;
new #[Title('Notes limits')] class extends Component
{
use WithPagination;
private const PER_PAGE = 10;
public function mount(): void
{
Gate::authorize('accessAdmin');
}
#[Locked]
public ?int $editingUserId = null;
public bool $showLimitForm = false;
public string $notesLimitInput = '';
public string $search = '';
public function updatedSearch(): void
{
$this->resetPage();
}
#[Computed]
public function users(): LengthAwarePaginator
{
return $this->filteredUserQuery()
->orderBy('name')
->paginate(self::PER_PAGE);
}
/**
* @return Builder<User>
*/
private function filteredUserQuery(): Builder
{
return User::query()
->when(
$this->search !== '',
fn (Builder $query) => $query->where(fn (Builder $query) => $query
->where('name', 'like', '%'.$this->search.'%')
->orWhere('email', 'like', '%'.$this->search.'%'))
);
}
public function startEditLimit(int $userId): void
{
$user = $this->userById($userId);
$this->editingUserId = $user->id;
$this->notesLimitInput = (string) $user->notesLimit();
$this->resetErrorBag('notesLimitInput');
$this->showLimitForm = true;
}
public function saveNotesLimit(): void
{
Gate::authorize('accessAdmin');
$user = $this->userById($this->editingUserId);
$validated = $this->validate([
'notesLimitInput' => ['required', 'integer', 'min:1', 'max:1000000'],
]);
$user->update(['notes_limit' => (int) $validated['notesLimitInput']]);
$this->reset('showLimitForm', 'editingUserId', 'notesLimitInput');
Flux::toast(variant: 'success', text: __('Notes limit updated.'));
}
private function userById(int $userId): User
{
return User::query()->findOrFail($userId);
}
}; ?>
<div class="flex h-full w-full flex-1 flex-col gap-6">
<div>
<flux:heading size="xl">{{ __('Notes limits') }}</flux:heading>
<flux:text class="mt-1">{{ __('Set how many characters each user may write in todo notes.') }}</flux:text>
</div>
<div class="flex flex-wrap items-center gap-2">
<flux:input
size="sm"
type="search"
icon="magnifying-glass"
wire:model.live.debounce.300ms="search"
placeholder="{{ __('Search users…') }}"
class="w-full sm:w-64"
data-test="search-input"
/>
@if ($search !== '')
<flux:button
size="sm"
variant="danger"
icon="x-mark"
wire:click="$set('search', '')"
class="shrink-0"
data-test="clear-search"
>
{{ __('Clear') }}
</flux:button>
@endif
</div>
<flux:card>
<flux:table :paginate="$this->users">
<flux:table.columns>
<flux:table.column>{{ __('Name') }}</flux:table.column>
<flux:table.column>{{ __('Email') }}</flux:table.column>
<flux:table.column>{{ __('Site role') }}</flux:table.column>
<flux:table.column>{{ __('Notes limit') }}</flux:table.column>
<flux:table.column align="end" width="5rem">{{ __('Actions') }}</flux:table.column>
</flux:table.columns>
<flux:table.rows>
@forelse ($this->users as $user)
<flux:table.row :key="$user->id" data-test="user-row-{{ $user->id }}">
<flux:table.cell>{{ $user->name }}</flux:table.cell>
<flux:table.cell class="text-zinc-500">{{ $user->email }}</flux:table.cell>
<flux:table.cell>
@if ($user->isSiteAdmin())
<flux:badge size="sm" color="blue">{{ __('Site admin') }}</flux:badge>
@else
<flux:badge size="sm" color="zinc">{{ __('Member') }}</flux:badge>
@endif
</flux:table.cell>
<flux:table.cell data-test="notes-limit-{{ $user->id }}">
{{ $user->notesLimit() }} {{ __('characters') }}
</flux:table.cell>
<flux:table.cell class="py-0">
<div class="flex justify-end gap-1">
<flux:button
size="sm"
variant="ghost"
icon="pencil-square"
:title="__('Edit notes limit')"
wire:click="startEditLimit({{ $user->id }})"
data-test="edit-notes-limit-{{ $user->id }}"
/>
</div>
</flux:table.cell>
</flux:table.row>
@empty
<flux:table.row>
<flux:table.cell colspan="5" align="center" class="py-10">
<flux:text>{{ __('No users match your search.') }}</flux:text>
</flux:table.cell>
</flux:table.row>
@endforelse
</flux:table.rows>
</flux:table>
</flux:card>
<flux:modal wire:model.self="showLimitForm" class="md:w-lg">
<form wire:submit="saveNotesLimit" class="space-y-6">
<div>
<flux:heading size="lg">{{ __('Edit notes limit') }}</flux:heading>
<flux:text class="mt-2">{{ __('Enter the maximum number of characters allowed in todo notes.') }}</flux:text>
</div>
<flux:field>
<flux:label>{{ __('Notes limit (characters)') }}</flux:label>
<flux:input
wire:model="notesLimitInput"
type="number"
min="1"
max="1000000"
inputmode="numeric"
data-test="notes-limit-input"
/>
<flux:error name="notesLimitInput" />
</flux:field>
<div class="flex justify-end gap-3">
<flux:modal.close>
<flux:button variant="ghost">{{ __('Cancel') }}</flux:button>
</flux:modal.close>
<flux:button type="submit" variant="primary">{{ __('Save') }}</flux:button>
</div>
</form>
</flux:modal>
</div>