Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
refactor(dashboard): Let the statuses and layout endpoints use a sane…
…r format

Signed-off-by: provokateurin <[email protected]>
  • Loading branch information
provokateurin committed May 10, 2024
commit 5f53e446da58cbddc6d6a89736c3fe86edc4695c
16 changes: 8 additions & 8 deletions apps/dashboard/lib/Controller/DashboardApiController.php
Original file line number Diff line number Diff line change
Expand Up @@ -194,27 +194,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));

Check notice

Code scanning / Psalm

PossiblyNullArgument

Argument 1 of OCP\IConfig::setUserValue cannot be null, possibly null value provided
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));

Check notice

Code scanning / Psalm

PossiblyNullArgument

Argument 1 of OCP\IConfig::setUserValue cannot be null, possibly null value provided
return new DataResponse(['statuses' => $statuses]);
}
}
15 changes: 10 additions & 5 deletions apps/dashboard/lib/Controller/DashboardController.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
*/
namespace OCA\Dashboard\Controller;

use JsonException;
use OCP\AppFramework\Controller;
use OCP\AppFramework\Http;
use OCP\AppFramework\Http\Attribute\OpenAPI;
Expand Down Expand Up @@ -68,7 +69,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 @@ -78,10 +79,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
17 changes: 10 additions & 7 deletions apps/dashboard/src/DashboardApp.vue
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,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 @@ -350,12 +350,12 @@ export default {
},
saveLayout() {
axios.post(generateOcsUrl('/apps/dashboard/api/v3/layout'), {
layout: this.layout.join(','),
layout: this.layout,
})
},
saveStatuses() {
axios.post(generateOcsUrl('/apps/dashboard/api/v3/statuses'), {
statuses: JSON.stringify(this.enabledStatuses),
statuses: this.enabledStatuses,
})
},
showModal() {
Expand Down Expand Up @@ -395,15 +395,18 @@ 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)
const i = this.enabledStatuses.findIndex((s) => s === app)
if (i !== -1) {
this.registeredStatus.splice(i, 1)
this.enabledStatuses.splice(i, 1)
}
const j = this.registeredStatus.findIndex((s) => s === app)
if (j !== -1) {
this.registeredStatus.splice(j, 1)
Vue.set(this.statuses, app, { mounted: false })
this.$nextTick(() => {
Vue.delete(this.callbacksStatus, app)
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.