Skip to content

Commit

Permalink
Add preference for theme (#2473)
Browse files Browse the repository at this point in the history
* add user preference for theme

* fix coalesce expression

* fix css selectors

* Run php-cs-fixer

* fix node build

* remove global install of node-gyp

---------

Co-authored-by: emmachughes <emmachughes@users.noreply.github.com>
  • Loading branch information
emmachughes and emmachughes authored Sep 1, 2023
1 parent 0f179a8 commit 80fefe0
Show file tree
Hide file tree
Showing 13 changed files with 653 additions and 5 deletions.
2 changes: 0 additions & 2 deletions sourcecode/hub/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,6 @@ FROM node:16-alpine AS frontend

WORKDIR /app

RUN npm i -g npm node-gyp

COPY package.json package-lock.json ./
RUN npm ci

Expand Down
52 changes: 52 additions & 0 deletions sourcecode/hub/app/Configuration/Themes.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?php

declare(strict_types=1);

namespace App\Configuration;

final readonly class Themes
{
/**
* @return array<string>
*/
public function all(): array
{
return [
'edlib',
'light',
'dark',
];
}

public function getName(string $theme, string $locale): string|null
{
return match ($theme) {
'edlib' => 'Edlib',
'light' => match ($locale) {
'nb', 'no' => 'Bootstrap lys',
default => 'Bootstrap Light',
},
'dark' => match ($locale) {
'nb', 'no' => 'Bootstrap mørk',
default => 'Bootstrap Dark',
},
default => null,
};
}

public function getDefault(): string
{
return 'edlib';
}

/**
* @return array<string, string>
*/
public function getTranslatedMap(string $locale): array
{
return array_combine($this->all(), array_map(
fn (string $key) => $this->getName($key, $locale) ?? $key,
$this->all(),
));
}
}
4 changes: 3 additions & 1 deletion sourcecode/hub/app/Http/Controllers/UserController.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace App\Http\Controllers;

use App\Configuration\Locales;
use App\Configuration\Themes;
use App\Http\Requests\SavePreferencesRequest;
use App\Http\Requests\StoreUserRequest;
use App\Http\Requests\UpdateUserRequest;
Expand Down Expand Up @@ -44,10 +45,11 @@ public function store(StoreUserRequest $request): RedirectResponse
return to_route('home');
}

public function preferences(Locales $locales): View
public function preferences(Locales $locales, Themes $themes): View
{
return view('user.preferences', [
'locales' => $locales->getTranslatedMap(app()->getLocale()),
'themes' => $themes->getTranslatedMap(app()->getLocale()),
'user' => $this->getUser(),
]);
}
Expand Down
8 changes: 7 additions & 1 deletion sourcecode/hub/app/Http/Requests/SavePreferencesRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace App\Http\Requests;

use App\Configuration\Locales;
use App\Configuration\Themes;
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Validation\Rule;

Expand All @@ -17,15 +18,20 @@ protected function prepareForValidation(): void
if (!$parameters->has('debug_mode')) {
$parameters->set('debug_mode', false);
}

if (!$parameters->get('theme')) {
$parameters->set('theme', null);
}
}

/**
* @return mixed[]
*/
public function rules(Locales $locales): array
public function rules(Locales $locales, Themes $themes): array
{
return [
'locale' => ['required', Rule::in($locales->all())],
'theme' => ['sometimes', 'nullable', Rule::in($themes->all())],
'debug_mode' => ['required', 'boolean'],
];
}
Expand Down
1 change: 1 addition & 0 deletions sourcecode/hub/app/Models/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ class User extends Model implements AuthenticatableContract
'email',
'google_id',
'facebook_id',
'theme',
];

protected $hidden = [
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class () extends Migration {
public function up(): void
{
Schema::table('users', function (Blueprint $table) {
$table->string('theme')->nullable();
});
}

public function down(): void
{
Schema::table('users', function (Blueprint $table) {
$table->dropColumn('theme');
});
}
};
2 changes: 2 additions & 0 deletions sourcecode/hub/lang/en/messages.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,4 +64,6 @@
'disconnect-google' => 'Disconnect from Google',
'disconnect-facebook' => 'Disconnect from Facebook',
'alert-password-empty' => 'To disconnect social login providers, you must first set a password on your account',
'theme' => 'Theme',
'default' => 'Default',
];
2 changes: 2 additions & 0 deletions sourcecode/hub/lang/nb/messages.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,6 @@
'debug-mode-form-help' => 'Feilsøkingsmodus vil avsløre tekniske detaljer som kan være behjelpelige under feilsøking eller utvikling av innholdstyper.',
'version-history' => 'Versjonshistorikk',
'published' => 'Publisert',
'theme' => 'Tema',
'default' => 'Standard',
];
Loading

0 comments on commit 80fefe0

Please sign in to comment.