diff --git a/modules/Contribution/Components/FillList.php b/modules/Contribution/Components/FillList.php index 625dd14e..f3b1fc89 100644 --- a/modules/Contribution/Components/FillList.php +++ b/modules/Contribution/Components/FillList.php @@ -2,14 +2,126 @@ namespace Modules\Contribution\Components; +use App\Contribution\ContributionFactory; +use App\Country; +use Illuminate\Support\Collection; +use App\Member\Member; +use Livewire\Attributes\Validate; use Livewire\Component; class FillList extends Component { + #[Validate(rule: 'required|string|max:255', as: 'Veranstaltungs-Name')] + public string $eventName = ''; + #[Validate(rule: 'required|date', as: 'Datum von')] + public Carbon $dateFrom; + #[Validate(rule: 'required|date', as: 'Datum bis')] + public Carbon $dateUntil; + #[Validate(rule: 'required|integer|gt:0', as: 'Land')] + public int $country; + #[Validate(rule: 'array|min:1', as: 'Mitglieder')] + public $members = []; + #[Validate(rule: 'required', as: 'Formular')] + public string $compiler; + #[Validate(rule: 'required', as: 'PLZ / Ort')] + public string $zipLocation; + + public Collection $countries; + public string $search = ''; + public Collection $compilers; + public Collection $memberResults; + + public function rules(): array + { + return [ + 'eventName' => 'required|string|max:255', + ]; + } + + public function mount(): void + { + $this->compilers = app(ContributionFactory::class)->compilerSelect(); + $this->countries = Country::select('name', 'id')->get()->toBase(); + $this->clearSearch(); + } + + /** @todo implement compilation of document */ + public function submit(): void + { + $this->validate(); + } + + public function updatedSearch(): void + { + $this->memberResults = Member::search($this->search, fn ($engine, $query, $options) => $engine->search($query, [ + ...$options, + 'filter' => ['birthday IS NOT NULL', 'address IS NOT EMPTY'] + ]))->get()->toBase(); + } + + public function onSubmitFirstMemberResult(): void + { + if (count($this->memberResults) === 0) { + $this->clearSearch(); + return; + } + + $this->onSubmitMemberResult($this->memberResults[0]->id); + } + + public function onSubmitMemberResult(int $memberId): void + { + if (in_array($memberId, $this->members)) { + $this->members = array_values(array_filter($this->members, fn ($m) => $m !== $memberId)); + } else { + $this->members[] = $memberId; + } + + $this->js('document.querySelector("#search_input").focus()'); + + $this->clearSearch(); + } + + public function clearSearch(): void + { + $this->search = ''; + $this->memberResults = collect([]); + } + public function render() { return <<<'HTML' +
+ + + + + + + + + + +
+ @foreach($memberResults as $member) + + @endforeach +
+
+ + + Formular erstellen +
HTML; } diff --git a/modules/Contribution/Components/FillListTest.php b/modules/Contribution/Components/FillListTest.php index 609f3fa8..df206d17 100644 --- a/modules/Contribution/Components/FillListTest.php +++ b/modules/Contribution/Components/FillListTest.php @@ -22,3 +22,17 @@ it('loads component', function () { Livewire::test(FillList::class) ->assertSee('Zuschüsse'); }); + +it('validates payload', function (array $attributes, array $error) { + Livewire::test(FillList::class) + ->setArray($attributes) + ->call('submit') + ->assertHasErrors($error); +})->with([ + [['eventName' => ''], ['eventName' => 'Veranstaltungs-Name ist erforderlich.']], + [['dateFrom' => ''], ['dateFrom' => 'Datum von ist erforderlich.']], + [['dateUntil' => ''], ['dateUntil' => 'Datum bis ist erforderlich.']], + [['country' => ''], ['country' => 'Land ist erforderlich.']], + [['zipLocation' => ''], ['zipLocation' => 'PLZ / Ort ist erforderlich.']], + [['compiler' => null], ['compiler' => 'Formular ist erforderlich.']], +]);