user_table: refactor to clarify view()

This commit is contained in:
Valentin Tolmer 2021-08-31 15:43:51 +02:00 committed by nitnelave
parent 9c4f8931a0
commit 6efafa99c8

View File

@ -16,9 +16,11 @@ pub struct ListUsersQuery;
use list_users_query::{RequestFilter, ResponseData}; use list_users_query::{RequestFilter, ResponseData};
type User = list_users_query::ListUsersQueryUsers;
pub struct UserTable { pub struct UserTable {
link: ComponentLink<Self>, link: ComponentLink<Self>,
users: Option<Result<Vec<list_users_query::ListUsersQueryUsers>>>, users: Option<Result<Vec<User>>>,
// Used to keep the request alive long enough. // Used to keep the request alive long enough.
_task: Option<FetchTask>, _task: Option<FetchTask>,
} }
@ -75,25 +77,19 @@ impl Component for UserTable {
} }
fn view(&self) -> Html { fn view(&self) -> Html {
match &self.users { let make_user_row = |user: &User| {
None => html! {{"Loading..."}},
Some(Err(e)) => html! {<div>{"Error: "}{e.to_string()}</div>},
Some(Ok(users)) => {
let table_content: Vec<_> = users
.iter()
.map(|u| {
html! { html! {
<tr> <tr>
<td>{&u.id}</td> <td>{&user.id}</td>
<td>{&u.email}</td> <td>{&user.email}</td>
<td>{&u.display_name.as_ref().unwrap_or(&String::new())}</td> <td>{&user.display_name.as_ref().unwrap_or(&String::new())}</td>
<td>{&u.first_name.as_ref().unwrap_or(&String::new())}</td> <td>{&user.first_name.as_ref().unwrap_or(&String::new())}</td>
<td>{&u.last_name.as_ref().unwrap_or(&String::new())}</td> <td>{&user.last_name.as_ref().unwrap_or(&String::new())}</td>
<td>{&u.creation_date.with_timezone(&chrono::Local)}</td> <td>{&user.creation_date.with_timezone(&chrono::Local)}</td>
</tr> </tr>
} }
}) };
.collect(); let make_table = |users: &Vec<User>| {
html! { html! {
<table> <table>
<tr> <tr>
@ -104,10 +100,14 @@ impl Component for UserTable {
<th>{"Last name"}</th> <th>{"Last name"}</th>
<th>{"Creation date"}</th> <th>{"Creation date"}</th>
</tr> </tr>
{table_content} {users.iter().map(make_user_row).collect::<Vec<_>>()}
</table> </table>
} }
} };
match &self.users {
None => html! {{"Loading..."}},
Some(Err(e)) => html! {<div>{"Error: "}{e.to_string()}</div>},
Some(Ok(users)) => make_table(users),
} }
} }
} }