Skip to content

Commit

Permalink
refactor(dashboard): Let the statuses and layout endpoints use a sane…
Browse files Browse the repository at this point in the history
…r format

Signed-off-by: provokateurin <[email protected]>
  • Loading branch information
provokateurin committed Feb 20, 2024
1 parent 95290bb commit ec1413e
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 28 deletions.
16 changes: 8 additions & 8 deletions apps/dashboard/lib/Controller/DashboardApiController.php
Original file line number Diff line number Diff line change
Expand Up @@ -205,27 +205,27 @@ public function getWidgets(): DataResponse {
* Update the layout
*
* @NoAdminRequired
* @param string $layout The new layout
* @return DataResponse<Http::STATUS_OK, array{layout: string}, array{}>
* @param list<string> $layout The new layout
* @return DataResponse<Http::STATUS_OK, array{layout: list<string>}, array{}>
*
* 200: Statuses updated successfully
*/
public function updateLayout(string $layout): DataResponse {
$this->config->setUserValue($this->userId, 'dashboard', 'layout', $layout);
public function updateLayout(array $layout): DataResponse {
$this->config->setUserValue($this->userId, 'dashboard', 'layout', implode(',', $layout));
return new DataResponse(['layout' => $layout]);
}

/**
* Update the statuses
*
* @NoAdminRequired
* @param string $statuses The new statuses
* @return DataResponse<Http::STATUS_OK, array{statuses: string}, array{}>
* @param list<string> $statuses The new statuses
* @return DataResponse<Http::STATUS_OK, array{statuses: list<string>}, array{}>
*
* 200: Statuses updated successfully
*/
public function updateStatuses(string $statuses): DataResponse {
$this->config->setUserValue($this->userId, 'dashboard', 'statuses', $statuses);
public function updateStatuses(array $statuses): DataResponse {
$this->config->setUserValue($this->userId, 'dashboard', 'statuses', implode(',', $statuses));
return new DataResponse(['statuses' => $statuses]);
}
}
16 changes: 11 additions & 5 deletions apps/dashboard/lib/Controller/DashboardController.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@
*/
namespace OCA\Dashboard\Controller;

use JsonException;
use OCA\Viewer\Event\LoadViewer;
use OCP\AppFramework\Controller;
use OCP\AppFramework\Http;
use OCP\AppFramework\Http\Attribute\OpenAPI;
Expand Down Expand Up @@ -88,7 +90,7 @@ public function index(): TemplateResponse {
\OCP\Util::addScript('dashboard', 'main', 'theming');

$systemDefault = $this->config->getAppValue('dashboard', 'layout', 'recommendations,spreed,mail,calendar');
$userLayout = explode(',', $this->config->getUserValue($this->userId, 'dashboard', 'layout', $systemDefault));
$userLayout = array_filter(explode(',', $this->config->getUserValue($this->userId, 'dashboard', 'layout', $systemDefault)), fn (string $value) => $value !== '');
$widgets = array_map(function (IWidget $widget) {
return [
'id' => $widget->getId(),
Expand All @@ -98,10 +100,14 @@ public function index(): TemplateResponse {
];
}, $this->dashboardManager->getWidgets());
$configStatuses = $this->config->getUserValue($this->userId, 'dashboard', 'statuses', '');
$statuses = json_decode($configStatuses, true);
// We avoid getting an empty array as it will not produce an object in UI's JS
// It does not matter if some statuses are missing from the array, missing ones are considered enabled
$statuses = ($statuses && count($statuses) > 0) ? $statuses : ['weather' => true];
try {
// Parse the old format
$statuses = json_decode($configStatuses, true, 512, JSON_THROW_ON_ERROR);
// We avoid getting an empty array as it will not produce an object in UI's JS
$statuses = array_keys(array_filter($statuses, static fn (bool $value) => $value));
} catch (JsonException $e) {
$statuses = array_filter(explode(',', $configStatuses), fn (string $value) => $value !== '');
}

$this->initialState->provideInitialState('panels', $widgets);
$this->initialState->provideInitialState('statuses', $statuses);
Expand Down
24 changes: 18 additions & 6 deletions apps/dashboard/openapi.json
Original file line number Diff line number Diff line change
Expand Up @@ -448,12 +448,15 @@
],
"parameters": [
{
"name": "layout",
"name": "layout[]",
"in": "query",
"description": "The new layout",
"required": true,
"schema": {
"type": "string"
"type": "array",
"items": {
"type": "string"
}
}
},
{
Expand Down Expand Up @@ -495,7 +498,10 @@
],
"properties": {
"layout": {
"type": "string"
"type": "array",
"items": {
"type": "string"
}
}
}
}
Expand Down Expand Up @@ -526,12 +532,15 @@
],
"parameters": [
{
"name": "statuses",
"name": "statuses[]",
"in": "query",
"description": "The new statuses",
"required": true,
"schema": {
"type": "string"
"type": "array",
"items": {
"type": "string"
}
}
},
{
Expand Down Expand Up @@ -573,7 +582,10 @@
],
"properties": {
"statuses": {
"type": "string"
"type": "array",
"items": {
"type": "string"
}
}
}
}
Expand Down
15 changes: 9 additions & 6 deletions apps/dashboard/src/DashboardApp.vue
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,7 @@ export default {
return (panel) => this.layout.indexOf(panel.id) > -1
},
isStatusActive() {
return (status) => !(status in this.enabledStatuses) || this.enabledStatuses[status]
return (status) => this.enabledStatuses.findIndex((s) => s === status) !== -1
},
sortedAllStatuses() {
Expand Down Expand Up @@ -349,12 +349,12 @@ export default {
},
saveLayout() {
axios.post(generateOcsUrl('/apps/dashboard/layout'), {
layout: this.layout.join(','),
layout: this.layout,
})
},
saveStatuses() {
axios.post(generateOcsUrl('/apps/dashboard/statuses'), {
statuses: JSON.stringify(this.enabledStatuses),
statuses: this.enabledStatuses,
})
},
showModal() {
Expand Down Expand Up @@ -394,13 +394,16 @@ export default {
}
},
enableStatus(app) {
this.enabledStatuses[app] = true
this.enabledStatuses.push(app);
this.registerStatus(app, this.allCallbacksStatus[app])
this.saveStatuses()
},
disableStatus(app) {
this.enabledStatuses[app] = false
const i = this.registeredStatus.findIndex((s) => s === app)
let i = this.enabledStatuses.findIndex((s) => s === app)
if (i !== -1) {
this.enabledStatuses.splice(i, 1)
}
i = this.registeredStatus.findIndex((s) => s === app)
if (i !== -1) {
this.registeredStatus.splice(i, 1)
Vue.set(this.statuses, app, { mounted: false })
Expand Down
4 changes: 2 additions & 2 deletions dist/dashboard-main.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/dashboard-main.js.map

Large diffs are not rendered by default.

0 comments on commit ec1413e

Please sign in to comment.