Skip to content

Commit 5933691

Browse files
committed
refactor: update parameter handling to use Promises in API routes and components
1 parent a0d3a8e commit 5933691

File tree

11 files changed

+52
-78
lines changed

11 files changed

+52
-78
lines changed

src/management-system-v2/app/(dashboard)/[environmentId]/machine-config/[configId]/aas-config-page-content.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -435,7 +435,7 @@ const AasConfigContent: React.FC<VariablesEditorProps> = ({
435435
data.forEach((item) => {
436436
keys.push(item.key);
437437
if (item.children && item.children.length > 0) {
438-
const itemTitle = item.title as React.ReactElement;
438+
const itemTitle = item.title as any;
439439
if (itemTitle?.props?.type != 'meta') {
440440
//console.log(itemTitle?.props?.element.name);
441441
keys = keys.concat(getAllKeys(item.children));

src/management-system-v2/app/(dashboard)/[environmentId]/machine-config/[configId]/aas-create-parameter-modal.tsx

Lines changed: 14 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@ import { buildLinkedInputParametersFromIds } from '../configuration-helper';
3333
import { getConfigurationCategories } from '@/lib/data/db/machine-config';
3434
import { useEnvironment } from '../../../../../components/auth-can';
3535
import { QuestionCircleOutlined } from '@ant-design/icons';
36-
import { IntegrityErrorModal } from '../../iam/users/create-users';
3736
export type CreateParameterModalReturnType = {
3837
name: string;
3938
value: string;
@@ -196,17 +195,6 @@ const AasCreateParameterModal = <T extends CreateParameterModalReturnType>({
196195
// Validate Failed
197196
}
198197
};
199-
if (error) {
200-
return (
201-
<IntegrityErrorModal
202-
open={open}
203-
close={() => {
204-
setError(null);
205-
onCancel();
206-
}}
207-
/>
208-
);
209-
}
210198

211199
return (
212200
<Modal
@@ -274,6 +262,16 @@ const AasCreateParameterModal = <T extends CreateParameterModalReturnType>({
274262
);
275263
};
276264

265+
// helper function to generate name from display name
266+
const generateNameFromDisplayName = (displayName: string): string => {
267+
if (!displayName) return '';
268+
return displayName
269+
.replace(/\s+/g, '-')
270+
.replace(/[^a-zA-Z0-9\-_+]/g, '')
271+
.replace(/^-+|-+$/g, '')
272+
.replace(/-+/g, '-');
273+
};
274+
277275
type CreateParameterInputsProps = {
278276
index: number;
279277
showKey?: boolean;
@@ -427,16 +425,6 @@ const ParameterInputs = ({
427425
setCurrentDescriptionLanguage(language);
428426
};
429427

430-
// helper function to generate name from display name
431-
const generateNameFromDisplayName = (displayName: string): string => {
432-
if (!displayName) return '';
433-
return displayName
434-
.replace(/\s+/g, '-')
435-
.replace(/[^a-zA-Z0-9\-_+]/g, '')
436-
.replace(/^-+|-+$/g, '')
437-
.replace(/-+/g, '-');
438-
};
439-
440428
// validation rule for name field
441429
const validateName = (_: any, value: string) => {
442430
if (!value) return Promise.resolve();
@@ -706,16 +694,16 @@ const ParameterInputs = ({
706694
other input parameter and is never changed automatically.
707695
</div>
708696
<div style={{ marginTop: '8px' }}>
709-
<strong>Manual:</strong> When the linked parameter's value is updated,
710-
this value will be cleared and can be manually set again.
697+
<strong>Manual:</strong> When the linked parameter&apos;s value is
698+
updated, this value will be cleared and can be manually set again.
711699
</div>
712700
<div style={{ marginTop: '8px' }}>
713701
<strong>Linked:</strong> The value is always identical to the linked
714-
parameter's value. Any linked value change will be copied over.
702+
parameter&apos;s value. Any linked value change will be copied over.
715703
</div>
716704
<div style={{ marginTop: '8px' }}>
717705
<strong>Formula:</strong> A JSONata formula can be used to automatically
718-
update this value whenever one of the linked parameter's values is
706+
update this value whenever one of the linked parameter&apos;s values is
719707
updated.
720708
</div>
721709
</>

src/management-system-v2/app/(dashboard)/[environmentId]/machine-config/[configId]/page.tsx

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,27 +9,26 @@ import MachineConfigViewClient from './page-client';
99
import { getCurrentEnvironment } from '@/components/auth';
1010

1111
type MachineConfigProps = {
12-
params: { environmentId: string; configId: string };
13-
searchParams: { [key: string]: string | string[] | undefined };
12+
params: Promise<{ environmentId: string; configId: string }>;
13+
searchParams: Promise<{ [key: string]: string | string[] | undefined }>;
1414
};
1515

16-
const MachineConfigView: React.FC<MachineConfigProps> = async ({
17-
params: { environmentId, configId },
18-
searchParams,
19-
}) => {
16+
const MachineConfigView: React.FC<MachineConfigProps> = async ({ params, searchParams }) => {
17+
const { environmentId, configId } = await params;
18+
const searchParamsResolved = await searchParams;
2019
const { ability, activeEnvironment } = await getCurrentEnvironment(environmentId);
2120
if (activeEnvironment.isOrganization) {
2221
await syncOrganizationUsers(activeEnvironment.spaceId);
2322
}
24-
const selectedVersionId = searchParams.version as string | undefined;
25-
const source = searchParams.source as string | undefined;
23+
const selectedVersionId = searchParamsResolved.version as string | undefined;
24+
const source = searchParamsResolved.source as string | undefined;
2625

2726
// collect all machine dataset version selections
2827
const machineDatasetVersions: Record<string, string> = {};
29-
Object.keys(searchParams).forEach((key) => {
28+
Object.keys(searchParamsResolved).forEach((key) => {
3029
if (key.startsWith('machineVersion_')) {
3130
const datasetId = key.replace('machineVersion_', '');
32-
machineDatasetVersions[datasetId] = searchParams[key] as string;
31+
machineDatasetVersions[datasetId] = searchParamsResolved[key] as string;
3332
}
3433
});
3534

src/management-system-v2/app/(dashboard)/[environmentId]/machine-config/page.tsx

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,14 @@ export type ListItem = Config;
1919
const MachineConfigPage = async ({
2020
params,
2121
}: {
22-
params: { environmentId: string; folderId?: string };
22+
params: Promise<{ environmentId: string; folderId?: string }>;
2323
}) => {
2424
if (!env.PROCEED_PUBLIC_CONFIG_SERVER_ACTIVE) {
2525
return notFound();
2626
}
27+
const { environmentId } = await params;
2728

28-
const { ability, activeEnvironment } = await getCurrentEnvironment(params.environmentId);
29+
const { ability, activeEnvironment } = await getCurrentEnvironment(environmentId);
2930

3031
if (!ability.can('view', 'MachineConfig')) return <UnauthorizedFallback />;
3132

src/management-system-v2/app/api/spaces/[spaceId]/configurations/[configSetId]/[versionId]/[parameterId]/route.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,12 @@ import { NextRequest, NextResponse } from 'next/server';
33

44
export async function GET(
55
request: NextRequest,
6-
{
7-
params: { configSetId, versionId, parameterId },
8-
}: { params: { configSetId: string; versionId: string; parameterId: string } },
6+
{ params }: { params: Promise<{ configSetId: string; versionId: string; parameterId: string }> },
97
) {
108
// Answer - Success: 200 OK, Body: one parameter of one configuration ({id, key, type, content[], parentId, parameters[], parentType, linkedParameters[]})
119
// Answer - Error: 404 Not Found
1210
try {
11+
const { configSetId, versionId, parameterId } = await params;
1312
const config = await nestedParametersFromStorage([parameterId]);
1413
return NextResponse.json(Object.values(config)[0]);
1514
} catch (error: any) {

src/management-system-v2/app/api/spaces/[spaceId]/configurations/[configSetId]/[versionId]/route.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,12 @@ import { validate as uuidValidate, v4 } from 'uuid';
55

66
export async function GET(
77
request: NextRequest,
8-
{
9-
params: { spaceId, configSetId, versionId },
10-
}: { params: { spaceId: string; configSetId: string; versionId: string } },
8+
{ params }: { params: Promise<{ spaceId: string; configSetId: string; versionId: string }> },
119
) {
1210
// Answer - Success: 200 OK, Body: one version of one configuration, same as export
1311
// Answer - Error: 404 Not Found
1412
try {
13+
const { spaceId, configSetId, versionId } = await params;
1514
if (versionId === 'latest') {
1615
const searchParams = request.nextUrl.searchParams;
1716
let queryId = uuidValidate(configSetId)

src/management-system-v2/app/api/spaces/[spaceId]/configurations/[configSetId]/latest-parameter/[parameterId]/route.ts

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -20,15 +20,14 @@ import { parameterToProp } from '@/app/(dashboard)/[environmentId]/machine-confi
2020

2121
export async function GET(
2222
request: NextRequest,
23-
{
24-
params: { configSetId, parameterId },
25-
}: { params: { configSetId: string; parameterId: string } },
23+
{ params }: { params: Promise<{ configSetId: string; parameterId: string }> },
2624
) {
2725
// Answer - Success: 200 OK, Body: one parameter of one configuration ({id, key, type, content[], parentId, parameters[], parentType, linkedParameters[]})
2826
// Answer - Error: 404 Not Found
2927

3028
const searchParams = request.nextUrl.searchParams;
3129
try {
30+
const { configSetId, parameterId } = await params;
3231
const parameter = await nestedParametersFromStorage([parameterId]);
3332

3433
if (searchParams.get('aas-format') === 'true') {
@@ -43,11 +42,10 @@ export async function GET(
4342

4443
export async function PUT(
4544
request: NextRequest,
46-
{
47-
params: { spaceId, configSetId, parameterId },
48-
}: { params: { spaceId: string; configSetId: string; parameterId: string } },
45+
{ params }: { params: Promise<{ spaceId: string; configSetId: string; parameterId: string }> },
4946
) {
5047
try {
48+
const { spaceId, configSetId, parameterId } = await params;
5149
let queryId = uuidValidate(configSetId)
5250
? configSetId
5351
: await getConfigIdFromShortName(configSetId, spaceId);
@@ -111,11 +109,10 @@ export async function PUT(
111109

112110
export async function DELETE(
113111
request: NextRequest,
114-
{
115-
params: { spaceId, configSetId, parameterId },
116-
}: { params: { spaceId: string; configSetId: string; parameterId: string } },
112+
{ params }: { params: Promise<{ spaceId: string; configSetId: string; parameterId: string }> },
117113
) {
118114
try {
115+
const { spaceId, configSetId, parameterId } = await params;
119116
let queryId = uuidValidate(configSetId)
120117
? configSetId
121118
: await getConfigIdFromShortName(configSetId, spaceId);

src/management-system-v2/app/api/spaces/[spaceId]/configurations/[configSetId]/latest-parameter/route.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,10 @@ import { validate as uuidValidate, v4 } from 'uuid';
2525

2626
export async function POST(
2727
request: NextRequest,
28-
{ params: { spaceId, configSetId } }: { params: { spaceId: string; configSetId: string } },
28+
{ params }: { params: Promise<{ spaceId: string; configSetId: string }> },
2929
) {
3030
try {
31+
const { spaceId, configSetId } = await params;
3132
const searchParams = request.nextUrl.searchParams;
3233
const subParameterOf = searchParams.get('asSubParameterOf');
3334
const aasFormat = searchParams.get('aas-format');
@@ -47,7 +48,6 @@ export async function POST(
4748
changeableByUser: true,
4849
usedAsInputParameterIn: true,
4950
valueType: true,
50-
subParameters: true,
5151
})
5252
.strict()
5353
.parse(body);
@@ -88,11 +88,12 @@ export async function POST(
8888

8989
export async function GET(
9090
request: NextRequest,
91-
{ params: { spaceId, configSetId } }: { params: { spaceId: string; configSetId: string } },
91+
{ params }: { params: Promise<{ spaceId: string; configSetId: string }> },
9292
) {
9393
// Answer - Success: 200 OK, Body: List of all versions of one configuration ({ id, shortname, name, versions: [ {id, name, createdOn, description, versionBasedOn }, ... ])
9494
// Answer - Error: 404 Not Found
9595
try {
96+
const { spaceId, configSetId } = await params;
9697
// TODO embed versions
9798
let queryId = uuidValidate(configSetId)
9899
? configSetId

src/management-system-v2/app/api/spaces/[spaceId]/configurations/[configSetId]/route.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,12 @@ import { validate as uuidValidate } from 'uuid';
1313

1414
export async function GET(
1515
request: NextRequest,
16-
{ params: { spaceId, configSetId } }: { params: { spaceId: string; configSetId: string } },
16+
{ params }: { params: Promise<{ spaceId: string; configSetId: string }> },
1717
) {
1818
// Answer - Success: 200 OK, Body: List of all versions of one configuration [ "1", "2", "latest"]
1919
// Answer - Error: 404 Not Found
2020
try {
21+
const { spaceId, configSetId } = await params;
2122
//TODO: find and return versions
2223
let queryId = uuidValidate(configSetId)
2324
? configSetId
@@ -31,11 +32,12 @@ export async function GET(
3132

3233
export async function PUT(
3334
request: NextRequest,
34-
{ params: { spaceId, configSetId } }: { params: { spaceId: string; configSetId: string } },
35+
{ params }: { params: Promise<{ spaceId: string; configSetId: string }> },
3536
) {
3637
// Answer - Success: 200 OK
3738
// Answer - Error: 409 Invalid input. Body contains the reason. For example, id was given, shortName already exists, syntax invalid, etc.
3839
try {
40+
const { spaceId, configSetId } = await params;
3941
const body: Config = await request.json();
4042
ConfigZod.parse(body);
4143
let usingId = uuidValidate(configSetId);
@@ -75,12 +77,13 @@ export async function PUT(
7577

7678
export async function DELETE(
7779
request: NextRequest,
78-
{ params: { configSetId } }: { params: { configSetId: string } },
80+
{ params }: { params: Promise<{ configSetId: string }> },
7981
) {
8082
// Answer - Success: 200 OK
8183
// Answer - Error: 400 Bad Request, Body: contains the reason
8284

8385
try {
86+
const { configSetId } = await params;
8487
await removeParentConfiguration(configSetId, false);
8588
return NextResponse.json({ message: 'Success!' });
8689
} catch (error: any) {

src/management-system-v2/app/api/spaces/[spaceId]/configurations/route.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,9 @@ export type ListItem = Config;
1616

1717
export async function GET(
1818
request: NextRequest,
19-
{ params: { spaceId } }: { params: { spaceId: string } },
19+
{ params }: { params: Promise<{ spaceId: string }> },
2020
) {
21+
const { spaceId } = await params;
2122
const searchParams = request.nextUrl.searchParams;
2223

2324
// Answer - Success: 200 OK, Body: List of all existing configurations ([ {id, shortname, name, categories }, ... ])
@@ -42,8 +43,9 @@ export async function GET(
4243

4344
export async function POST(
4445
request: NextRequest,
45-
{ params: { spaceId } }: { params: { spaceId: string } },
46+
{ params }: { params: Promise<{ spaceId: string }> },
4647
) {
48+
const { spaceId } = await params;
4749
//TODOs:
4850
// - error if transformation of request body to json fails (but no check of content-type header is json)
4951
// - error if id is inside body

0 commit comments

Comments
 (0)