Skip to content

Commit 5c17d64

Browse files
feat: improve Try It Out panel and docs
1 parent ce8de8b commit 5c17d64

2 files changed

Lines changed: 101 additions & 27 deletions

File tree

‎frontend/src/components/TryItPanel.jsx‎

Lines changed: 100 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,33 @@ export default function TryItPanel({ endpoint, method = "POST" }) {
55
const [jsonBody, setJsonBody] = useState(`{}`);
66
const [response, setResponse] = useState("");
77
const [status, setStatus] = useState("");
8+
const [error, setError] = useState("");
9+
const [params, setParams] = useState({});
10+
11+
function buildEndpoint() {
12+
let url = endpoint;
13+
Object.keys(params).forEach(key => {
14+
url = url.replace(`:${key}`, params[key]);
15+
});
16+
return url;
17+
}
18+
819

920
async function sendRequest() {
21+
// Stop if API key is missing
22+
if (!apiKey || apiKey.trim() === "") {
23+
setError("You need an API key. Go to Dashboard → Create Project → Copy API key.");
24+
return;
25+
}
26+
27+
// Clear previous error
28+
setError("");
29+
1030
try {
11-
const res = await fetch(`https://api.urbackend.bitbros.in${endpoint}`, {
31+
const res = await fetch(`https://api.urbackend.bitbros.in${buildEndpoint()}`, {
32+
33+
34+
1235
method,
1336
headers: {
1437
"Content-Type": "application/json",
@@ -26,40 +49,91 @@ export default function TryItPanel({ endpoint, method = "POST" }) {
2649
}
2750

2851
return (
29-
<div className="bg-zinc-900 p-4 rounded-lg mt-4 border border-zinc-700">
30-
<div className="mb-2">
31-
<label className="text-sm text-zinc-400">x-api-key</label>
32-
<input
33-
className="w-full p-2 mt-1 bg-black border border-zinc-700 rounded"
34-
value={apiKey}
35-
onChange={(e) => setApiKey(e.target.value)}
36-
placeholder="sk_live_xxxxx"
37-
/>
38-
</div>
52+
<div className="card" style={{ marginTop: "1.5rem" }}>
53+
<h4 style={{ fontSize: "0.9rem", marginBottom: "1rem", color: "var(--color-text-muted)" }}>
54+
Try It Out
55+
</h4>
56+
57+
<div style={{ marginBottom: "1rem" }}>
58+
<label className="form-label">x-api-key</label>
59+
<input
60+
className="input-field"
61+
value={apiKey}
62+
onChange={(e) => setApiKey(e.target.value)}
63+
placeholder="YOUR API KEY"
64+
/>
65+
</div>
66+
{endpoint.includes(":") && (
67+
<div style={{ marginBottom: "1rem" }}>
68+
<label className="form-label">Path Parameters</label>
69+
70+
{endpoint
71+
.match(/:([a-zA-Z0-9_]+)/g)
72+
?.map(p => {
73+
const key = p.replace(":", "");
74+
return (
75+
<input
76+
key={key}
77+
className="input-field"
78+
placeholder={key}
79+
value={params[key] || ""}
80+
onChange={e => setParams({ ...params, [key]: e.target.value })}
81+
style={{ marginBottom: "0.5rem" }}
82+
/>
83+
);
84+
})}
85+
</div>
86+
)}
3987

40-
<div className="mb-2">
41-
<label className="text-sm text-zinc-400">Request Body</label>
88+
{method !== "GET" && (
89+
<div style={{ marginBottom: "1rem" }}>
90+
<label className="form-label">Request Body</label>
4291
<textarea
43-
className="w-full p-2 mt-1 bg-black border border-zinc-700 rounded font-mono"
44-
rows={6}
92+
className="input-field"
93+
style={{ minHeight: "120px", fontFamily: "monospace" }}
4594
value={jsonBody}
4695
onChange={(e) => setJsonBody(e.target.value)}
4796
/>
4897
</div>
98+
)}
4999

50-
<button
51-
onClick={sendRequest}
52-
className="bg-green-500 text-black px-4 py-2 rounded"
53-
>
54-
Send Request
55-
</button>
100+
{error && (
101+
<div style={{
102+
background: "rgba(239,68,68,0.1)",
103+
border: "1px solid rgba(239,68,68,0.4)",
104+
padding: "0.75rem",
105+
borderRadius: "6px",
106+
color: "#f87171",
107+
marginBottom: "1rem",
108+
fontSize: "0.85rem"
109+
}}>
110+
{error}
111+
</div>
112+
)}
113+
114+
<button onClick={sendRequest} className="btn btn-primary">
115+
Send Request
116+
</button>
56117

57-
<div className="mt-4">
58-
<p className="text-sm text-zinc-400">Status: {status}</p>
59-
<pre className="bg-black p-2 mt-1 rounded text-green-400 text-sm overflow-x-auto">
118+
{response && (
119+
<div style={{ marginTop: "1rem" }}>
120+
<div style={{ fontSize: "0.8rem", color: "var(--color-text-muted)", marginBottom: "0.25rem" }}>
121+
Status: {status}
122+
</div>
123+
<pre style={{
124+
background: "#0b0b0b",
125+
border: "1px solid #222",
126+
borderRadius: "6px",
127+
padding: "1rem",
128+
color: "var(--color-primary)",
129+
fontSize: "0.8rem",
130+
overflowX: "auto"
131+
}}>
60132
{response}
61133
</pre>
62134
</div>
63-
</div>
64-
);
135+
)}
136+
</div>
137+
);
138+
65139
}

‎frontend/src/pages/Docs.jsx‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -254,7 +254,7 @@ console.log(data);
254254
body={{ name: "MacBook Pro", price: 1299, inStock: true }}
255255
comment="Add a new item"
256256
/>
257-
<TryItPanel endpoint="/api/data/products" method="POST" />
257+
<TryItPanel endpoint="/api/data/:collectionName" method="POST" />
258258

259259

260260
<h3 style={{ fontSize: '1.1rem', marginTop: '2rem' }}>3. Get / Update / Delete by ID</h3>

0 commit comments

Comments
 (0)