Skip to content

Commit 8aee3e9

Browse files
committed
refactor(web): Handle empty fields by sending null in Docker Compose
1 parent 2d8cfe0 commit 8aee3e9

3 files changed

Lines changed: 47 additions & 16 deletions

File tree

web/src/lib/helper.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,18 @@ export const getNestedValue = (obj: any, path: string) => {
55
};
66

77
export const convertKVtoObject = (
8-
kvArray: Array<{ key: string; value: string }>,
8+
kvArray: Array<{ key: string; value: string } | null>,
99
) => {
10-
return kvArray.reduce(
10+
return kvArray?.reduce(
1111
(acc, curr) => {
12-
acc[curr.key] = curr.value;
12+
13+
if (curr && acc) {
14+
acc[curr.key] = curr?.value;
15+
16+
}
1317
return acc;
1418
},
15-
{} as Record<string, string>,
19+
{} as Record<string, string> | null,
1620
);
1721
};
1822

web/src/pages/docker-compose/docker-compose.tsx

Lines changed: 34 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -117,13 +117,17 @@ const DockerCompose: FC = () => {
117117
const refactoredService = data.services.map(
118118
({ build: { enabled, ...buildRest }, ...service }) => ({
119119
...service,
120-
environment: convertKVtoObject(service.environment),
121-
...(enabled && {
122-
build: {
123-
...buildRest,
124-
args: convertKVtoObject(buildRest.args),
125-
},
120+
...(service.environment && {
121+
environment: convertKVtoObject(service.environment),
126122
}),
123+
...(enabled
124+
? {
125+
build: {
126+
...buildRest,
127+
args: convertKVtoObject(buildRest.args),
128+
},
129+
}
130+
: { build: null }),
127131
}),
128132
);
129133

@@ -147,9 +151,31 @@ const DockerCompose: FC = () => {
147151
{},
148152
);
149153

154+
const services = refactoredService.map((item) => {
155+
if (item.ports && item?.ports[0].length === 0) {
156+
item.ports = null;
157+
}
158+
if (item.volumes && item.volumes[0].length === 0) {
159+
item.volumes = null;
160+
}
161+
if (item.networks && item.networks[0].length === 0) {
162+
item.networks = null;
163+
}
164+
if (item.depends_on && item.depends_on[0].length === 0) {
165+
item.depends_on = null;
166+
}
167+
if (item.environment && !item.environment[0]) {
168+
item.environment = null;
169+
}
170+
if (item.environment && item.environment[0]) {
171+
item.environment = null;
172+
}
173+
return item;
174+
});
175+
150176
const requestBody: DockerComposeBody = {
151177
version: data.version,
152-
services: convertServicesToObject(refactoredService),
178+
services: convertServicesToObject(services),
153179
networks: refactoredNetwork,
154180
};
155181

@@ -162,6 +188,7 @@ const DockerCompose: FC = () => {
162188
`${error.response?.data.detail[0].loc[error.response?.data.detail[0].loc.length - 1]} ${error.response?.data.detail[0].msg}`,
163189
);
164190
} else {
191+
console.log(error);
165192
toast.error('Something went wrong');
166193
}
167194
}

web/src/pages/docker-compose/docker-compose.type.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ const KV_Schema = zod.array(
6464
value: zod
6565
.string()
6666
.min(1, { message: 'Value must be at least 1 character long' }),
67-
}),
67+
}).nullable(),
6868
);
6969

7070
export const BuildSchema = zod.object({
@@ -80,11 +80,11 @@ export const ServiceSchema = zod.object({
8080
image: zod.string(),
8181
environment: KV_Schema,
8282
container_name: zod.string(),
83-
ports: zod.array(zod.string()),
83+
ports: zod.array(zod.string()).nullable(),
8484
command: zod.string().optional(),
85-
volumes: zod.array(zod.string()),
86-
networks: zod.array(zod.string()),
87-
depends_on: zod.array(zod.string()),
85+
volumes: zod.array(zod.string()).nullable(),
86+
networks: zod.array(zod.string()).nullable(),
87+
depends_on: zod.array(zod.string()).nullable(),
8888
});
8989

9090
const labelValueSchema = zod.object({

0 commit comments

Comments
 (0)