Browse Source

Add lang switcher on sidebar

pull/6/head
Nafies Luthfi 8 years ago
parent
commit
1fb60ff524
  1. 17
      app/Http/Controllers/Users/ProfileController.php
  2. 1
      database/factories/ModelFactory.php
  3. 2
      public/assets/css/app.css
  4. 2
      resources/lang/en/customer.php
  5. 14
      resources/views/layouts/partials/sidebar.blade.php
  6. 2
      resources/views/projects/index.blade.php
  7. 6
      routes/web/account.php
  8. 32
      tests/Feature/Users/UserProfileTest.php
  9. 12
      tests/TestCase.php

17
app/Http/Controllers/Users/ProfileController.php

@ -30,7 +30,7 @@ class ProfileController extends Controller
request()->validate([
'name' => 'required|string|max:100',
'email' => 'required|email|max:255',
'lang' => 'required|string',
'lang' => 'required|string|in:en,id',
]);
$user = auth()->user();
@ -40,10 +40,21 @@ class ProfileController extends Controller
$user->lang = request('lang');
$user->save();
session(['lang' => $user->lang]);
flash(trans('auth.profile_updated'), 'success');
return redirect()->route('users.profile.show');
}
public function switchLang()
{
$userData = request()->validate([
'lang' => 'required|string|in:en,id',
]);
$user = request()->user();
$user->lang = $userData['lang'];
$user->save();
return back();
}
}

1
database/factories/ModelFactory.php

@ -13,6 +13,7 @@ $factory->define(User::class, function (Faker\Generator $faker) {
'password' => 'member',
'remember_token' => str_random(10),
'api_token' => str_random(32),
'lang' => 'en',
];
});

2
public/assets/css/app.css

