-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(cdl): begin implementation of zone loader
- Loading branch information
Showing
25 changed files
with
359 additions
and
207 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,5 @@ | ||
pub mod cassette; | ||
pub mod components; | ||
#[cfg(feature = "ui")] | ||
pub mod data; | ||
pub mod document; | ||
pub mod net; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,7 @@ | ||
pub fn add(left: usize, right: usize) -> usize { | ||
left + right | ||
} | ||
mod zone; | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::*; | ||
use actix_web::Scope; | ||
|
||
#[test] | ||
fn it_works() { | ||
let result = add(2, 2); | ||
assert_eq!(result, 4); | ||
} | ||
pub fn build_services(scope: Scope) -> Scope { | ||
scope.service(self::zone::list) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
use actix_web::{get, HttpRequest, HttpResponse, Responder}; | ||
use anyhow::Result; | ||
use cassette_core::{ | ||
data::{ | ||
csv::CsvTable, | ||
table::{DataTable, DataTableLog, DataTableSource}, | ||
}, | ||
result::Result as HttpResult, | ||
}; | ||
use cassette_plugin_kubernetes_api::{load_client, Client}; | ||
use serde::Serialize; | ||
|
||
#[get("/cdl/zone")] | ||
pub async fn list(request: HttpRequest) -> impl Responder { | ||
async fn try_handle(client: Client) -> Result<DataTable> { | ||
Ok(DataTable { | ||
name: "cdl-zones".into(), | ||
data: DataTableSource::Csv(CsvTable::default()), | ||
log: DataTableLog::default(), | ||
}) | ||
} | ||
|
||
match load_client(&request).await { | ||
Ok(client) => response(try_handle(client).await), | ||
Err(error) => HttpResponse::Unauthorized().json(HttpResult::<()>::Err(error.to_string())), | ||
} | ||
} | ||
|
||
fn response<T>(result: Result<T>) -> HttpResponse | ||
where | ||
T: Serialize, | ||
{ | ||
match result { | ||
Ok(data) => HttpResponse::Ok().json(HttpResult::Ok(data)), | ||
Err(error) => { | ||
HttpResponse::MethodNotAllowed().json(HttpResult::<T>::Err(error.to_string())) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,136 @@ | ||
use cassette_core::{ | ||
cassette::CassetteContext, | ||
components::ComponentRenderer, | ||
task::{TaskResult, TaskState}, | ||
}; | ||
use patternfly_yew::prelude::*; | ||
use serde::{Deserialize, Serialize}; | ||
use yew::prelude::*; | ||
|
||
#[derive(Clone, Debug, PartialEq, Deserialize, Properties)] | ||
#[serde(rename_all = "camelCase")] | ||
pub struct Spec {} | ||
|
||
#[derive(Clone, Debug, Default, PartialEq, Serialize, Deserialize)] | ||
#[serde(rename_all = "camelCase")] | ||
pub struct State {} | ||
|
||
impl ComponentRenderer<Spec> for State { | ||
fn render(self, _ctx: &mut CassetteContext, spec: Spec) -> TaskResult<Option<Self>> { | ||
let Spec {} = spec; | ||
|
||
Ok(TaskState::Continue { | ||
body: html! { <Manager/> }, | ||
state: Some(Self {}), | ||
}) | ||
} | ||
} | ||
|
||
#[function_component(Manager)] | ||
fn manager() -> Html { | ||
const TOTAL_ENTRIES: usize = 394; | ||
|
||
let offset = use_state_eq(|| 0); | ||
let limit = use_state_eq(|| 5); | ||
|
||
let entries = use_memo((*offset, *limit), |(offset, limit)| { | ||
(*offset..(offset + limit).clamp(0, TOTAL_ENTRIES)) | ||
.map(ExampleEntry) | ||
.collect::<Vec<_>>() | ||
}); | ||
|
||
let (entries, _) = use_table_data(MemoizedTableModel::new(entries)); | ||
|
||
let header = html_nested! { | ||
<TableHeader<Columns>> | ||
<TableColumn<Columns> index={Columns::Select} /> | ||
<TableColumn<Columns> label="Decimal" index={Columns::Decimal} /> | ||
<TableColumn<Columns> label="Hex" index={Columns::Hex} /> | ||
<TableColumn<Columns> label="Button" index={Columns::Button} /> | ||
</TableHeader<Columns>> | ||
}; | ||
|
||
let total_entries = Some(TOTAL_ENTRIES); | ||
|
||
let limit_callback = use_callback(limit.clone(), |number, limit| limit.set(number)); | ||
|
||
let nav_callback = use_callback( | ||
(offset.clone(), *limit), | ||
|page: Navigation, (offset, limit)| { | ||
let o = match page { | ||
Navigation::First => 0, | ||
Navigation::Last => ((TOTAL_ENTRIES - 1) / limit) * limit, | ||
Navigation::Previous => **offset - limit, | ||
Navigation::Next => **offset + limit, | ||
Navigation::Page(n) => n * limit, | ||
}; | ||
offset.set(o); | ||
}, | ||
); | ||
|
||
html! ( | ||
<> | ||
<Toolbar> | ||
<ToolbarContent> | ||
// FIXME: add bulk-select support: https://www.patternfly.org/components/table/react-demos/bulk-select/ | ||
<ToolbarItem r#type={ToolbarItemType::Pagination}> | ||
<Pagination | ||
{total_entries} | ||
offset={*offset} | ||
entries_per_page_choices={vec![5, 10, 25, 50, 100]} | ||
selected_choice={*limit} | ||
onlimit={&limit_callback} | ||
onnavigation={&nav_callback} | ||
/> | ||
</ToolbarItem> | ||
</ToolbarContent> | ||
</Toolbar> | ||
<Table<Columns, UseTableData<Columns, MemoizedTableModel<ExampleEntry>>> | ||
mode={TableMode::Compact} | ||
{header} | ||
{entries} | ||
/> | ||
<Pagination | ||
{total_entries} | ||
offset={*offset} | ||
entries_per_page_choices={vec![5, 10, 25, 50, 100]} | ||
selected_choice={*limit} | ||
onlimit={&limit_callback} | ||
onnavigation={&nav_callback} | ||
position={PaginationPosition::Bottom} | ||
/> | ||
</> | ||
) | ||
} | ||
|
||
#[derive(Clone, Eq, PartialEq)] | ||
pub enum Columns { | ||
Select, | ||
Decimal, | ||
Hex, | ||
Button, | ||
} | ||
|
||
#[derive(Clone)] | ||
struct ExampleEntry(usize); | ||
|
||
impl TableEntryRenderer<Columns> for ExampleEntry { | ||
fn render_cell(&self, context: CellContext<'_, Columns>) -> Cell { | ||
match context.column { | ||
Columns::Select => html! { <Checkbox /> }, | ||
Columns::Decimal => html!(self.0.to_string()), | ||
Columns::Hex => html!(format!("{:x}", self.0)), | ||
Columns::Button => html! { | ||
<Flex modifiers={[ FlexModifier::Shrink ]}> | ||
<FlexItem> | ||
<Button variant={ ButtonVariant::Primary }>{ "Describe" }</Button> | ||
</FlexItem> | ||
<FlexItem> | ||
<Button variant={ ButtonVariant::Danger }>{ "Delete" }</Button> | ||
</FlexItem> | ||
</Flex> | ||
}, | ||
} | ||
.into() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
use std::rc::Rc; | ||
|
||
use cassette_core::{ | ||
cassette::{CassetteContext, CassetteTaskHandle}, | ||
data::table::DataTable, | ||
net::{ | ||
fetch::{FetchRequestWithoutBody, FetchState, Method}, | ||
gateway::get_gateway, | ||
}, | ||
}; | ||
|
||
pub fn use_fetch( | ||
ctx: &mut CassetteContext, | ||
base_url: Option<String>, | ||
force: bool, | ||
) -> CassetteTaskHandle<FetchState<Rc<DataTable>>> { | ||
let handler_name = "chat completions"; | ||
let state = ctx.use_state(handler_name, force, || FetchState::Pending); | ||
{ | ||
let state = state.clone(); | ||
let base_url = base_url.unwrap_or(get_gateway()); | ||
let request = FetchRequestWithoutBody { | ||
method: Method::GET, | ||
name: handler_name, | ||
url: "/cdl/zone", | ||
body: None, | ||
}; | ||
|
||
request.try_fetch(&base_url, state) | ||
} | ||
state | ||
} |
Oops, something went wrong.