From de0bd576220bf960a14f8a6db61f8a6ae21445e4 Mon Sep 17 00:00:00 2001 From: Gregory Schier Date: Thu, 9 Mar 2023 13:07:13 -0800 Subject: [PATCH] Hook up header editor! --- src-tauri/icons/icon.icns | Bin 616124 -> 616124 bytes src-tauri/sqlx-data.json | 20 ++++++++++---------- src-tauri/src/main.rs | 10 ++++++++++ src-tauri/src/models.rs | 1 + src-web/App.tsx | 2 +- src-web/components/HeaderEditor.tsx | 25 ++++++++++++++++++------- src-web/components/RequestPane.tsx | 2 +- src-web/components/Tabs.css | 5 +++++ src-web/components/Tabs.tsx | 13 +++++++++++-- 9 files changed, 57 insertions(+), 21 deletions(-) create mode 100644 src-web/components/Tabs.css diff --git a/src-tauri/icons/icon.icns b/src-tauri/icons/icon.icns index 1d926cd4aecc07056dd4360d0711061813587655..bcd5e8e0eb2c09424496074865f5d57ba2428fed 100644 GIT binary patch delta 92 zcmdmUR&~!=RgTQ$ykZ8Kfw*65U`(%;rb{>q2`8INLvTRP^&7BJXO?D>0 diff --git a/src-tauri/sqlx-data.json b/src-tauri/sqlx-data.json index cb44cf1d..c04253b8 100644 --- a/src-tauri/sqlx-data.json +++ b/src-tauri/sqlx-data.json @@ -92,16 +92,6 @@ }, "query": "\n SELECT id, workspace_id, created_at, updated_at, deleted_at, name, url, method, body,\n headers AS \"headers!: sqlx::types::Json>\"\n FROM http_requests\n WHERE workspace_id = ?\n " }, - "3d3cc959cd3844950dde2426945bad638fa5f1a46c4681b5fe2bff60780dea62": { - "describe": { - "columns": [], - "nullable": [], - "parameters": { - "Right": 7 - } - }, - "query": "\n INSERT INTO http_requests (id, workspace_id, name, url, method, body, headers, updated_at)\n VALUES (?, ?, ?, ?, ?, ?, ?, CURRENT_TIMESTAMP)\n ON CONFLICT (id) DO UPDATE SET\n updated_at = CURRENT_TIMESTAMP,\n name = excluded.name,\n method = excluded.method,\n body = excluded.body,\n url = excluded.url\n " - }, "448a1d1f1866ab42c0f81fcf8eb2930bf21dfdd43ca4831bc1a198cf45ac3732": { "describe": { "columns": [], @@ -322,6 +312,16 @@ }, "query": "\n SELECT id, created_at, updated_at, deleted_at, name, description\n FROM workspaces\n " }, + "a097740ea4ab772ec6f9d8a5144d6871e0b172130d5abe4da61e663155d2bf25": { + "describe": { + "columns": [], + "nullable": [], + "parameters": { + "Right": 7 + } + }, + "query": "\n INSERT INTO http_requests (id, workspace_id, name, url, method, body, headers, updated_at)\n VALUES (?, ?, ?, ?, ?, ?, ?, CURRENT_TIMESTAMP)\n ON CONFLICT (id) DO UPDATE SET\n updated_at = CURRENT_TIMESTAMP,\n name = excluded.name,\n method = excluded.method,\n headers = excluded.headers,\n body = excluded.body,\n url = excluded.url\n " + }, "a83698dcf9a815b881097133edb31a34ba25e7c6c114d463c495342a85371639": { "describe": { "columns": [], diff --git a/src-tauri/src/main.rs b/src-tauri/src/main.rs index 7661d108..9e8a8f08 100644 --- a/src-tauri/src/main.rs +++ b/src-tauri/src/main.rs @@ -110,6 +110,16 @@ async fn send_request( HeaderValue::from_str(models::generate_id("x").as_str()).expect("Failed to create header"), ); + for h in req.headers.0 { + if h.name.is_empty() { + continue; + } + headers.insert( + HeaderName::from_bytes(h.name.as_bytes()).expect("Failed to create header name"), + HeaderValue::from_str(h.value.as_str()).expect("Failed to create header value"), + ); + } + let m = Method::from_bytes(req.method.to_uppercase().as_bytes()).expect("Failed to create method"); let builder = client.request(m, url_string.to_string()).headers(headers); diff --git a/src-tauri/src/models.rs b/src-tauri/src/models.rs index 2a063302..97e2cbda 100644 --- a/src-tauri/src/models.rs +++ b/src-tauri/src/models.rs @@ -137,6 +137,7 @@ pub async fn upsert_request( updated_at = CURRENT_TIMESTAMP, name = excluded.name, method = excluded.method, + headers = excluded.headers, body = excluded.body, url = excluded.url "#, diff --git a/src-web/App.tsx b/src-web/App.tsx index 62defac4..539c0842 100644 --- a/src-web/App.tsx +++ b/src-web/App.tsx @@ -24,7 +24,7 @@ export function App({ matches }: { path: string; matches?: Params }) { const isH = screenWidth > 900; return ( -
+
; id: string }; -export function HeaderEditor() { +export function HeaderEditor({ request }: Props) { + const updateRequest = useRequestUpdate(request); const newPair = () => { return { header: { name: '', value: '' }, id: Math.random().toString() }; }; - const [pairs, setPairs] = useState([newPair()]); + const [pairs, setPairs] = useState( + request.headers.map((h) => ({ header: h, id: Math.random().toString() })), + ); const handleChangeHeader = (pair: PairWithId) => { - setPairs((pairs) => - pairs.map((p) => + setPairs((pairs) => { + const newPairs = pairs.map((p) => pair.id !== p.id ? p : { id: p.id, header: { ...p.header, ...pair.header } }, - ), - ); + ); + const headers = newPairs.map((p) => ({ name: '', value: '', ...p.header })); + updateRequest.mutate({ headers }); + return newPairs; + }); }; useEffect(() => { diff --git a/src-web/components/RequestPane.tsx b/src-web/components/RequestPane.tsx index 3d4e6a5f..bc2b427f 100644 --- a/src-web/components/RequestPane.tsx +++ b/src-web/components/RequestPane.tsx @@ -54,7 +54,7 @@ export function RequestPane({ fullHeight, request, className }: Props) { /> - +
diff --git a/src-web/components/Tabs.css b/src-web/components/Tabs.css new file mode 100644 index 00000000..ba7841d1 --- /dev/null +++ b/src-web/components/Tabs.css @@ -0,0 +1,5 @@ +.tab-content { + &[data-state="inactive"] { + @apply hidden; + } +} diff --git a/src-web/components/Tabs.tsx b/src-web/components/Tabs.tsx index 8577561e..03d35dd7 100644 --- a/src-web/components/Tabs.tsx +++ b/src-web/components/Tabs.tsx @@ -6,6 +6,8 @@ import { Button } from './Button'; import { ScrollArea } from './ScrollArea'; import { HStack } from './Stacks'; +import './Tabs.css'; + interface Props { defaultValue?: string; label: string; @@ -49,9 +51,12 @@ export function TabTrigger({ value, children, active }: TabTriggerProps) { return ( @@ -67,7 +72,11 @@ interface TabContentProps { export function TabContent({ value, children, className }: TabContentProps) { return ( - + {children} );