Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Working out on routing #536

Closed
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
style: nightly cargo fmt
arn-the-long-beard committed Oct 30, 2020
commit bce164b631482db67c29ef0138f41a3404e05199
22 changes: 12 additions & 10 deletions examples/advanced_routing/src/models/auth.rs
Original file line number Diff line number Diff line change
@@ -8,27 +8,27 @@ pub struct Data {
}
/// Setters and getters for password
impl Data {
pub fn set_password(&mut self, pwd: String) {
pub fn set_password(&mut self, pwd: String,) {
self.password = pwd
}

pub fn password(&self) -> &str {
pub fn password(&self,) -> &str {
self.password.as_str()
}

pub fn set_email(&mut self, email: String) {
pub fn set_email(&mut self, email: String,) {
self.email = email
}

pub fn email(&self) -> &str {
pub fn email(&self,) -> &str {
self.email.as_str()
}

pub fn set_username(&mut self, username: String) {
pub fn set_username(&mut self, username: String,) {
self.username = username
}

pub fn username(&self) -> &str {
pub fn username(&self,) -> &str {
self.username.as_str()
}
}
@@ -40,18 +40,20 @@ pub struct LoginCredentials {
}

impl LoginCredentials {
pub fn target(&self) -> &str {
pub fn target(&self,) -> &str {
&self.target
}

pub fn password(&self) -> &str {
pub fn password(&self,) -> &str {
&self.password
}

/// Set email or username
pub fn set_target(&mut self, target: String) {
pub fn set_target(&mut self, target: String,) {
self.target = target;
}
pub fn set_password(&mut self, password: String) {

pub fn set_password(&mut self, password: String,) {
self.password = password;
}
}
9 changes: 6 additions & 3 deletions examples/advanced_routing/src/models/user.rs
Original file line number Diff line number Diff line change
@@ -10,7 +10,9 @@ pub struct LoggedData {
}

impl LoggedData {
pub fn new(first_name: &str, last_name: &str, username: &str, email: &str, role: Role) -> Self {
pub fn new(
first_name: &str, last_name: &str, username: &str, email: &str, role: Role,
) -> Self {
LoggedData {
first_name: first_name.to_string(),
last_name: last_name.to_string(),
@@ -22,10 +24,11 @@ impl LoggedData {
}

impl LoggedData {
pub fn username(&self) -> &str {
pub fn username(&self,) -> &str {
&self.username
}
pub fn email(&self) -> &str {

pub fn email(&self,) -> &str {
&self.email
}
}
26 changes: 13 additions & 13 deletions examples/advanced_routing/src/pages/admin/mod.rs
Original file line number Diff line number Diff line change
@@ -12,18 +12,18 @@ pub fn init(
_: &mut Model,
id: &str,
children: &Routes,
orders: &mut impl Orders<Msg>,
orders: &mut impl Orders<Msg,>,
) -> Model {
let models = load_models();
let model_to_load = models.get(id);
let model_to_load = models.get(id,);

if let Some((name, description)) = model_to_load {
if let Some((name, description,),) = model_to_load {
Model {
id: id.to_string(),
name: name.to_string(),
description: description.to_string(),
}
} else if children.eq(&Routes::NotFound) {
} else if children.eq(&Routes::NotFound,) {
let mut not_found_model = Model::default();
not_found_model.id = id.to_string();
not_found_model
@@ -34,7 +34,7 @@ pub fn init(
children: Routes::NotFound,
}
.to_url(),
));
),);
let mut not_found_model = Model::default();
not_found_model.id = id.to_string();
not_found_model
@@ -60,28 +60,28 @@ pub enum Routes {
NotFound,
}

pub fn update(_: Msg, _: &mut Model, _: &mut impl Orders<Msg>) {}
pub fn update(_: Msg, _: &mut Model, _: &mut impl Orders<Msg,>,) {}

pub fn view(routes: &Routes, model: &Model) -> Node<Msg> {
routes.view(model)
pub fn view(routes: &Routes, model: &Model,) -> Node<Msg,> {
routes.view(model,)
}
fn manager(model: &Model) -> Node<Msg> {
fn manager(model: &Model,) -> Node<Msg,> {
div![
"Management",
h3![&model.name],
br![],
p![&model.description]
]
}
fn root(model: &Model) -> Node<Msg> {
fn root(model: &Model,) -> Node<Msg,> {
div!["Root", h3![&model.name], br![], p![&model.description]]
}
fn not_found(model: &Model) -> Node<Msg> {
fn not_found(model: &Model,) -> Node<Msg,> {
div!["model not found with id ", span![&model.id]]
}

fn load_models() -> HashMap<String, (String, String)> {
let mut models: HashMap<String, (String, String)> = HashMap::new();
fn load_models() -> HashMap<String, (String, String,),> {
let mut models: HashMap<String, (String, String,),> = HashMap::new();

models.insert(
"1".to_string(),
12 changes: 6 additions & 6 deletions examples/advanced_routing/src/pages/dashboard/message.rs
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
use seed::{prelude::*, *};

pub fn init(_: Url, _: &mut Model, _: &mut impl Orders<Msg>) -> Model {
pub fn init(_: Url, _: &mut Model, _: &mut impl Orders<Msg,>,) -> Model {
Model::default()
}

#[derive(Default, Clone)]
pub struct Model {
pub messages: Vec<String>,
pub messages: Vec<String,>,
}

pub enum Msg {
AddMessage(String),
AddMessage(String,),
}
pub fn update(msg: Msg, _: &mut Model, _: &mut impl Orders<Msg>) {
pub fn update(msg: Msg, _: &mut Model, _: &mut impl Orders<Msg,>,) {
match msg {
Msg::AddMessage(_) => {}
Msg::AddMessage(_,) => {},
}
}
pub fn view(_: &Model) -> Node<Msg> {
pub fn view(_: &Model,) -> Node<Msg,> {
div!["messages list"]
}
36 changes: 19 additions & 17 deletions examples/advanced_routing/src/pages/dashboard/mod.rs
Original file line number Diff line number Diff line change
@@ -10,7 +10,7 @@ use seed_routing::*;
pub enum Routes {
Message,
Tasks {
query: IndexMap<String, String>,
query: IndexMap<String, String,>,
children: tasks::Routes,
},
Statistics,
@@ -19,8 +19,8 @@ pub enum Routes {
#[as_path = ""]
Root,
}
pub fn init(_: Url, model: &mut Model, nested: &Routes, orders: &mut impl Orders<Msg>) -> Model {
nested.init(model, orders);
pub fn init(_: Url, model: &mut Model, nested: &Routes, orders: &mut impl Orders<Msg,>,) -> Model {
nested.init(model, orders,);
model.clone()
}

@@ -34,29 +34,31 @@ pub struct Model {

pub enum Msg {
ChangeName,
Message(message::Msg),
Statistics(statistics::Msg),
Tasks(tasks::Msg),
Message(message::Msg,),
Statistics(statistics::Msg,),
Tasks(tasks::Msg,),
}

pub fn update(msg: Msg, model: &mut Model, orders: &mut impl Orders<Msg>) {
pub fn update(msg: Msg, model: &mut Model, orders: &mut impl Orders<Msg,>,) {
match msg {
Msg::ChangeName => {}
Msg::Message(message) => {
message::update(message, &mut model.message, &mut orders.proxy(Msg::Message))
}
Msg::Statistics(statistics) => statistics::update(
Msg::ChangeName => {},
Msg::Message(message,) => message::update(
message,
&mut model.message,
&mut orders.proxy(Msg::Message,),
),
Msg::Statistics(statistics,) => statistics::update(
statistics,
&mut model.statistics,
&mut orders.proxy(Msg::Statistics),
&mut orders.proxy(Msg::Statistics,),
),
Msg::Tasks(task) => tasks::update(task, &mut model.tasks, &mut orders.proxy(Msg::Tasks)),
Msg::Tasks(task,) => tasks::update(task, &mut model.tasks, &mut orders.proxy(Msg::Tasks,),),
}
}
pub fn view(dashboard_routes: &Routes, model: &Model) -> Node<Msg> {
dashboard_routes.view(model)
pub fn view(dashboard_routes: &Routes, model: &Model,) -> Node<Msg,> {
dashboard_routes.view(model,)
}

pub fn root(_: &Model) -> Node<Msg> {
pub fn root(_: &Model,) -> Node<Msg,> {
div!["root for dashboard"]
}
14 changes: 7 additions & 7 deletions examples/advanced_routing/src/pages/dashboard/statistics.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use seed::{prelude::*, *};

pub fn init(_: Url, _: &mut Model, orders: &mut impl Orders<Msg>) -> Model {
orders.subscribe(Msg::UrlChanged);
pub fn init(_: Url, _: &mut Model, orders: &mut impl Orders<Msg,>,) -> Model {
orders.subscribe(Msg::UrlChanged,);

Model::default()
}
@@ -12,15 +12,15 @@ pub struct Model {
}

pub enum Msg {
UrlChanged(subs::UrlChanged),
UrlChanged(subs::UrlChanged,),
}
pub fn update(msg: Msg, model: &mut Model, _: &mut impl Orders<Msg>) {
pub fn update(msg: Msg, model: &mut Model, _: &mut impl Orders<Msg,>,) {
match msg {
Msg::UrlChanged(_) => {
Msg::UrlChanged(_,) => {
model.routes_history_count += 1;
}
},
}
}
pub fn view(model: &Model) -> Node<Msg> {
pub fn view(model: &Model,) -> Node<Msg,> {
div!["route visited => {} ", &model.routes_history_count]
}
60 changes: 30 additions & 30 deletions examples/advanced_routing/src/pages/dashboard/tasks/mod.rs
Original file line number Diff line number Diff line change
@@ -8,19 +8,19 @@ pub mod task;
pub fn init(
_: Url,
model: &mut Model,
query: &IndexMap<String, String>,
query: &IndexMap<String, String,>,
_: &Routes,
_: &mut impl Orders<Msg>,
_: &mut impl Orders<Msg,>,
) -> Model {
if model.is_default {
let mut selected_no: Vec<u32> = vec![];
let mut selected_no: Vec<u32,> = vec![];
for selected in query.iter() {
if selected.0.contains("select") {
if selected.0.contains("select",) {
let no: u32 = selected
.1
.parse()
.expect("expect value from query parameters");
selected_no.push(no)
.expect("expect value from query parameters",);
selected_no.push(no,)
}
}
Model {
@@ -38,8 +38,8 @@ pub fn init(
}
#[derive(Clone)]
pub struct Model {
pub tasks: Vec<task::Model>,
pub checked_tasks_no: Vec<u32>,
pub tasks: Vec<task::Model,>,
pub checked_tasks_no: Vec<u32,>,
pub is_default: bool,
}

@@ -54,53 +54,53 @@ impl Default for Model {
}
#[derive(Debug, PartialEq, Clone, AsUrl)]
pub enum Routes {
Task { id: String },
Task { id: String, },
// #[as_path = ""] this makes run time error
Root,
}

#[derive(Debug, Copy, Clone)]
pub enum Msg {
ClickTask(u32, bool),
Task(task::Msg),
ClickTask(u32, bool,),
Task(task::Msg,),
LoadTasks,
}

pub fn update(msg: Msg, model: &mut Model, _: &mut impl Orders<Msg>) {
pub fn update(msg: Msg, model: &mut Model, _: &mut impl Orders<Msg,>,) {
match msg {
Msg::ClickTask(task_no, will_uncheck) => {
Msg::ClickTask(task_no, will_uncheck,) => {
if will_uncheck {
if let Some(index) = model.checked_tasks_no.iter().position(|no| no == &task_no) {
model.checked_tasks_no.remove(index);
if let Some(index,) = model.checked_tasks_no.iter().position(|no| no == &task_no,) {
model.checked_tasks_no.remove(index,);
}
} else {
model.checked_tasks_no.push(task_no)
model.checked_tasks_no.push(task_no,)
}
}
},
Msg::LoadTasks => model.tasks = get_dummy_data(),
Msg::Task(_) => {}
Msg::Task(_,) => {},
}
}

fn render_tasks(model: &Model) -> Node<Msg> {
fn render_tasks(model: &Model,) -> Node<Msg,> {
ul![list(&model.tasks, &model.checked_tasks_no)]
}

pub fn list(tasks: &[task::Model], list: &[u32]) -> Vec<Node<Msg>> {
pub fn list(tasks: &[task::Model], list: &[u32],) -> Vec<Node<Msg,>,> {
let mut tasks_list = Vec::new();
for t in tasks {
tasks_list.push(render_task(t, list.contains(&t.task_no)));
tasks_list.push(render_task(t, list.contains(&t.task_no,),),);
}
tasks_list
}

pub fn render_task(task: &task::Model, is_checked: bool) -> Node<Msg> {
pub fn render_task(task: &task::Model, is_checked: bool,) -> Node<Msg,> {
let task_url = Root::Dashboard(Parent::Tasks {
children: Routes::Task {
id: task.task_no.to_string(),
},
query: IndexMap::new(),
})
},)
.to_url();

let task_no = task.task_no;
@@ -123,10 +123,10 @@ pub fn render_task(task: &task::Model, is_checked: bool) -> Node<Msg> {
]
]]
}
fn is_current_url(url: Url) -> bool {
fn is_current_url(url: Url,) -> bool {
Url::current() == url
}
pub fn get_dummy_data() -> Vec<task::Model> {
pub fn get_dummy_data() -> Vec<task::Model,> {
vec![
task::Model {
task_no: 0,
@@ -145,14 +145,14 @@ pub fn get_dummy_data() -> Vec<task::Model> {
},
]
}
pub fn view(task_routes: &Routes, model: &Model) -> Node<Msg> {
pub fn view(task_routes: &Routes, model: &Model,) -> Node<Msg,> {
div![vec![
render_tasks(model),
match task_routes {
Routes::Task { id } => {
let task = model.tasks.iter().find(|t| t.task_no.to_string() == *id);
task::view(task.unwrap()).map_msg(Msg::Task)
}
Routes::Task { id, } => {
let task = model.tasks.iter().find(|t| t.task_no.to_string() == *id,);
task::view(task.unwrap(),).map_msg(Msg::Task,)
},
Routes::Root => div!["no task selected"],
},
]]
4 changes: 2 additions & 2 deletions examples/advanced_routing/src/pages/dashboard/tasks/task.rs
Original file line number Diff line number Diff line change
@@ -8,7 +8,7 @@ pub struct Model {
}

impl Clone for Model {
fn clone(&self) -> Self {
fn clone(&self,) -> Self {
Model {
task_no: self.task_no,
task_title: self.task_title.clone(),
@@ -21,7 +21,7 @@ impl Clone for Model {
pub enum Msg {
ClickTask,
}
pub fn view(model: &Model) -> Node<Msg> {
pub fn view(model: &Model,) -> Node<Msg,> {
div![
"Title",
h3![model.task_title.to_string()],
42 changes: 23 additions & 19 deletions examples/advanced_routing/src/pages/login/mod.rs
Original file line number Diff line number Diff line change
@@ -1,24 +1,28 @@
use crate::models::auth::LoginCredentials;
use crate::models::user::{LoggedData, Role};
use crate::request::State;
use crate::{
models::{
auth::LoginCredentials,
user::{LoggedData, Role},
},
request::State,
};
use seed::{prelude::*, *};

/// Can trigger specific update when loading the page
pub fn init(
_: Url,
_: &mut Model,
query: &IndexMap<String, String>,
_: &mut impl Orders<Msg>,
query: &IndexMap<String, String,>,
_: &mut impl Orders<Msg,>,
) -> Model {
let name = query.get("name");
let name = query.get("name",);

if let Some(name_from_query) = name {
if let Some(name_from_query,) = name {
let mut model = Model {
credentials: Default::default(),
request_state: Default::default(),
};

model.credentials.set_target(name_from_query.to_string());
model.credentials.set_target(name_from_query.to_string(),);
model
} else {
Model {
@@ -31,16 +35,16 @@ pub fn init(
#[derive(Default, Debug)]
pub struct Model {
credentials: LoginCredentials,
request_state: State<LoggedData>,
request_state: State<LoggedData,>,
}

pub enum Msg {
AutoLogin(Role),
AutoLogin(Role,),
}

pub fn update(msg: Msg, model: &mut Model, orders: &mut impl Orders<Msg>) {
pub fn update(msg: Msg, model: &mut Model, orders: &mut impl Orders<Msg,>,) {
match msg {
Msg::AutoLogin(role) => {
Msg::AutoLogin(role,) => {
let logged_user = match role {
Role::StandardUser => LoggedData::new(
"John",
@@ -57,25 +61,25 @@ pub fn update(msg: Msg, model: &mut Model, orders: &mut impl Orders<Msg>) {
Role::Admin,
),
};
model.request_state = State::Success(logged_user.clone());
orders.notify(logged_user);
}
model.request_state = State::Success(logged_user.clone(),);
orders.notify(logged_user,);
},
}
}
pub fn view(model: &Model) -> Node<Msg> {
pub fn view(model: &Model,) -> Node<Msg,> {
match &model.request_state {
State::Success(user) => div![p![
State::Success(user,) => div![p![
C!["centred"],
"Welcome ",
style! {St::Color => "darkblue"},
user.username(),
". :)"
]],
State::IsPending(status) => form(model, status),
State::IsPending(status,) => form(model, status,),
}
}

fn form(model: &Model, status: &bool) -> Node<Msg> {
fn form(model: &Model, status: &bool,) -> Node<Msg,> {
form![
fieldset![
attrs! {
10 changes: 5 additions & 5 deletions examples/advanced_routing/src/request.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
#[derive(Debug)]
pub enum State<T> {
Success(T),
IsPending(bool),
pub enum State<T,> {
Success(T,),
IsPending(bool,),
}

impl<T> Default for State<T> {
impl<T,> Default for State<T,> {
fn default() -> Self {
State::IsPending(false)
State::IsPending(false,)
}
}
48 changes: 24 additions & 24 deletions examples/advanced_routing/src/top_bar.rs
Original file line number Diff line number Diff line change
@@ -6,54 +6,54 @@ use web_sys::HtmlElement;

/// The top bar is the component used for navigation, user actions and title
/// located on the top of the applicatiob
pub struct TopBar<Ms: 'static> {
title: Option<Cow<'static, str>>,
pub struct TopBar<Ms: 'static,> {
title: Option<Cow<'static, str,>,>,
style: Theme,
block: bool,
attrs: Attrs,
user_logged_in: bool,
disabled: bool,
content: Vec<Node<Ms>>,
el_ref: ElRef<HtmlElement>,
content: Vec<Node<Ms,>,>,
el_ref: ElRef<HtmlElement,>,
css: Css,
}

impl<Ms: 'static> TopBar<Ms> {
pub fn new(title: impl Into<Cow<'static, str>>) -> Self {
Self::default().title(title)
impl<Ms: 'static,> TopBar<Ms,> {
pub fn new(title: impl Into<Cow<'static, str,>,>,) -> Self {
Self::default().title(title,)
}

pub fn title(mut self, title: impl Into<Cow<'static, str>>) -> Self {
self.title = Some(title.into());
pub fn title(mut self, title: impl Into<Cow<'static, str,>,>,) -> Self {
self.title = Some(title.into(),);
self
}

// --- style ---

pub const fn style(mut self, style: Theme) -> Self {
pub const fn style(mut self, style: Theme,) -> Self {
self.style = style;
self
}

pub const fn set_user_login_state(mut self, is_user_logged_in: bool) -> Self {
pub const fn set_user_login_state(mut self, is_user_logged_in: bool,) -> Self {
self.user_logged_in = is_user_logged_in;
self
}

pub fn content(mut self, content: impl IntoNodes<Ms>) -> Self {
pub fn content(mut self, content: impl IntoNodes<Ms,>,) -> Self {
self.content = content.into_nodes();
self
}

fn view(mut self) -> Node<Ms> {
fn view(mut self,) -> Node<Ms,> {
let tag = Tag::Div;
let content = div![self.title.take().map(Node::new_text),];
let attrs = {
let mut attrs = attrs! {};

if self.disabled {
attrs.add(At::from("aria-disabled"), true);
attrs.add(At::TabIndex, -1);
attrs.add(At::from("aria-disabled",), true,);
attrs.add(At::TabIndex, -1,);
}
attrs
};
@@ -84,20 +84,20 @@ impl<Ms: 'static> TopBar<Ms> {

St::BackgroundColor => "transparent",
St::Border => format!("{} {} {}", px(5), "solid", color),
});
},);
} else {
css.merge(style! { St::Color => font_color,
St::BackgroundColor => background });
St::BackgroundColor => background },);
};

if self.block {
css.merge(style! {St::Display => "block"});
css.merge(style! {St::Display => "block"},);
}

if self.disabled {
css.merge(style! {St::Opacity => 0.5});
css.merge(style! {St::Opacity => 0.5},);
} else {
css.merge(style! {St::Cursor => "pointer"});
css.merge(style! {St::Cursor => "pointer"},);
}

css
@@ -117,7 +117,7 @@ impl<Ms: 'static> TopBar<Ms> {
top_bar
}
}
impl<Ms> Default for TopBar<Ms> {
impl<Ms,> Default for TopBar<Ms,> {
fn default() -> Self {
Self {
title: None,
@@ -133,8 +133,8 @@ impl<Ms> Default for TopBar<Ms> {
}
}

impl<Ms> UpdateEl<Ms> for TopBar<Ms> {
fn update_el(self, el: &mut El<Ms>) {
self.view().update_el(el)
impl<Ms,> UpdateEl<Ms,> for TopBar<Ms,> {
fn update_el(self, el: &mut El<Ms,>,) {
self.view().update_el(el,)
}
}