*/
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);
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
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.
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));
}
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'
+ });
}
}
@include('partials.custom-styles')
@include('partials.custom-head')
+ @stack('head')
</head>
<body class="@yield('body-class')">
<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>
<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>
<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>
@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">
--- /dev/null
+{{--
+$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
\ No newline at end of file
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');
--- /dev/null
+<?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);
+ }
+}
\ No newline at end of file