@ -18,4 +18,4 @@
* By Nafies Luthfi
* Code licensed under the Apache License v2.0.
* For details, see http://www.apache.org/licenses/LICENSE-2.0.
*/ol,ul{margin-bottom:0;padding-left:20px}.breadcrumb{margin-top:15px}.login-panel{margin-top:10vh}.checkbox,.radio{margin-top:0;margin-bottom:0}h1.page-header{margin:20px 0}.bold{font-weight:700}.sidebar-logo{width:80px;margin:auto}.nav>li>a{padding:6px 10px;border-color:#eee #eee #ddd}.navbar-right>li>a{padding:15px}.navbar-brand{float:none;border-bottom:1px solid #e7e7e7;display:block;height:auto;padding:10px}label.control-label{color:#428bca}.well .pagination{margin:-2px 0 0 -2px}.panel .lead,.panel .table,div.notifier{margin-bottom:0}@media (min-width:768px){#page-wrapper{margin-left:200px;padding:0;position:relative}nav.hidden-md{display:none}#page-wrapper.page-no-sidebar{margin-left:0}.sidebar{margin-top:0;width:200px;box-shadow:none}.index-search-form{width:50%}}.form-error{color:#d9534f}div.notifier{border-radius:0;position:absolute;left:0;right:0}#footer{padding:15px 10px}#map-canvas-0{min-height:400px}h1.site-tilte{font-size:30px}h3.site-description{font-size:16px;font-weight:400;margin:0}.guest-header{padding:0 10px}@media (max-width:480px){.guest-header{text-align:center}}.nav .nav-second-level li.active{text-decoration:none}.form-control{height:30px;padding:3px 6px}@media print{#page-wrapper{margin-left:0}}.strong{font-weight:bold}
*/ol,ul{margin-bottom:0;padding-left:20px}.breadcrumb{margin-top:15px}.login-panel{margin-top:10vh}.checkbox,.radio{margin-top:0;margin-bottom:0}h1.page-header{margin:20px 0}.bold{font-weight:700}.sidebar-logo{width:80px;margin:auto}.nav>li>a{padding:6px 10px;border-color:#eee #eee #ddd}.navbar-right>li>a{padding:15px}.navbar-brand{float:none;display:block;height:auto;padding:10px}label.control-label{color:#428bca}.well .pagination{margin:-2px 0 0 -2px}.panel .lead,.panel .table,div.notifier{margin-bottom:0}@media (min-width:768px){#page-wrapper{margin-left:200px;padding:0;position:relative}nav.hidden-md{display:none}#page-wrapper.page-no-sidebar{margin-left:0}.sidebar{margin-top:0;width:200px;box-shadow:none}.index-search-form{width:50%}}.form-error{color:#d9534f}div.notifier{border-radius:0;position:absolute;left:0;right:0}#footer{padding:15px 10px}#map-canvas-0{min-height:400px}h1.site-tilte{font-size:30px}h3.site-description{font-size:16px;font-weight:400;margin:0}.guest-header{padding:0 10px}@media (max-width:480px){.guest-header{text-align:center}}.nav .nav-second-level li.active{text-decoration:none}.form-control{height:30px;padding:3px 6px}@media print{#page-wrapper{margin-left:0}}.strong{font-weight:bold}

2
resources/lang/en/customer.php

@ -2,7 +2,7 @@
return [
// Labels
'master' => 'Customer',
'customer' => 'Customer',
'list' => 'Customer List',
'search' => 'Search Customer',
'detail' => 'Customer Detail',

14
resources/views/layouts/partials/sidebar.blade.php

@ -13,6 +13,20 @@
{{ appLogoImage(['class' => 'sidebar-logo']) }}
<div class="small" style="margin-top:10px">{{ config('app.name') }}</div>
</a>
<div class="text-center" style="border-bottom: 1px solid #e7e7e7; padding-bottom: 10px">
{!! FormField::formButton(
['route' => 'users.profile.switch-lang', 'method' => 'patch'],
'EN',
['class' => 'btn btn-default btn-xs', 'id' => 'switch_lang_en'],
['lang' => 'en']
) !!}
{!! FormField::formButton(
['route' => 'users.profile.switch-lang', 'method' => 'patch'],
'ID',
['class' => 'btn btn-default btn-xs', 'id' => 'switch_lang_id'],
['lang' => 'id']
) !!}
</div>
<ul class="nav" id="side-menu">
<li>{!! html_link_to_route('home', trans('nav_menu.dashboard'), [], ['icon' => 'dashboard']) !!}</li>
@can('manage_agency')

2
resources/views/projects/index.blade.php

@ -45,7 +45,7 @@
</tr>
@empty
<tr>
<td colspan="5">{{ $status }} {{ trans('project.not_found') }}</td>
<td colspan="9">{{ $status }} {{ trans('project.not_found') }}</td>
</tr>
@endforelse
</tbody>

6
routes/web/account.php

@ -41,6 +41,12 @@ Route::patch('profile/update', [
'middleware' => ['web', 'auth'],
]);
Route::patch('profile/switch-lang', [
'as' => 'users.profile.switch-lang',
'uses' => 'Users\ProfileController@switchLang',
'middleware' => ['web', 'auth'],
]);
// User's Agency routes
Route::get('agency', [
'as' => 'users.agency.show',

32
tests/Feature/Users/UserProfileTest.php

@ -49,4 +49,36 @@ class UserProfileTest extends TestCase
'lang' => 'en',
]);
}
/** @test */
public function user_get_locale_bases_on_their_lang_profile_value()
{
$user = $this->userSigningIn(['lang' => 'en']);
$this->visit(route('home'));
$this->assertEquals('en', app()->getLocale());
$user = $this->userSigningIn(['lang' => 'id']);
$this->visit(route('home'));
$this->assertEquals('id', app()->getLocale());
}
/** @test */
public function user_can_switch_lang_from_sidebar()
{
$user = $this->userSigningIn(['lang' => 'id']);
$this->visit('/');
$this->submitForm('switch_lang_en', ['lang' => 'en']);
$this->assertEquals('en', app()->getLocale());
$this->assertEquals('en', $user->fresh()->lang);
$this->submitForm('switch_lang_id', ['lang' => 'id']);
$this->assertEquals('id', app()->getLocale());
$this->assertEquals('id', $user->fresh()->lang);
}
}

12
tests/TestCase.php

@ -20,25 +20,25 @@ abstract class TestCase extends BaseTestCase
}
}
protected function adminUserSigningIn()
protected function adminUserSigningIn($userDataOverrides = [])
{
$user = $this->createUser();
$user = $this->createUser('admin', $userDataOverrides);
$this->actingAs($user);
return $user;
}
protected function userSigningIn()
protected function userSigningIn($userDataOverrides = [])
{
$user = $this->createUser('worker');
$user = $this->createUser('worker', $userDataOverrides);
$this->actingAs($user);
return $user;
}
protected function createUser($role = 'admin')
protected function createUser($role = 'admin', $userDataOverrides = [])
{
$user = factory(User::class)->create();
$user = factory(User::class)->create($userDataOverrides);
$user->assignRole($role);
return $user;

Loading…
Cancel
Save