Skip to content

Commit

Permalink
wip: headers pane rendering
Browse files Browse the repository at this point in the history
  • Loading branch information
wllfaria committed Jun 3, 2024
1 parent b804c8d commit 552f062
Show file tree
Hide file tree
Showing 5 changed files with 139 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -536,6 +536,7 @@ impl<'cv> CollectionViewer<'cv> {
CreateReqKind::Request => RequestKind::Single(Arc::new(RwLock::new(Request {
id: uuid::Uuid::new_v4().to_string(),
name: form_state.req_name.clone(),
headers: None,
method: form_state.method.clone(),
uri: String::default(),
body: None,
Expand Down
34 changes: 21 additions & 13 deletions hac-client/src/pages/collection_viewer/request_editor.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
#[allow(clippy::module_inception)]
mod auth_editor;
mod body_editor;
mod headers_editor;

use auth_editor::AuthEditor;
use body_editor::{BodyEditor, BodyEditorEvent};
use hac_config::EditorMode;
use hac_core::collection::types::{Request, RequestMethod};
use hac_core::text_object::{TextObject, Write};
use headers_editor::{HeadersEditor, HeadersEditorEvent};

use crate::pages::collection_viewer::collection_store::CollectionStore;
use crate::pages::under_construction::UnderConstruction;
Expand Down Expand Up @@ -93,6 +96,8 @@ pub struct RequestEditor<'re> {
colors: &'re hac_colors::Colors,
collection_store: Rc<RefCell<CollectionStore>>,
body_editor: BodyEditor<'re>,
headers_editor: HeadersEditor<'re>,
auth_editor: AuthEditor<'re>,

layout: ReqEditorLayout,
curr_tab: ReqEditorTabs,
Expand Down Expand Up @@ -124,6 +129,8 @@ impl<'re> RequestEditor<'re> {
collection_store.clone(),
layout.content_pane,
),
headers_editor: HeadersEditor::new(colors, collection_store.clone()),
auth_editor: AuthEditor::new(colors),
layout,
curr_tab,
collection_store,
Expand All @@ -148,9 +155,9 @@ impl<'re> RequestEditor<'re> {
fn draw_current_tab(&mut self, frame: &mut Frame, size: Rect) -> anyhow::Result<()> {
match self.curr_tab {
ReqEditorTabs::Body => self.body_editor.draw(frame, size)?,
ReqEditorTabs::Headers => UnderConstruction::new(self.colors).draw(frame, size)?,
ReqEditorTabs::Headers => self.headers_editor.draw(frame, size)?,
ReqEditorTabs::Query => UnderConstruction::new(self.colors).draw(frame, size)?,
ReqEditorTabs::Auth => UnderConstruction::new(self.colors).draw(frame, size)?,
ReqEditorTabs::Auth => self.auth_editor.draw(frame, size)?,
}

Ok(())
Expand Down Expand Up @@ -179,14 +186,9 @@ impl<'re> RequestEditor<'re> {
}

fn draw_container(&self, size: Rect, frame: &mut Frame) {
let is_focused = self
.collection_store
.borrow()
.get_focused_pane()
.eq(&PaneFocus::Editor);
let is_selected = self
.collection_store
.borrow()
let store = self.collection_store.borrow();
let is_focused = store.get_focused_pane().eq(&PaneFocus::Editor);
let is_selected = store
.get_selected_pane()
.is_some_and(|pane| pane.eq(&PaneFocus::Editor));

Expand Down Expand Up @@ -256,9 +258,15 @@ impl Eventful for RequestEditor<'_> {
Some(BodyEditorEvent::Quit) => return Ok(Some(RequestEditorEvent::Quit)),
None => {}
},
ReqEditorTabs::Headers => {}
ReqEditorTabs::Headers => match self.headers_editor.handle_key_event(key_event)? {
Some(HeadersEditorEvent::Quit) => return Ok(Some(RequestEditorEvent::Quit)),
None => {}
},
ReqEditorTabs::Query => {}
ReqEditorTabs::Auth => {}
ReqEditorTabs::Auth => match self.auth_editor.handle_key_event(key_event)? {
Some(_) => todo!(),
None => {}
},
}

Ok(None)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
use crossterm::event::KeyEvent;
use ratatui::{layout::Rect, widgets::Paragraph, Frame};

use crate::pages::{Eventful, Renderable};

#[derive(Debug)]
pub struct AuthEditor<'ae> {
colors: &'ae hac_colors::colors::Colors,
}

impl<'ae> AuthEditor<'ae> {
pub fn new(colors: &'ae hac_colors::colors::Colors) -> Self {
AuthEditor { colors }
}
}

impl Renderable for AuthEditor<'_> {
fn draw(&mut self, frame: &mut Frame, size: Rect) -> anyhow::Result<()> {
frame.render_widget(Paragraph::new("hello from auth editor").centered(), size);

Ok(())
}
}

impl Eventful for AuthEditor<'_> {
type Result = ();

fn handle_key_event(&mut self, key_event: KeyEvent) -> anyhow::Result<Option<Self::Result>> {
Ok(None)
}
}
Original file line number Diff line number Diff line change
@@ -1 +1,75 @@
pub struct HeadersEditor {}
use crate::pages::{collection_viewer::collection_store::CollectionStore, Eventful, Renderable};

use std::{cell::RefCell, rc::Rc};

use crossterm::event::{KeyCode, KeyEvent, KeyModifiers};
use ratatui::{
layout::Rect,
style::Stylize,
widgets::{Cell, Paragraph, Row, Table},
Frame,
};

#[derive(Debug)]
pub enum HeadersEditorEvent {
Quit,
}

#[derive(Debug)]
pub struct HeadersEditor<'he> {
colors: &'he hac_colors::colors::Colors,
collection_store: Rc<RefCell<CollectionStore>>,
}

impl<'he> HeadersEditor<'he> {
pub fn new(
colors: &'he hac_colors::colors::Colors,
collection_store: Rc<RefCell<CollectionStore>>,
) -> Self {
HeadersEditor {
colors,
collection_store,
}
}
}

impl Renderable for HeadersEditor<'_> {
fn draw(&mut self, frame: &mut Frame, size: Rect) -> anyhow::Result<()> {
let Some(request) = self.collection_store.borrow().get_selected_request() else {
return Ok(());
};

let request = request.read().expect("failed to read selected request");
let Some(headers) = request.headers.as_ref() else {
return Ok(());
};

let table = Table::default()
.header(Row::new(vec![
Cell::new("Name".fg(self.colors.normal.red)),
Cell::new("Value".fg(self.colors.normal.red)),
]))
.rows(headers.iter().map(|header_map| {
Row::new(vec![
header_map.pair.0.to_string(),
header_map.pair.1.to_string(),
])
}));

frame.render_widget(table, size);

Ok(())
}
}

impl Eventful for HeadersEditor<'_> {
type Result = HeadersEditorEvent;

fn handle_key_event(&mut self, key_event: KeyEvent) -> anyhow::Result<Option<Self::Result>> {
if let (KeyCode::Char('c'), KeyModifiers::CONTROL) = (key_event.code, key_event.modifiers) {
return Ok(Some(HeadersEditorEvent::Quit));
}

Ok(None)
}
}
17 changes: 11 additions & 6 deletions hac-core/src/collection/types.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
use std::{
hash::Hash,
path::PathBuf,
sync::{Arc, RwLock},
};
use std::hash::Hash;
use std::path::PathBuf;
use std::sync::{Arc, RwLock};

use serde::{Deserialize, Serialize};

Expand Down Expand Up @@ -89,17 +87,24 @@ impl RequestMethod {
}
}

#[derive(Debug, Serialize, Deserialize, PartialEq, Eq, Clone)]
#[derive(Debug, Serialize, Deserialize, Clone)]
pub struct Request {
pub id: String,
pub method: RequestMethod,
pub name: String,
pub uri: String,
pub headers: Option<Vec<HeaderMap>>,
pub body: Option<String>,
#[serde(rename = "bodyType")]
pub body_type: Option<BodyType>,
}

#[derive(Debug, Deserialize, Serialize, Clone)]
pub struct HeaderMap {
pub pair: (String, String),
pub enabled: bool,
}

#[derive(Debug, Serialize, Deserialize, PartialEq, Eq, Clone)]
pub enum BodyType {
#[serde(rename = "json")]
Expand Down

0 comments on commit 552f062

Please sign in to comment.