Skip to content

Commit

Permalink
Merge pull request #1050 from didoda/refactor/admin-roles-modules+loa…
Browse files Browse the repository at this point in the history
…d-data

RolesModules and Admin index load data
  • Loading branch information
didoda authored Oct 9, 2023
2 parents c58a387 + a2c8914 commit 698a9e4
Show file tree
Hide file tree
Showing 6 changed files with 79 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -127,15 +127,10 @@ export default {
},

methods: {
/**
* load objects using PaginatedContentMixin.getPaginatedObjects()
*
* @return {Promise} resp
*/
async loadObjects() {
async loadObjects(autoload = true, query = {}) {
this.loading = true;

let response = this.getPaginatedObjects();
let response = this.getPaginatedObjects(autoload, query);

response
.then((objs) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ export default {
},
async mounted() {
await this.loadObjects();
await this.loadObjects(true, { page_size:100 });
this.$nextTick(() => {
let groups = this.groups;
if (Object.keys(groups).length === 0) {
Expand Down Expand Up @@ -119,3 +119,9 @@ export default {
},
}
</script>
<style>
.roles-list-view {
height: 50vh;
overflow-y: scroll;
}
</style>
31 changes: 27 additions & 4 deletions src/Controller/Admin/AdministrationBaseController.php
Original file line number Diff line number Diff line change
Expand Up @@ -113,11 +113,8 @@ public function beforeFilter(EventInterface $event): ?Response
public function index(): ?Response
{
$this->getRequest()->allowMethod(['get']);
$query = $this->getRequest()->getQueryParams();

try {
$endpoint = $this->resourceType === 'roles' ? $this->endpoint : sprintf('%s/%s', $this->endpoint, $this->resourceType);
$response = $this->apiClient->get($endpoint, $query);
$response = $this->loadData();
} catch (BEditaClientException $e) {
$this->log($e->getMessage(), 'error');
$this->Flash->error($e->getMessage(), ['params' => $e]);
Expand Down Expand Up @@ -207,4 +204,30 @@ protected function endpoint(): string

return sprintf('%s/%s', $this->endpoint, $this->resourceType);
}

/**
* Get all results iterating over pagination.
*
* @return array
*/
protected function loadData(): array
{
$query = $this->getRequest()->getQueryParams();
$resourceEndpoint = sprintf('%s/%s', $this->endpoint, $this->resourceType);
$endpoint = $this->resourceType === 'roles' ? 'roles' : $resourceEndpoint;
$resultResponse = [];
$pagination = ['page' => 0];
while ($pagination['page'] === 0 || $pagination['page'] < $pagination['page_count']) {
$query['page'] = $pagination['page'] + 1;
$response = (array)$this->apiClient->get($endpoint, $query);
$pagination = (array)Hash::get($response, 'meta.pagination');
foreach ((array)Hash::get($response, 'data') as $data) {
$resultResponse['data'][] = $data;
}
$resultResponse['meta'] = $response['meta'];
$resultResponse['links'] = $response['links'];
}

return $resultResponse;
}
}
3 changes: 3 additions & 0 deletions src/Controller/Admin/RolesModulesController.php
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,9 @@ public function save(): ?Response
}
}
}
$response = (array)$this->apiClient->get('/config', ['filter' => ['name' => 'AccessControl']]);
$content = json_decode((string)Hash::get($response, 'data.0.attributes.content'), true);
$payload = is_array($content) ? array_merge($content, $payload) : $payload;
$this->saveApiConfig(Inflector::camelize('access_control'), $payload);

return $this->redirect(['_name' => 'admin:list:roles_modules']);
Expand Down
25 changes: 11 additions & 14 deletions templates/Pages/Admin/RolesModules/index.twig
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,6 @@
<admin-index inline-template>

<div class="module-index">
{{ Form.create(null, {
'id': 'form-roles-modules',
'url': {'_name': 'admin:save:roles_modules'},
'class': 'table-row',
})|raw }}

<div class="list-objects">

Expand All @@ -20,6 +15,11 @@
{% endif %}

{% for resource in resources %}
{{ Form.create(null, {
'id': 'form-roles-' ~ resource.attributes.name,
'url': {'_name': 'admin:save:roles_modules'},
'class': 'table-row',
})|raw }}
{% set roleName = resource.attributes.name %}

<property-view inline-template :tab-open="tabsOpen" tab-name="roles-modules-{{ resource.attributes.name }}">
Expand Down Expand Up @@ -56,23 +56,20 @@
{{ Form.control(key, radio)|raw }}
</div>
{% endfor %}
<button class="button button-primary button-primary-hover-module-admin is-width-auto mt-2" title="{{ __('Save') }}">
<app-icon icon="carbon:save"></app-icon>
<span class="ml-05">{{ __('Save') }}</span>
</button>
</div>
</div>
</section>

</property-view>

{% endfor %}
</div>
{{ Form.end()|raw }}

<div class="buttons-cell narrow mt-1">
<button class="button button-primary button-primary-hover-module-admin is-width-auto" title="{{ __('Save') }}">
<app-icon icon="carbon:save"></app-icon>
<span class="ml-05">{{ __('Save') }}</span>
</button>
{% endfor %}
</div>

{{ Form.end()|raw }}
</div>

</admin-index>
Original file line number Diff line number Diff line change
Expand Up @@ -346,4 +346,33 @@ public function testEndpoint(): void
$actual = $method->invokeArgs($this->AdministrationBaseController, []);
static::assertEquals('/admin/applications', $actual);
}

/**
* Test `loadData` method
*
* @return void
* @covers ::loadData()
*/
public function testLoadData(): void
{
$this->initRolesController(
[
'environment' => [
'REQUEST_METHOD' => 'GET',
],
'params' => [
'resource_type' => 'roles',
],
'query' => [
'page' => 1,
'page_size' => 1,
],
]
);
$reflectionClass = new \ReflectionClass($this->RlsController);
$method = $reflectionClass->getMethod('loadData');
$method->setAccessible(true);
$actual = $method->invokeArgs($this->RlsController, []);
static::assertNotEmpty($actual);
}
}

0 comments on commit 698a9e4

Please sign in to comment.