Skip to content

Commit 2156737

Browse files
authored
Merge pull request #4 from KAMAL-02/feat/Real-time-query-change
feat/Realtime-query-update
2 parents f021c30 + 6c52061 commit 2156737

File tree

2 files changed

+26
-27
lines changed

2 files changed

+26
-27
lines changed

src/components/Input Components/QueryProp.tsx

Lines changed: 25 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,43 @@
11
import useStore from "../../store/store";
22
import DynamicFields from "./DynamicFields";
3-
import { useState } from "react";
3+
import { useState, useEffect } from "react";
44

55
const QueryProp = ({ tab }: { tab: number }) => {
66
const setQuery = useStore((state) => state.setQuery);
77

88
const [queryInput, setQueryInput] = useState([{ key: "", value: "" }]);
99

10-
// converts queryInput object into query state i.e URL format string (?pages=10&offset=20)
11-
const addQueries = () => {
12-
const queries =
13-
"?" +
14-
queryInput
15-
.filter((q) => q.key !== "" && q.value !== "")
16-
.map(
17-
(q) => `${encodeURIComponent(q.key)}=${encodeURIComponent(q.value)}`
18-
)
19-
.join("&");
20-
setQuery(queries);
21-
};
10+
useEffect(() => {
11+
const queries = queryInput
12+
.filter((q) => q.key !== "" || q.value !== "") // Include if either is non-empty
13+
.map(
14+
(q) =>
15+
`${encodeURIComponent(q.key)}=${encodeURIComponent(
16+
q.value
17+
)}`.replace(/=$/, "") // Remove "=" if the value is empty
18+
)
19+
.join("&");
20+
21+
const queryString = queries ? `?${queries}` : "";
22+
setQuery(queryString);
23+
}, [queryInput, setQuery]);
24+
25+
2226
const clearQueries = () => {
2327
setQuery("");
2428
setQueryInput([{ key: "", value: "" }]);
2529
};
30+
2631
return (
2732
<div className={`${tab == 1 ? "block" : "hidden"}`}>
2833
{queryInput[0].key !== "" && queryInput[0].value !== "" && (
29-
<>
30-
<button
31-
className="bg-green-500 mt-2 ml-4 px-5 py-1 font-semibold text-white rounded-md"
32-
onClick={addQueries}
33-
>
34-
Add Queries
35-
</button>
36-
<button
37-
className="bg-red-500 mt-2 ml-4 px-5 py-1 font-semibold text-white rounded-md"
38-
onClick={clearQueries}
39-
>
40-
Clear Queries
41-
</button>
34+
<>
35+
<button
36+
className="bg-red-500 mt-2 ml-4 px-5 py-1 font-semibold text-white rounded-md"
37+
onClick={clearQueries}
38+
>
39+
Clear Queries
40+
</button>
4241
</>
4342
)}
4443
<DynamicFields inputs={queryInput} setInputs={setQueryInput} />

src/components/RequestSection.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ const RequestSection = () => {
3232
const api = axios.create({
3333
baseURL: url,
3434
headers,
35-
});
35+
});
3636
if (method === "GET") {
3737
res = await api.get("");
3838
} else if (method === "POST") {

0 commit comments

Comments
 (0)