refactor(dashboard): Let the statuses and layout endpoints use a saner format

Signed-off-by: provokateurin <kate@provokateurin.de>
pull/42973/head
provokateurin 4 months ago
parent d82fe6c7b4
commit 5f53e446da
No known key found for this signature in database

@ -194,13 +194,13 @@ class DashboardApiController extends OCSController {
* 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]);
}
@ -208,13 +208,13 @@ class DashboardApiController extends OCSController {
* 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]);
}
}

@ -30,6 +30,7 @@ declare(strict_types=1);
*/
namespace OCA\Dashboard\Controller;
use JsonException;
use OCP\AppFramework\Controller;
use OCP\AppFramework\Http;
use OCP\AppFramework\Http\Attribute\OpenAPI;
@ -68,7 +69,7 @@ class DashboardController extends Controller {
\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(),
@ -78,10 +79,14 @@ class DashboardController extends Controller {
];
}, $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);

@ -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"
}
}
},
{
@ -495,7 +498,10 @@
],
"properties": {
"layout": {
"type": "string"
"type": "array",
"items": {
"type": "string"
}
}
}
}
@ -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"
}
}
},
{
@ -573,7 +582,10 @@
],
"properties": {
"statuses": {
"type": "string"
"type": "array",
"items": {
"type": "string"
}
}
}
}

@ -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() {
@ -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() {
@ -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)

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long
Loading…
Cancel
Save