From d3e962fadbbb118894665155f2d6d7a61631bb30 Mon Sep 17 00:00:00 2001 From: Daniel Mursa Date: Wed, 8 Jan 2025 14:47:49 +0100 Subject: [PATCH] [#464] Improve JS --- src/objects/core/admin.py | 14 ++++++------ .../admin/permissions/permission-form.js | 22 ++++++++++--------- 2 files changed, 19 insertions(+), 17 deletions(-) diff --git a/src/objects/core/admin.py b/src/objects/core/admin.py index 3381d696..c03c5240 100644 --- a/src/objects/core/admin.py +++ b/src/objects/core/admin.py @@ -29,17 +29,17 @@ def get_urls(self): return my_urls + urls def versions_view(self, request, objecttype_id): - versions = {} + versions = { + "count": 0, + "next": None, + "previous": None, + "results": [], + } if objecttype := self.get_object(request, objecttype_id): client = build_client(objecttype.service) try: response = client.get(objecttype.versions_url) - response_data = response.json() - - # TODO: remove check once API V1 is removed - if "results" in response_data: - versions = response_data["results"] - + versions = response.json() except (requests.RequestException, requests.JSONDecodeError): pass return JsonResponse(versions, safe=False) diff --git a/src/objects/js/components/admin/permissions/permission-form.js b/src/objects/js/components/admin/permissions/permission-form.js index 8d064fbd..a1d7d1c8 100644 --- a/src/objects/js/components/admin/permissions/permission-form.js +++ b/src/objects/js/components/admin/permissions/permission-form.js @@ -22,17 +22,19 @@ const PermissionForm = ({objectFields, tokenChoices, objecttypeChoices, modeChoi }) .then(response => response.json()) .then(response_data => { - const objecttypes = { - [objecttype_id]: response_data.reduce((acc, version) => { - const properties = Object.keys(version.jsonSchema?.properties || {}); - acc[version.version] = properties.reduce((propsAcc, prop) => { - propsAcc[prop] = `record__data__${prop}`; - return propsAcc; - }, {}); - return acc; - }, {}) - }; + if (response_data?.results?.length > 0) { + const objecttypes = { + [objecttype_id]: response_data.results.reduce((acc, version) => { + const properties = Object.keys(version?.jsonSchema?.properties || {}); + acc[version.version] = properties.reduce((propsAcc, prop) => { + propsAcc[prop] = `record__data__${prop}`; + return propsAcc; + }, {}); + return acc; + }, {}) + }; setDataFieldChoices(objecttypes); + } }) .catch(error => { console.error('An error occurred while fetching the Objecttype versions endpoint:', error);