From 57981f1cf950a0cceae6e1a9508661a858d89df7 Mon Sep 17 00:00:00 2001 From: James Cole Date: Sun, 24 Mar 2024 11:08:24 +0100 Subject: [PATCH] Read and remember sort order by URL --- .../UserGroups/Account/AccountRepository.php | 28 ++++++++++--------- resources/assets/v2/pages/accounts/index.js | 25 ++++++++++++++--- 2 files changed, 36 insertions(+), 17 deletions(-) diff --git a/app/Repositories/UserGroups/Account/AccountRepository.php b/app/Repositories/UserGroups/Account/AccountRepository.php index 53e8322ee8..d262966a94 100644 --- a/app/Repositories/UserGroups/Account/AccountRepository.php +++ b/app/Repositories/UserGroups/Account/AccountRepository.php @@ -63,8 +63,7 @@ class AccountRepository implements AccountRepositoryInterface $q1->where('account_meta.name', '=', 'account_number'); $q1->where('account_meta.data', '=', $json); } - ) - ; + ); if (0 !== count($types)) { $dbQuery->leftJoin('account_types', 'accounts.account_type_id', '=', 'account_types.id'); @@ -90,7 +89,7 @@ class AccountRepository implements AccountRepositoryInterface public function findByName(string $name, array $types): ?Account { - $query = $this->userGroup->accounts(); + $query = $this->userGroup->accounts(); if (0 !== count($types)) { $query->leftJoin('account_types', 'accounts.account_type_id', '=', 'account_types.id'); @@ -114,8 +113,8 @@ class AccountRepository implements AccountRepositoryInterface public function getAccountCurrency(Account $account): ?TransactionCurrency { - $type = $account->accountType->type; - $list = config('firefly.valid_currency_account_types'); + $type = $account->accountType->type; + $list = config('firefly.valid_currency_account_types'); // return null if not in this list. if (!in_array($type, $list, true)) { @@ -248,7 +247,11 @@ class AccountRepository implements AccountRepositoryInterface } // add sort parameters. At this point they're filtered to allowed fields to sort by: + $hasActiveColumn = array_key_exists('active', $sort); if (count($sort) > 0) { + if (false === $hasActiveColumn) { + $query->orderBy('accounts.active', 'DESC'); + } foreach ($sort as $column => $direction) { if (in_array($column, $sortable, true)) { $query->orderBy(sprintf('accounts.%s', $column), $direction); @@ -258,9 +261,9 @@ class AccountRepository implements AccountRepositoryInterface if (0 === count($sort)) { if (0 !== count($res)) { - $query->orderBy('accounts.order', 'ASC'); + $query->orderBy('accounts.active', 'DESC'); } - $query->orderBy('accounts.active', 'DESC'); + $query->orderBy('accounts.order', 'ASC'); $query->orderBy('accounts.name', 'ASC'); } @@ -271,12 +274,11 @@ class AccountRepository implements AccountRepositoryInterface { // search by group, not by user $dbQuery = $this->userGroup->accounts() - ->where('active', true) - ->orderBy('accounts.order', 'ASC') - ->orderBy('accounts.account_type_id', 'ASC') - ->orderBy('accounts.name', 'ASC') - ->with(['accountType']) - ; + ->where('active', true) + ->orderBy('accounts.order', 'ASC') + ->orderBy('accounts.account_type_id', 'ASC') + ->orderBy('accounts.name', 'ASC') + ->with(['accountType']); if ('' !== $query) { // split query on spaces just in case: $parts = explode(' ', $query); diff --git a/resources/assets/v2/pages/accounts/index.js b/resources/assets/v2/pages/accounts/index.js index 050006d5a1..47557e1bee 100644 --- a/resources/assets/v2/pages/accounts/index.js +++ b/resources/assets/v2/pages/accounts/index.js @@ -32,9 +32,22 @@ import Put from "../../api/v2/model/account/put.js"; import AccountRenderer from "../../support/renderers/AccountRenderer.js"; // set type from URL -const urlParts = window.location.href.split('/'); +const beforeQuery = window.location.href.split('?'); +const urlParts = beforeQuery[0].split('/'); const type = urlParts[urlParts.length - 1]; +let sortingColumn = ''; +let sortDirection = ''; + +// get sort parameters +const params = new Proxy(new URLSearchParams(window.location.search), { + get: (searchParams, prop) => searchParams.get(prop), +}); +// Get the value of "some_key" in eg "https://example.com/?some_key=some_value" +sortingColumn = params.column ?? ''; +sortDirection = params.direction ?? ''; + + let index = function () { return { // notifications @@ -54,8 +67,8 @@ let index = function () { }, }, editors: {}, - sortingColumn: '', - sortDirection: '', + sortingColumn: sortingColumn, + sortDirection: sortDirection, accounts: [], accountRole(roleName) { @@ -65,6 +78,10 @@ let index = function () { sort(column) { this.sortingColumn = column; this.sortDirection = this.sortDirection === 'asc' ? 'desc' : 'asc'; + const url = './accounts/'+type+'?column='+column+'&direction='+this.sortDirection; + + window.history.pushState({}, "", url); + this.loadAccounts(); return false; }, @@ -130,7 +147,7 @@ let index = function () { // sort instructions // &sorting[0][column]=description&sorting[0][direction]=asc const sorting = [{column: this.sortingColumn, direction: this.sortDirection}]; - // one page only. + // one page only.o (new Get()).index({sorting: sorting, type: type, page: this.page}).then(response => { for (let i = 0; i < response.data.data.length; i++) { if (response.data.data.hasOwnProperty(i)) {