Updated view toggle to store date

Also added test for user list order preferences
This commit is contained in:
Dan Brown 2019-04-14 13:01:51 +01:00
parent 01be72d5e2
commit 9406b4d4c9
No known key found for this signature in database
GPG Key ID: 46D9F943C24A2EF9
11 changed files with 155 additions and 20 deletions

View File

@ -299,7 +299,6 @@ class UserController extends Controller
*/
public function changeSort(string $id, string $type, Request $request)
{
// TODO - Test this endpoint
$validSortTypes = ['books', 'bookshelves'];
if (!in_array($type, $validSortTypes)) {
return redirect()->back(500);
@ -307,6 +306,28 @@ class UserController extends Controller
return $this->changeListSort($id, $request, $type);
}
/**
* Update the stored section expansion preference for the given user.
* @param string $id
* @param string $key
* @param Request $request
* @return \Illuminate\Contracts\Routing\ResponseFactory|\Symfony\Component\HttpFoundation\Response
*/
public function updateExpansionPreference(string $id, string $key, Request $request)
{
$this->checkPermissionOrCurrentUser('users-manage', $id);
$keyWhitelist = ['home-details'];
if (!in_array($key, $keyWhitelist)) {
return response("Invalid key", 500);
}
$newState = $request->get('expand', 'false');
$user = $this->user->findOrFail($id);
setting()->putUser($user, 'section_expansion#' . $key, $newState);
return response("", 204);
}
/**
* Changed the stored preference for a list sort order.
* @param int $userId

View File

@ -67,6 +67,17 @@ class SettingService
return $this->get($this->userKey($user->id, $key), $default);
}
/**
* Get a value for the current logged-in user.
* @param $key
* @param bool $default
* @return bool|string
*/
public function getForCurrentUser($key, $default = false)
{
return $this->getUser(auth()->user(), $key, $default);
}
/**
* Gets a setting value from the cache or database.
* Looks at the system defaults if not cached or in database.

View File

@ -3,8 +3,13 @@ class ExpandToggle {
constructor(elem) {
this.elem = elem;
this.isOpen = false;
// Component state
this.isOpen = elem.getAttribute('expand-toggle-is-open') === 'yes';
this.updateEndpoint = elem.getAttribute('expand-toggle-update-endpoint');
this.selector = elem.getAttribute('expand-toggle');
// Listener setup
elem.addEventListener('click', this.click.bind(this));
}
@ -53,11 +58,20 @@ class ExpandToggle {
click(event) {
event.preventDefault();
let matchingElems = document.querySelectorAll(this.selector);
for (let i = 0, len = matchingElems.length; i < len; i++) {
this.isOpen ? this.close(matchingElems[i]) : this.open(matchingElems[i]);
const matchingElems = document.querySelectorAll(this.selector);
for (let match of matchingElems) {
this.isOpen ? this.close(match) : this.open(match);
}
this.isOpen = !this.isOpen;
this.updateSystemAjax(this.isOpen);
}
updateSystemAjax(isOpen) {
window.$http.patch(this.updateEndpoint, {
expand: isOpen ? 'true' : 'false'
});
}
}

View File

@ -20,6 +20,7 @@
@include('partials.custom-styles')
@include('partials.custom-head')
@stack('head')
</head>
<body class="@yield('body-class')">

View File

@ -9,10 +9,7 @@
<h5>{{ trans('common.actions') }}</h5>
<div class="icon-list text-primary">
@include('partials.view-toggle', ['view' => $view, 'type' => 'book'])
<a expand-toggle=".entity-list.compact .entity-item-snippet" class="icon-list-item">
<span>@icon('expand-text')</span>
<span>{{ trans('common.toggle_details') }}</span>
</a>
@include('components.expand-toggle', ['target' => '.entity-list.compact .entity-item-snippet', 'key' => 'home-details'])
</div>
</div>

View File

@ -8,10 +8,7 @@
<div class="actions mb-xl">
<h5>{{ trans('common.actions') }}</h5>
<div class="icon-list text-primary">
<a expand-toggle=".entity-list.compact .entity-item-snippet" class="icon-list-item">
<span>@icon('expand-text')</span>
<span>{{ trans('common.toggle_details') }}</span>
</a>
@include('components.expand-toggle', ['target' => '.entity-list.compact .entity-item-snippet', 'key' => 'home-details'])
</div>
</div>

View File

@ -9,10 +9,7 @@
<h5>{{ trans('common.actions') }}</h5>
<div class="icon-list text-primary">
@include('partials.view-toggle', ['view' => $view, 'type' => 'shelf'])
<a expand-toggle=".entity-list.compact .entity-item-snippet" class="icon-list-item">
<span>@icon('expand-text')</span>
<span>{{ trans('common.toggle_details') }}</span>
</a>
@include('components.expand-toggle', ['target' => '.entity-list.compact .entity-item-snippet', 'key' => 'home-details'])
</div>
</div>

View File

@ -1,10 +1,11 @@
@extends('simple-layout')
@section('body')
<div class="container px-xl py-l">
<a expand-toggle=".entity-list.compact .entity-item-snippet" class="text-muted">@icon('expand-text'){{ trans('common.toggle_details') }}</a>
<div class="container px-xl py-s">
<div class="icon-list inline block">
@include('components.expand-toggle', ['target' => '.entity-list.compact .entity-item-snippet', 'key' => 'home-details'])
</div>
</div>
<div class="container" id="home-default">

View File

@ -0,0 +1,19 @@
{{--
$target - CSS selector of items to expand
$key - Unique key for checking existing stored state.
--}}
<?php $isOpen = setting()->getForCurrentUser('section_expansion#'. $key); ?>
<a expand-toggle="{{ $target }}"
expand-toggle-update-endpoint="{{ baseUrl('/settings/users/'. auth()->user()->id .'/update-expansion-preference/' . $key) }}"
expand-toggle-is-open="{{ $isOpen ? 'yes' : 'no' }}"
class="text-muted icon-list-item text-primary">
<span>@icon('expand-text')</span>
<span>{{ trans('common.toggle_details') }}</span>
</a>
@if($isOpen)
@push('head')
<style>
{{ $target }} {display: block;}
</style>
@endpush
@endif

View File

@ -177,6 +177,7 @@ Route::group(['middleware' => 'auth'], function () {
Route::patch('/users/{id}/switch-book-view', 'UserController@switchBookView');
Route::patch('/users/{id}/switch-shelf-view', 'UserController@switchShelfView');
Route::patch('/users/{id}/change-sort/{type}', 'UserController@changeSort');
Route::patch('/users/{id}/update-expansion-preference/{key}', 'UserController@updateExpansionPreference');
Route::post('/users/create', 'UserController@store');
Route::get('/users/{id}', 'UserController@edit');
Route::put('/users/{id}', 'UserController@update');

View File

@ -0,0 +1,76 @@
<?php namespace Tests;
class UserPreferencesTest extends TestCase
{
public function test_update_sort_preference()
{
$editor = $this->getEditor();
$this->actingAs($editor);
$updateRequest = $this->patch('/settings/users/' . $editor->id.'/change-sort/books', [
'sort' => 'created_at',
'order' => 'desc'
]);
$updateRequest->assertStatus(302);
$this->assertDatabaseHas('settings', [
'setting_key' => 'user:' . $editor->id . ':books_sort',
'value' => 'created_at'
]);
$this->assertDatabaseHas('settings', [
'setting_key' => 'user:' . $editor->id . ':books_sort_order',
'value' => 'desc'
]);
$this->assertEquals('created_at', setting()->getForCurrentUser('books_sort'));
$this->assertEquals('desc', setting()->getForCurrentUser('books_sort_order'));
}
public function test_update_sort_preference_defaults()
{
$editor = $this->getEditor();
$this->actingAs($editor);
$updateRequest = $this->patch('/settings/users/' . $editor->id.'/change-sort/bookshelves', [
'sort' => 'cat',
'order' => 'dog'
]);
$updateRequest->assertStatus(302);
$this->assertEquals('name', setting()->getForCurrentUser('bookshelves_sort'));
$this->assertEquals('asc', setting()->getForCurrentUser('bookshelves_sort_order'));
}
public function test_update_sort_bad_entity_type_handled()
{
$editor = $this->getEditor();
$this->actingAs($editor);
$updateRequest = $this->patch('/settings/users/' . $editor->id.'/change-sort/dogs', [
'sort' => 'name',
'order' => 'asc'
]);
$updateRequest->assertStatus(500);
$this->assertNotEmpty('name', setting()->getForCurrentUser('bookshelves_sort'));
$this->assertNotEmpty('asc', setting()->getForCurrentUser('bookshelves_sort_order'));
}
public function test_update_expansion_preference()
{
$editor = $this->getEditor();
$this->actingAs($editor);
$updateRequest = $this->patch('/settings/users/' . $editor->id.'/update-expansion-preference/home-details', ['expand' => 'true']);
$updateRequest->assertStatus(204);
$this->assertDatabaseHas('settings', [
'setting_key' => 'user:' . $editor->id . ':section_expansion#home-details',
'value' => 'true'
]);
$this->assertEquals(true, setting()->getForCurrentUser('section_expansion#home-details'));
$invalidKeyRequest = $this->patch('/settings/users/' . $editor->id.'/update-expansion-preference/my-home-details', ['expand' => 'true']);
$invalidKeyRequest->assertStatus(500);
}
}