Allow user to switch time zones (#332)

* Description not required

* Overwrite to use user’s time zone

* Allow users to change time zones

* Update app/Filament/Resources/UserResource/Pages/EditProfile.php

Co-authored-by: Boy132 <Boy132@users.noreply.github.com>

* Pint fix

---------

Co-authored-by: Charles <charles@pelican.dev>
Co-authored-by: Boy132 <Boy132@users.noreply.github.com>
This commit is contained in:
Lance Pioch 2024-06-25 20:17:08 -04:00 committed by GitHub
parent fc92a87993
commit cf37994c3b
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 73 additions and 0 deletions

View File

@ -13,6 +13,7 @@ use chillerlan\QRCode\Common\EccLevel;
use chillerlan\QRCode\Common\Version;
use chillerlan\QRCode\QRCode;
use chillerlan\QRCode\QROptions;
use DateTimeZone;
use Filament\Forms\Components\Actions\Action;
use Filament\Forms\Components\Grid;
use Filament\Forms\Components\Placeholder;
@ -85,6 +86,12 @@ class EditProfile extends \Filament\Pages\Auth\EditProfile
->visible(fn (Get $get): bool => filled($get('password')))
->dehydrated(false),
Select::make('timezone')
->required()
->prefixIcon('tabler-clock-pin')
->options(fn () => collect(DateTimeZone::listIdentifiers())->mapWithKeys(fn ($tz) => [$tz => $tz]))
->searchable(),
Select::make('language')
->label(trans('strings.language'))
->required()
@ -193,8 +200,10 @@ class EditProfile extends \Filament\Pages\Auth\EditProfile
->schema([
Grid::make('asdf')->columns(5)->schema([
Section::make('Create API Key')->columnSpan(3)->schema([
TextInput::make('description')
->live(),
TagsInput::make('allowed_ips')
->live()
->splitKeys([',', ' ', 'Tab'])

View File

@ -2,7 +2,11 @@
namespace App\Models;
use Carbon\CarbonInterface;
use DateTimeInterface;
use Illuminate\Support\Arr;
use Illuminate\Support\Carbon;
use Illuminate\Support\Facades\Date;
use Illuminate\Support\Str;
use Illuminate\Validation\Rule;
use Illuminate\Container\Container;
@ -12,6 +16,7 @@ use App\Exceptions\Model\DataValidationException;
use Illuminate\Database\Eloquent\Model as IlluminateModel;
use Illuminate\Validation\Factory as ValidationFactory;
use Illuminate\Validation\Validator;
use InvalidArgumentException;
abstract class Model extends IlluminateModel
{
@ -64,6 +69,36 @@ abstract class Model extends IlluminateModel
return 'uuid';
}
protected function asDateTime($value)
{
$timezone = auth()->user()?->timezone ?? config('app.timezone', 'UTC');
if ($value instanceof CarbonInterface) {
return Date::instance($value->timezone($timezone));
}
if ($value instanceof DateTimeInterface) {
return Date::parse($value->format('Y-m-d H:i:s.u'), $timezone);
}
if (is_numeric($value)) {
return Date::createFromTimestamp($value, $timezone);
}
if ($this->isStandardDateFormat($value)) {
return Date::instance(Carbon::createFromFormat('Y-m-d', $value)->timezone($timezone)->startOfDay());
}
$format = $this->getDateFormat();
try {
$date = Date::createFromFormat($format, $value)->timezone($timezone);
} catch (InvalidArgumentException) {
$date = false;
}
return $date ?: Date::parse($value);
}
/**
* Returns the validator instance used by this model.
*/

View File

@ -123,6 +123,7 @@ class User extends Model implements AuthenticatableContract, AuthorizableContrac
'name_last',
'password',
'language',
'timezone',
'use_totp',
'totp_secret',
'totp_authenticated_at',

View File

@ -0,0 +1,28 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
/**
* Run the migrations.
*/
public function up(): void
{
Schema::table('users', function (Blueprint $table) {
$table->string('timezone')->default('UTC')->after('language');
});
}
/**
* Reverse the migrations.
*/
public function down(): void
{
Schema::table('users', function (Blueprint $table) {
$table->dropColumn('timezone');
});
}
};