From a4937bb6e9bf124fff21935dcddadf7366144827 Mon Sep 17 00:00:00 2001 From: philipp lang Date: Thu, 1 Feb 2024 01:58:40 +0100 Subject: [PATCH] Add event fetching --- assets/vendor/adrema-form | 2 +- components/EventIndex.php | 22 ++++++++++++++++++---- components/eventindex/default.htm | 12 +++++++++++- lang/de.json | 2 +- lang/en.json | 5 +++-- routes.php | 9 ++++----- support/FetchAllEvents.php | 21 +++++++++++++++++++++ 7 files changed, 59 insertions(+), 14 deletions(-) create mode 100644 support/FetchAllEvents.php diff --git a/assets/vendor/adrema-form b/assets/vendor/adrema-form index e629345..10070f3 160000 --- a/assets/vendor/adrema-form +++ b/assets/vendor/adrema-form @@ -1 +1 @@ -Subproject commit e62934504bf2d33b65d0282a8d311df9cecb178e +Subproject commit 10070f39ad7b91dee37f953325726e8b56694c6c diff --git a/components/EventIndex.php b/components/EventIndex.php index 4d8141f..93bacd1 100644 --- a/components/EventIndex.php +++ b/components/EventIndex.php @@ -4,8 +4,10 @@ namespace Silva\Adrema\Components; use Cms\Classes\ComponentBase; use Cms\Classes\Page; +use FetchAllEvents; use Silva\Adrema\Exceptions\ComponentException; use Silva\Adrema\Models\Settings; +use Silva\Adrema\Support\FetchAllEvents as SupportFetchAllEvents; class EventIndex extends ComponentBase { @@ -13,7 +15,9 @@ class EventIndex extends ComponentBase public $settings; /** @var string The active event */ - public ?string $event; + public ?string $eventSlug; + public ?array $event; + public string $currentUrl; public function componentDetails() { @@ -26,12 +30,22 @@ class EventIndex extends ComponentBase public function onRun() { $this->addJs('assets/vendor/adrema-form/dist/main.js'); + $this->currentUrl = url()->current(); + // $this->addJs('http://localhost:5174/src/main.js', ['type' => 'module']); $this->settings = [ 'primary_color' => Settings::get('primary_color'), ...$this->resolvePageUrls(), ]; - $this->event = $this->property('event'); + $this->eventSlug = $this->property('eventSlug'); + + if ($this->eventSlug) { + $events = data_get(app(SupportFetchAllEvents::class)->run(), 'data'); + throw_if(is_null($events), ComponentException::class, 'event_fetching_failed'); + + $this->event = collect($events)->first(fn ($event) => $event['slug'] === $this->eventSlug); + $this->page->title = $this->event['name']; + } } /** @@ -81,8 +95,8 @@ class EventIndex extends ComponentBase 'type' => 'dropdown', 'default' => null, ], - 'event' => [ - 'title' => __('properties.event_title'), + 'eventSlug' => [ + 'title' => __('properties.event_slug_title'), 'type' => 'text', 'default' => null, ], diff --git a/components/eventindex/default.htm b/components/eventindex/default.htm index 4bf8120..1ddf708 100644 --- a/components/eventindex/default.htm +++ b/components/eventindex/default.htm @@ -1,8 +1,18 @@ +{% if __SELF__.event %} + {% put metatags %} + + + + + + {% endput %} +{% endif %} + diff --git a/lang/de.json b/lang/de.json index 60d436f..f09fc8b 100644 --- a/lang/de.json +++ b/lang/de.json @@ -5,5 +5,5 @@ "properties.index_page_title": "Seite für Übersicht", "properties.single_page_title": "Seite für Einzelansicht", "properties.register_page_title": "Seite zum anmelden", - "properties.event_title": "Veranstaltung slug" + "properties.event_slug_title": "Veranstaltung slug" } diff --git a/lang/en.json b/lang/en.json index 7f83962..499f36b 100644 --- a/lang/en.json +++ b/lang/en.json @@ -5,8 +5,9 @@ "properties.index_page_title": "Page for overview", "properties.single_page_title": "Page for single view", "properties.register_page_title": "Page for registration", - "properties.event_title": "Event slug", + "properties.event_slug_title": "Event slug", "errors.not_all_pages_set": "You didn't assign all pages. Please edit your components.", "errors.page_not_found": "One page cannot be found. Event component cannot be rendered.", - "errors.slug_not_found": "You need to set a placeholder in page urls to set the event slug (\":slug\")" + "errors.slug_not_found": "You need to set a placeholder in page urls to set the event slug (\":slug\")", + "errors.event_fetching_failed": "Failed to fetch event." } diff --git a/routes.php b/routes.php index 67e0fbf..dd91ac3 100644 --- a/routes.php +++ b/routes.php @@ -1,14 +1,13 @@ run(); - if (!$response->ok()) { + if (is_null($events)) { return Response::json(['message' => 'Fehler beim Laden der Veranstaltungen'], 422); } - return Response::json($response->json()); + return Response::json($events); })->middleware('api'); diff --git a/support/FetchAllEvents.php b/support/FetchAllEvents.php new file mode 100644 index 0000000..e5ce7a7 --- /dev/null +++ b/support/FetchAllEvents.php @@ -0,0 +1,21 @@ +ok()) { + return null; + } + + return $response->json(); + } +}