Enhance timezone support across application

- Introduced a `SetTimezone` middleware to dynamically apply user-specific timezones.
- Added a `timezone chooser` component for users to select their timezone.
- Enhanced date and time display in views with `asDate`, `asTime`, and `asDateTime` methods.
- Updated `AppServiceProvider` to leverage `preventLazyLoading` in local environments and set custom `Carbon` instance for dates.
- Expanded configuration with `user-timezone`.
- Integrated timezone support into meetups and events for consistent scheduling.
This commit is contained in:
HolgerHatGarKeineNode
2025-11-23 19:21:19 +01:00
parent cdf8744883
commit ca9cd9b875
13 changed files with 134 additions and 20 deletions

View File

@@ -9,6 +9,7 @@ class SeoDataAttribute
{ {
public function __construct( public function __construct(
public ?string $key = null, // e.g., 'meetups_index', 'event_show', etc. public ?string $key = null, // e.g., 'meetups_index', 'event_show', etc.
public ?string $image = null, // image url override
) {} ) {}
// Centralized SEO data definitions by key as SEOData instances (lazy initialized) // Centralized SEO data definitions by key as SEOData instances (lazy initialized)

View File

@@ -0,0 +1,36 @@
<?php
namespace App\Http\Middleware;
use Closure;
use Illuminate\Http\Request;
use Symfony\Component\HttpFoundation\Response;
class SetTimezone
{
/**
* Handle an incoming request.
*
* @param \Closure(\Illuminate\Http\Request): (\Symfony\Component\HttpFoundation\Response) $next
*/
public function handle(Request $request, Closure $next): Response
{
if (
$request->user()
&& $timezone = $request->user()->timezone
) {
config([
'app.timezone' => $timezone,
'app.user-timezone' => $timezone,
]);
return $next($request);
}
config([
'app.timezone' => 'Europe/Berlin',
'app.user-timezone' => 'Europe/Berlin',
]);
return $next($request);
}
}

View File

@@ -123,6 +123,7 @@ class Meetup extends Model implements HasMedia
return Attribute::make( return Attribute::make(
get: fn() get: fn()
=> $nextEvent ? [ => $nextEvent ? [
'id' => $nextEvent->id,
'start' => $nextEvent->start, 'start' => $nextEvent->start,
'portalLink' => url()->route('meetups.landingpage-event', 'portalLink' => url()->route('meetups.landingpage-event',
['country' => $this->city->country, 'meetup' => $this, 'event' => $nextEvent]), ['country' => $this->city->country, 'meetup' => $this, 'event' => $nextEvent]),
@@ -139,7 +140,8 @@ class Meetup extends Model implements HasMedia
protected function belongsToMe(): Attribute protected function belongsToMe(): Attribute
{ {
return Attribute::make( return Attribute::make(
get: fn() => DB::table('meetup_user')->where('meetup_id', $this->id)->where('user_id', auth()->id())->exists(), get: fn() => DB::table('meetup_user')->where('meetup_id', $this->id)->where('user_id',
auth()->id())->exists(),
); );
} }

View File

@@ -2,6 +2,9 @@
namespace App\Providers; namespace App\Providers;
use App\Support\Carbon;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\Date;
use Illuminate\Support\ServiceProvider; use Illuminate\Support\ServiceProvider;
class AppServiceProvider extends ServiceProvider class AppServiceProvider extends ServiceProvider
@@ -11,7 +14,9 @@ class AppServiceProvider extends ServiceProvider
*/ */
public function register(): void public function register(): void
{ {
// Date::use(
Carbon::class
);
} }
/** /**
@@ -19,6 +24,6 @@ class AppServiceProvider extends ServiceProvider
*/ */
public function boot(): void public function boot(): void
{ {
// Model::preventLazyLoading(app()->environment('local'));
} }
} }

View File

@@ -14,6 +14,7 @@ return Application::configure(basePath: dirname(__DIR__))
->withMiddleware(function (Middleware $middleware) { ->withMiddleware(function (Middleware $middleware) {
$middleware->web(append: [ $middleware->web(append: [
\Stefro\LaravelLangCountry\Middleware\LangCountrySession::class, \Stefro\LaravelLangCountry\Middleware\LangCountrySession::class,
\App\Http\Middleware\SetTimezone::class,
]); ]);
}) })
->withExceptions(function (Exceptions $exceptions) { ->withExceptions(function (Exceptions $exceptions) {

View File

@@ -67,6 +67,8 @@ return [
'timezone' => 'UTC', 'timezone' => 'UTC',
'user-timezone' => 'UTC',
/* /*
|-------------------------------------------------------------------------- |--------------------------------------------------------------------------
| Application Locale Configuration | Application Locale Configuration

View File

@@ -82,8 +82,15 @@
</flux:navlist> </flux:navlist>
<flux:navlist variant="outline"> <flux:navlist variant="outline">
<flux:navlist.group :heading="__('Land')" class="grid"> <flux:navlist.group :heading="__('Land')">
<div class="grid gap-4">
<div>
<livewire:country.chooser/> <livewire:country.chooser/>
</div>
<div>
<livewire:timezone.chooser/>
</div>
</div>
</flux:navlist.group> </flux:navlist.group>
</flux:navlist> </flux:navlist>

View File

@@ -96,7 +96,7 @@ class extends Component {
{{ $event->meetup->city->name }}, {{ $event->meetup->city->country->name }} {{ $event->meetup->city->name }}, {{ $event->meetup->city->country->name }}
</div> </div>
<flux:badge color="green" size="sm" class="mt-1"> <flux:badge color="green" size="sm" class="mt-1">
{{ $event->start->format('d.m.Y H:i') }} {{ $event->start->asDateTime() }}
</flux:badge> </flux:badge>
</div> </div>
</div> </div>

View File

@@ -100,9 +100,10 @@ class extends Component {
<flux:table.cell> <flux:table.cell>
@if($meetup->nextEvent && $meetup->nextEvent['start']->isFuture()) @if($meetup->nextEvent && $meetup->nextEvent['start']->isFuture())
<a href="{{ route('meetups.landingpage-event', ['meetup' => $meetup, 'event' => $meetup->nextEvent['id'], 'country' => $country]) }}">
<div class="flex flex-col gap-1"> <div class="flex flex-col gap-1">
<flux:badge color="green" size="sm"> <flux:badge color="green" size="sm">
{{ $meetup->nextEvent['start']->format('d.m.Y H:i') }} {{ $meetup->nextEvent['start']->asDateTime() }}
</flux:badge> </flux:badge>
<div class="text-xs text-zinc-500 flex items-center gap-2"> <div class="text-xs text-zinc-500 flex items-center gap-2">
<span>{{ $meetup->nextEvent['attendees'] }} {{ __('Zusagen') }}</span> <span>{{ $meetup->nextEvent['attendees'] }} {{ __('Zusagen') }}</span>
@@ -110,6 +111,7 @@ class extends Component {
<span>{{ $meetup->nextEvent['might_attendees'] }} {{ __('Vielleicht') }}</span> <span>{{ $meetup->nextEvent['might_attendees'] }} {{ __('Vielleicht') }}</span>
</div> </div>
</div> </div>
</a>
@endif @endif
</flux:table.cell> </flux:table.cell>

View File

@@ -163,7 +163,7 @@ class extends Component {
<flux:card class="max-w-3xl"> <flux:card class="max-w-3xl">
<flux:heading size="xl" class="mb-4"> <flux:heading size="xl" class="mb-4">
<flux:icon.calendar class="inline w-6 h-6 mr-2"/> <flux:icon.calendar class="inline w-6 h-6 mr-2"/>
{{ $event->start->format('d.m.Y') }} {{ $event->start->asDateTime() }}
</flux:heading> </flux:heading>
<div class="space-y-4"> <div class="space-y-4">
@@ -171,9 +171,9 @@ class extends Component {
<div class="flex items-center text-zinc-700 dark:text-zinc-300"> <div class="flex items-center text-zinc-700 dark:text-zinc-300">
<flux:icon.clock class="w-5 h-5 mr-3"/> <flux:icon.clock class="w-5 h-5 mr-3"/>
<div> <div>
<div class="font-semibold">{{ $event->start->format('H:i') }} Uhr</div> <div class="font-semibold">{{ $event->start->asTime() }} Uhr</div>
<div <div
class="text-sm text-zinc-600 dark:text-zinc-400">{{ $event->start->isoFormat('dddd, D. MMMM YYYY') }}</div> class="text-sm text-zinc-600 dark:text-zinc-400">{{ $event->start->asDate() }}</div>
</div> </div>
</div> </div>

View File

@@ -215,12 +215,12 @@ class extends Component {
@foreach($events as $event) @foreach($events as $event)
<flux:card size="sm" class="h-full flex flex-col"> <flux:card size="sm" class="h-full flex flex-col">
<flux:heading class="flex items-center gap-2"> <flux:heading class="flex items-center gap-2">
{{ $event->start->format('d.m.Y') }} {{ $event->start->asDate() }}
</flux:heading> </flux:heading>
<flux:text class="mt-2 text-sm text-zinc-600 dark:text-zinc-400"> <flux:text class="mt-2 text-sm text-zinc-600 dark:text-zinc-400">
<flux:icon.clock class="inline w-4 h-4"/> <flux:icon.clock class="inline w-4 h-4"/>
{{ $event->start->format('H:i') }} Uhr {{ $event->start->asTime() }} Uhr
</flux:text> </flux:text>
@if($event->location) @if($event->location)

View File

@@ -115,6 +115,12 @@ class extends Component {
</div> </div>
</form> </form>
<div>
<flux:heading size="lg" class="mb-4">{{ __('Zeitzone') }}</flux:heading>
<flux:subheading class="mb-6">{{ __('Wähle deine Zeitzone aus...') }}</flux:subheading>
<livewire:timezone.chooser :withRedirect="false"/>
</div>
<div class="my-8"> <div class="my-8">
<flux:heading size="lg" class="mb-4">{{ __('Spracheinstellungen') }}</flux:heading> <flux:heading size="lg" class="mb-4">{{ __('Spracheinstellungen') }}</flux:heading>
<flux:subheading class="mb-6">{{ __('Wähle deine Sprache aus...') }}</flux:subheading> <flux:subheading class="mb-6">{{ __('Wähle deine Sprache aus...') }}</flux:subheading>

View File

@@ -0,0 +1,52 @@
<?php
use Livewire\Volt\Component;
use Flux\Flux;
new class extends Component {
public bool $withRedirect = true;
public $currentRouteName;
public $currentRouteParams;
public string $selectedTimezone = 'UTC';
public function mount(): void
{
$this->currentRouteName = request()->route()->getName();
$this->currentRouteParams = request()->route()->parameters();
$this->selectedTimezone = config('app.timezone', 'UTC');
}
public function updatedSelectedTimezone()
{
// Handle timezone change here
// You can emit an event or update user settings
auth()->user()->update([
'timezone' => $this->selectedTimezone,
]);
Flux::toast(text: __('Zeitzone erfolgreich aktualisiert'), heading: __('Zeitzone'), variant: 'success');
if ($this->withRedirect) {
$this->redirectRoute($this->currentRouteName, $this->currentRouteParams, navigate: true);
}
}
public function with(): array
{
return [
'timezones' => \DateTimeZone::listIdentifiers(),
];
}
}; ?>
<div>
<flux:select variant="listbox" searchable placeholder="{{ __('Wähle deine Zeitzone...') }}"
wire:model.live.debounce="selectedTimezone">
<x-slot name="search">
<flux:select.search class="px-4" placeholder="{{ __('Suche Zeitzone...') }}"/>
</x-slot>
@foreach($timezones as $timezone)
<flux:select.option value="{{ $timezone }}">
{{ $timezone }}
</flux:select.option>
@endforeach
</flux:select>
</div>