Skip to content

Commit eb562ce

Browse files
authored
Merge pull request #2 from Picea/codex/create-navbar-navigation-for-anissa
Add navigation bar for Conduit demo
2 parents ff857ad + 3ccd893 commit eb562ce

File tree

2 files changed

+65
-12
lines changed

2 files changed

+65
-12
lines changed

Abies.Conduit/Main.cs

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using Abies.Conduit.Routing;
22
using Abies.Conduit.Services;
3+
using Abies.DOM;
34
using System.Threading.Tasks;
45
using static Abies.Conduit.Main.Message.Event;
56
using static Abies.UrlRequest;
@@ -222,24 +223,32 @@ public static (Model model, IEnumerable<Command> commands) Update(Abies.Message
222223
}
223224

224225
}
225-
226-
226+
private static Node WithLayout(Node page, Model model) =>
227+
div([], [
228+
Navigation.View(model),
229+
page
230+
]);
227231

228232
public static Document View(Model model)
229233
=> model.Page switch
230234
{
231-
Page.Redirect => new Document(string.Format(Title, "Redirect"), h1([], [text("Redirecting...")])),
232-
Page.NotFound => new Document(string.Format(Title, "Not Found"), h1([], [text("Not Found")])),
233-
Page.Home home => new Document(string.Format(Title, nameof(Conduit.Page.Home)), Conduit.Page.Home.Page.View(home.Model)),
234-
Page.Settings settings => new Document(string.Format(Title, nameof(Conduit.Page.Settings)), Conduit.Page.Settings.Page.View(settings.Model)),
235-
Page.Login login => new Document(string.Format(Title, nameof(Conduit.Page.Login)), Conduit.Page.Login.Page.View(login.Model)),
236-
Page.Register register => new Document(string.Format(Title, nameof(Conduit.Page.Register)), Conduit.Page.Register.Page.View(register.Model)), Page.Profile profile => new Document(string.Format(Title, nameof(Conduit.Page.Profile)), Conduit.Page.Profile.Page.View(profile.Model)),
237-
Page.ProfileFavorites profileFavorites => new Document(string.Format(Title, "Profile Favorites"), Conduit.Page.Profile.Page.View(profileFavorites.Model)),
238-
Page.Article article => new Document(string.Format(Title, nameof(Conduit.Page.Article)), Conduit.Page.Article.Page.View(article.Model)),
239-
Page.NewArticle newArticle => new Document(string.Format(Title, "New Article"), Conduit.Page.Editor.Page.View(newArticle.Model)),
240-
_ => new Document(string.Format(Title, "Not Found"), h1([], [text("Not Found")]))
235+
Page.Redirect => new Document(string.Format(Title, "Redirect"), WithLayout(h1([], [text("Redirecting...")]), model)),
236+
Page.NotFound => new Document(string.Format(Title, "Not Found"), WithLayout(h1([], [text("Not Found")]), model)),
237+
Page.Home home => new Document(string.Format(Title, nameof(Conduit.Page.Home)), WithLayout(Conduit.Page.Home.Page.View(home.Model), model)),
238+
Page.Settings settings => new Document(string.Format(Title, nameof(Conduit.Page.Settings)), WithLayout(Conduit.Page.Settings.Page.View(settings.Model), model)),
239+
Page.Login login => new Document(string.Format(Title, nameof(Conduit.Page.Login)), WithLayout(Conduit.Page.Login.Page.View(login.Model), model)),
240+
Page.Register register => new Document(string.Format(Title, nameof(Conduit.Page.Register)), WithLayout(Conduit.Page.Register.Page.View(register.Model), model)),
241+
Page.Profile profile => new Document(string.Format(Title, nameof(Conduit.Page.Profile)), WithLayout(Conduit.Page.Profile.Page.View(profile.Model), model)),
242+
Page.ProfileFavorites profileFavorites => new Document(string.Format(Title, "Profile Favorites"), WithLayout(Conduit.Page.Profile.Page.View(profileFavorites.Model), model)),
243+
Page.Article article => new Document(string.Format(Title, nameof(Conduit.Page.Article)), WithLayout(Conduit.Page.Article.Page.View(article.Model), model)),
244+
Page.NewArticle newArticle => new Document(string.Format(Title, "New Article"), WithLayout(Conduit.Page.Editor.Page.View(newArticle.Model), model)),
245+
_ => new Document(string.Format(Title, "Not Found"), WithLayout(h1([], [text("Not Found")]), model))
241246
};
242247

248+
249+
250+
251+
243252
public static Abies.Message OnUrlChanged(Url url)
244253
=> new UrlChanged(url);
245254

Abies.Conduit/Navigation.cs

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
using Abies.Conduit.Main;
2+
using Abies.Conduit.Routing;
3+
using Abies.DOM;
4+
using static Abies.Html.Elements;
5+
using static Abies.Html.Attributes;
6+
7+
namespace Abies.Conduit;
8+
9+
public static class Navigation
10+
{
11+
private static Node NavLink(string url, string label, bool active) =>
12+
li([class_("nav-item")], [
13+
a([class_(active ? "nav-link active" : "nav-link"), href(url)], [text(label)])
14+
]);
15+
16+
public static Node View(Model model)
17+
{
18+
var userLinks = model.CurrentUser == null
19+
? new Node[]
20+
{
21+
NavLink("/login", "Sign in", model.CurrentRoute is Route.Login),
22+
NavLink("/register", "Sign up", model.CurrentRoute is Route.Register)
23+
}
24+
: new Node[]
25+
{
26+
NavLink("/editor", "New Article", model.CurrentRoute is Route.NewArticle),
27+
NavLink("/settings", "Settings", model.CurrentRoute is Route.Settings),
28+
NavLink($"/profile/{model.CurrentUser!.Username.Value}",
29+
model.CurrentUser.Username.Value,
30+
model.CurrentRoute is Route.Profile p && p.UserName.Value == model.CurrentUser.Username.Value ||
31+
model.CurrentRoute is Route.ProfileFavorites pf && pf.UserName.Value == model.CurrentUser.Username.Value)
32+
};
33+
34+
return nav([class_("navbar navbar-light")], [
35+
div([class_("container")], [
36+
a([class_("navbar-brand"), href("/")], [text("conduit")]),
37+
ul([class_("nav navbar-nav pull-xs-right")], [
38+
NavLink("/", "Home", model.CurrentRoute is Route.Home),
39+
..userLinks
40+
])
41+
])
42+
]);
43+
}
44+
}

0 commit comments

Comments
 (0)