|
1
|
"use client" |
|
2
|
|
|
3
|
import React, { useEffect, useState } from "react" |
|
4
|
import { ApiRunnerParamInput, ApiRunnerParamInputProps } from "../Default" |
|
5
|
import clsx from "clsx" |
|
6
|
import setObjValue from "@/utils/set-obj-value" |
|
7
|
import { Button } from "../../../../components/Button" |
|
8
|
import { Minus, Plus } from "@medusajs/icons" |
|
9
|
|
|
10
|
export const ApiRunnerParamArrayInput = ({ |
|
11
|
paramName, |
|
12
|
paramValue, |
|
13
|
objPath, |
|
14
|
setValue, |
|
15
|
}: ApiRunnerParamInputProps) => { |
|
16
|
const [itemsValue, setItemsValue] = useState<typeof paramValue>(paramValue) |
|
17
|
|
|
18
|
useEffect(() => { |
|
19
|
setValue((prev: unknown) => { |
|
20
|
return typeof prev === "object" && !Array.isArray(prev) |
|
21
|
? setObjValue({ |
|
22
|
obj: { ...prev }, |
|
23
|
value: itemsValue, |
|
24
|
path: `${objPath.length ? `${objPath}.` : ""}${paramName}`, |
|
25
|
}) |
|
26
|
: itemsValue |
|
27
|
}) |
|
28
|
}, [itemsValue]) |
|
29
|
|
|
30
|
if (!Array.isArray(paramValue)) { |
|
31
|
return ( |
|
32
|
<ApiRunnerParamInput |
|
33
|
paramName={paramName} |
|
34
|
paramValue={paramValue} |
|
35
|
objPath={objPath} |
|
36
|
setValue={setValue} |
|
37
|
/> |
|
38
|
) |
|
39
|
} |
|
40
|
|
|
41
|
return ( |
|
42
|
<fieldset |
|
43
|
className={clsx( |
|
44
|
"border border-medusa-border-strong rounded", |
|
45
|
"p-docs_0.5" |
|
46
|
)} |
|
47
|
> |
|
48
|
<legend className="px-docs_0.5"> |
|
49
|
<code>{paramName}</code> Array Items |
|
50
|
</legend> |
|
51
|
{(itemsValue as unknown[]).map((value, index) => ( |
|
52
|
<div |
|
53
|
key={index} |
|
54
|
className={clsx( |
|
55
|
index > 0 && |
|
56
|
"flex gap-docs_0.5 items-center justify-center mt-docs_0.5" |
|
57
|
)} |
|
58
|
> |
|
59
|
<ApiRunnerParamInput |
|
60
|
paramName={`[${index}]`} |
|
61
|
paramValue={value} |
|
62
|
objPath={""} |
|
63
|
setValue={setItemsValue} |
|
64
|
/> |
|
65
|
{index > 0 && ( |
|
66
|
<Button |
|
67
|
buttonType="icon" |
|
68
|
variant="secondary" |
|
69
|
onClick={() => { |
|
70
|
setItemsValue((prev: unknown[]) => prev.splice(index, 1)) |
|
71
|
}} |
|
72
|
className="mt-0.5" |
|
73
|
data-testid="minus-button" |
|
74
|
> |
|
75
|
<Minus /> |
|
76
|
</Button> |
|
77
|
)} |
|
78
|
</div> |
|
79
|
))} |
|
80
|
<Button |
|
81
|
buttonType="icon" |
|
82
|
variant="secondary" |
|
83
|
onClick={() => { |
|
84
|
setItemsValue((prev: unknown[]) => [ |
|
85
|
...prev, |
|
86
|
Array.isArray(prev[0]) |
|
87
|
? [...prev[0]] |
|
88
|
: typeof prev[0] === "object" |
|
89
|
? Object.assign({}, prev[0]) |
|
90
|
: prev[0], |
|
91
|
]) |
|
92
|
}} |
|
93
|
className="mt-0.5" |
|
94
|
data-testid="plus-button" |
|
95
|
> |
|
96
|
<Plus /> |
|
97
|
</Button> |
|
98
|
</fieldset> |
|
99
|
) |
|
100
|
} |