diff options
author | Vidhu Kant Sharma <vidhukant@vidhukant.com> | 2023-08-30 10:38:00 +0530 |
---|---|---|
committer | Vidhu Kant Sharma <vidhukant@vidhukant.com> | 2023-08-30 10:38:00 +0530 |
commit | cdd1159b2e78c4a76fde0dd4a45374dddfe00f1b (patch) | |
tree | dee0744ff3b0254de5a321d67001166749251da1 | |
parent | 5d510a8258c07598ab4bfe840c820035583bd0a8 (diff) |
added conditional navbar links
-rw-r--r-- | src/components/navbar/mod.rs | 5 | ||||
-rw-r--r-- | src/components/navbar/nav.rs | 86 |
2 files changed, 88 insertions, 3 deletions
diff --git a/src/components/navbar/mod.rs b/src/components/navbar/mod.rs index 3aaeb72..7dbc08d 100644 --- a/src/components/navbar/mod.rs +++ b/src/components/navbar/mod.rs @@ -15,6 +15,7 @@ * along with this program. If not, see <https://www.gnu.org/licenses/>. */ +mod nav; mod nav_link; use yew::prelude::*; @@ -33,9 +34,7 @@ pub fn navbar() -> Html { </Link<Route>> </span> - <nav> - <nav_link::NavLink label={"Home"} to={Route::Home} /> - </nav> + <nav::Nav /> </div> </div> } diff --git a/src/components/navbar/nav.rs b/src/components/navbar/nav.rs new file mode 100644 index 0000000..0cdc029 --- /dev/null +++ b/src/components/navbar/nav.rs @@ -0,0 +1,86 @@ +/* openbills-web - Web client for Libre Billing Software + * Copyright (C) 2023 Vidhu Kant Sharma <vidhukant@vidhukant.com> + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see <https://www.gnu.org/licenses/>. + */ + +use yew::prelude::*; +use yew_router::prelude::*; + +use crate::app::Route; +use crate::components::navbar::nav_link::NavLink; + +#[derive(PartialEq)] +struct Link { + pub label: String, + pub to: Route, +} + +// shows different links based on the current route +#[function_component(Nav)] +pub fn nav() -> Html { + let current_route = use_route::<Route>().unwrap_or_default(); + + // fallback to this set of links + let default_links = Vec::from([ + Link { + label: String::from("Home"), + to: Route::Home, + }, + Link { + label: String::from("Manage"), + to: Route::ManageMenu, + }, + ]); + + let manage_links = Vec::from([ + Link { + label: String::from("Home"), + to: Route::Home, + }, + Link { + label: String::from("Clients"), + to: Route::ManageClients, + }, + Link { + label: String::from("Brands"), + to: Route::ManageBrands, + }, + Link { + label: String::from("Items"), + to: Route::ManageItems, + }, + Link { + label: String::from("Invoices"), + to: Route::ManageInvoices, + }, + ]); + + let links_html: Html = match current_route { + Route::ManageClients + | Route::ManageBrands + | Route::ManageItems + | Route::ManageInvoices => manage_links, + _ => default_links, + } + .iter() + .map(|link| html!(<NavLink label={link.label.clone()} to={link.to.clone()} />)) + .collect(); + + html! { + <nav> + {links_html} + </nav> + } +} |