Skip to content

Commit

Permalink
feat: integrate file list filters and pagination query params WIP
Browse files Browse the repository at this point in the history
Signed-off-by: Victor Bianchi <vbianchi@protonmail.com>
  • Loading branch information
v-bianchi authored and vitormattos committed Oct 31, 2024
1 parent b06e506 commit 5303fda
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 27 deletions.
2 changes: 1 addition & 1 deletion lib/Db/AccountFileMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ private function getUserAccountFile(array $filter = []): Pagination {
$qb->setMaxResults($filter['length']);
}

$pagination = new Pagination($qb);
$pagination = new Pagination($qb, $this->urlGenerator);
return $pagination;
}

Expand Down
2 changes: 1 addition & 1 deletion lib/Db/SignRequestMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -526,7 +526,7 @@ private function getFilesAssociatedFilesWithMeStmt(
return (int)$qb->executeQuery()->fetchOne();
};

$pagination = new Pagination($qb);
$pagination = new Pagination($qb, $this->urlGenerator);
return $pagination;
}

Expand Down
36 changes: 26 additions & 10 deletions lib/Helper/Pagination.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,15 @@
namespace OCA\Libresign\Helper;

use OCA\Libresign\Db\PagerFantaQueryAdapter;
use OCP\IURLGenerator;
use OCP\DB\QueryBuilder\IQueryBuilder;
use Pagerfanta\Pagerfanta;

class Pagination extends Pagerfanta {
/** @var string */
private $rootPath;
private string $routeName;
public function __construct(
IQueryBuilder $queryBuilder,
private IURLGenerator $urlGenerator,
) {
$adapter = new PagerFantaQueryAdapter($queryBuilder);
parent::__construct($adapter);
Expand All @@ -25,20 +26,28 @@ public function __construct(
/**
* @return static
*/
public function setRootPath(string $rootPath = ''): self {
$this->rootPath = $rootPath;
public function setRouteName(string $routeName = ''): self {
$this->routeName = $routeName;
return $this;
}

public function getPagination(?int $page, ?int $length): array {
public function getPagination(?int $page, ?int $length, array $filter = []): array {
$this->setMaxPerPage($length);
$pagination['total'] = $this->count();
if ($pagination['total'] > $length) {
$pagination['current'] = $this->rootPath . '?page=' . $page . '&length=' . $length;
$pagination['next'] = $this->hasNextPage() ? $this->rootPath . '?page=' . $this->getNextPage() . '&length=' . $length : null;
$pagination['prev'] = $this->hasPreviousPage() ? $this->rootPath . '?page=' . $this->getPreviousPage() . '&length=' . $length : null;
$pagination['last'] = $this->hasNextPage() ? $this->rootPath . '?page=' . $this->getNbPages() . '&length=' . $length : null;
$pagination['first'] = $this->hasPreviousPage() ? $this->rootPath . '?page=1&length=' . $length : null;
$pagination['current'] = $this->linkToRoute($page, $length, $filter);
$pagination['next'] = $this->hasNextPage()
? $this->linkToRoute($this->getNextPage(), $length, $filter)
: null;
$pagination['prev'] = $this->hasPreviousPage()
? $this->linkToRoute($this->getPreviousPage(), $length, $filter)
: null;
$pagination['last'] = $this->hasNextPage()
? $this->linkToRoute($this->getNbPages(), $length, $filter)
: null;
$pagination['first'] = $this->hasPreviousPage()
? $this->linkToRoute(1, $length, $filter)
: null;
} else {
$pagination['current'] = null;
$pagination['next'] = null;
Expand All @@ -48,4 +57,11 @@ public function getPagination(?int $page, ?int $length): array {
}
return $pagination;
}

private function linkToRoute(int $page, int $length, array $filter): string {
return $this->urlGenerator->linkToRoute(
$this->routeName,
array_merge(['page' => $page, 'length' => $length, 'apiVersion' => 'v1'], $filter)
);
}
}
4 changes: 2 additions & 2 deletions lib/Service/AccountFileService.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,10 @@ public function accountFileList(array $filter, ?int $page = null, ?int $length =
$page = $page ?? 1;
$length = $length ?? (int)$this->appConfig->getAppValue('length_of_page', '100');
$data = $this->accountFileMapper->accountFileList($filter, $page, $length);
$data['pagination']->setRootPath('/file/list');
$data['pagination']->setRouteName('ocs.libresign.File.list');
return [
'data' => $data['data'],
'pagination' => $data['pagination']->getPagination($page, $length)
'pagination' => $data['pagination']->getPagination($page, $length, $filter)
];
}
}
4 changes: 2 additions & 2 deletions lib/Service/FileService.php
Original file line number Diff line number Diff line change
Expand Up @@ -454,10 +454,10 @@ public function listAssociatedFilesOfSignFlow(
$visibleElements = $this->signRequestMapper->getVisibleElementsFromSigners($signers);
$return['data'] = $this->associateAllAndFormat($this->me, $return['data'], $signers, $identifyMethods, $visibleElements);

$return['pagination']->setRootPath('/file/list');
$return['pagination']->setRouteName('ocs.libresign.File.list');
return [
'data' => $return['data'],
'pagination' => $return['pagination']->getPagination($page, $length)
'pagination' => $return['pagination']->getPagination($page, $length, $filter)
];
}

Expand Down
18 changes: 7 additions & 11 deletions src/store/files.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,9 @@ export const useFilesStore = function(...args) {
filterActive: 'all',
canRequestSign: loadState('libresign', 'can_request_sign', false),
ordered: [],
paginationCurrent: '',
paginationNext: '',
paginationLast: '',
paginationCurrentUrl: '',
paginationNextUrl: '',
paginationLastUrl: '',
paginationLength: '',
}
},
Expand Down Expand Up @@ -245,16 +245,12 @@ export const useFilesStore = function(...args) {
filter.sortDirection = sortingDirection
}

// Build pagination params object
const pagination = {}
if (this.paginationNext) pagination.page = this.paginationNext
if (this.paginationLength) pagination.length = this.paginationNext

// Call API
const response = await axios.get(generateOcsUrl('/apps/libresign/api/v1/file/list'), { params: {...filter, ...pagination} })

const response = this.paginationNextUrl
? await axios.get(this.paginationNextUrl)
: await axios.get(generateOcsUrl('/apps/libresign/api/v1/file/list'), { params: filter })
// Update state according to API response
if (!this.paginationCurrent) {
if (!this.paginationCurrentUrl) {
this.files = {}
this.ordered = []
}
Expand Down

0 comments on commit 5303fda

Please sign in to comment.