mirror of
https://github.com/yewstack/yew.git
synced 2025-12-08 21:26:25 +00:00
fix deserialize error of fetch example in yewtil (#1435)
* fix deserialize error of fetch example in yewtil * fix code format * Update yewtil/examples/fetch/src/lib.rs Co-authored-by: Simon <simon@siku2.io> Co-authored-by: Simon <simon@siku2.io>
This commit is contained in:
parent
fb27791332
commit
eee6827b35
@ -11,12 +11,13 @@ pub fn run_app() {
|
||||
}
|
||||
|
||||
struct Model {
|
||||
markdown: Fetch<Request, Vec<Employee>>,
|
||||
markdown: Fetch<Request, RequestBody>,
|
||||
link: ComponentLink<Self>,
|
||||
}
|
||||
|
||||
#[derive(Default, Debug, Clone)]
|
||||
pub struct Request;
|
||||
|
||||
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
|
||||
pub struct Employee {
|
||||
id: String,
|
||||
@ -26,9 +27,24 @@ pub struct Employee {
|
||||
profile_image: String,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
|
||||
pub struct RequestBody {
|
||||
status: String,
|
||||
data: Vec<Employee>,
|
||||
}
|
||||
|
||||
impl Default for RequestBody {
|
||||
fn default() -> RequestBody {
|
||||
RequestBody {
|
||||
status: String::from(""),
|
||||
data: vec![],
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl FetchRequest for Request {
|
||||
type RequestBody = ();
|
||||
type ResponseBody = Vec<Employee>;
|
||||
type ResponseBody = RequestBody;
|
||||
type Format = Json;
|
||||
|
||||
fn url(&self) -> String {
|
||||
@ -51,7 +67,7 @@ impl FetchRequest for Request {
|
||||
}
|
||||
|
||||
enum Msg {
|
||||
SetMarkdownFetchState(FetchAction<Vec<Employee>>),
|
||||
SetMarkdownFetchState(FetchAction<RequestBody>),
|
||||
GetMarkdown,
|
||||
}
|
||||
|
||||
@ -94,7 +110,7 @@ impl Component for Model {
|
||||
html! {<button onclick=self.link.callback(|_| Msg::GetMarkdown)>{"Get employees"}</button>}
|
||||
}
|
||||
FetchState::Fetching(_) => html! {"Fetching"},
|
||||
FetchState::Fetched(data) => data.iter().map(render_employee).collect(),
|
||||
FetchState::Fetched(body) => body.data.iter().map(render_employee).collect(),
|
||||
FetchState::Failed(_, err) => html! {&err},
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user