Skip to content

Commit 75c1ca1

Browse files
committed
Side Navigation ViewModel
1 parent 4ae759a commit 75c1ca1

File tree

6 files changed

+242
-0
lines changed

6 files changed

+242
-0
lines changed
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
namespace LearningHub.Nhs.WebUI.Models.SideMenu
2+
{
3+
using System.Collections.Generic;
4+
using Microsoft.AspNetCore.Routing;
5+
6+
/// <summary>
7+
/// Defines the <see cref="SideNavViewModel" />.
8+
/// </summary>
9+
public class SideNavViewModel
10+
{
11+
/// <summary>
12+
/// Gets or sets the Groups.
13+
/// </summary>
14+
public List<SideNavigationGroup> Groups { get; set; } = [];
15+
16+
/// <summary>
17+
/// Gets or sets the RouteData.
18+
/// </summary>
19+
public RouteValueDictionary RouteData { get; set; } = [];
20+
}
21+
}
Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
namespace LearningHub.Nhs.WebUI.Models.SideMenu
2+
{
3+
using System;
4+
using System.Collections.Generic;
5+
using System.Linq;
6+
using Microsoft.AspNetCore.Routing;
7+
8+
/// <summary>
9+
/// Defines the <see cref="SideNavigationConfiguration" />.
10+
/// </summary>
11+
public static class SideNavigationConfiguration
12+
{
13+
/// <summary>
14+
/// GetGroupedMenus.
15+
/// </summary>
16+
/// <returns>IEnumerable.</returns>
17+
public static IEnumerable<SideNavigationGroup> GetGroupedMenus()
18+
{
19+
return new List<SideNavigationGroup>
20+
{
21+
new SideNavigationGroup
22+
{
23+
GroupTitle = "Account",
24+
Items = new List<SideNavigationItem>
25+
{
26+
new SideNavigationItem
27+
{
28+
Text = "Personal details",
29+
Controller = "Account",
30+
Action = "PersonalDetails",
31+
IsActive = route => MatchRoute(route, "Account", "PersonalDetails"),
32+
},
33+
new SideNavigationItem
34+
{
35+
Text = "My employment",
36+
Controller = "Account",
37+
Action = "MyEmployment",
38+
IsActive = route => MatchRoute(route, "Account", "MyEmployment"),
39+
},
40+
new SideNavigationItem
41+
{
42+
Text = "Security",
43+
Controller = "Account",
44+
Action = "Security",
45+
IsActive = route => MatchRoute(route, "Account", "Security"),
46+
},
47+
new SideNavigationItem
48+
{
49+
Text = "Notification",
50+
Controller = "Account",
51+
Action = "Notification",
52+
IsActive = route => MatchRoute(route, "Account", "Notification"),
53+
},
54+
},
55+
},
56+
new SideNavigationGroup
57+
{
58+
GroupTitle = "Activity",
59+
Items = new List<SideNavigationItem>
60+
{
61+
new SideNavigationItem
62+
{
63+
Text = "Recent",
64+
Controller = "Activity",
65+
Action = "Recent",
66+
IsActive = route => MatchRoute(route, "Activity", "Recent"),
67+
},
68+
new SideNavigationItem
69+
{
70+
Text = "Bookmark",
71+
Controller = "Activity",
72+
Action = "Bookmark",
73+
IsActive = route => MatchRoute(route, "Activity", "Bookmark"),
74+
},
75+
new SideNavigationItem
76+
{
77+
Text = "Certificates",
78+
Controller = "Activity",
79+
Action = "Certificates",
80+
IsActive = route => MatchRoute(route, "Activity", "Certificates"),
81+
},
82+
new SideNavigationItem
83+
{
84+
Text = "Learning history",
85+
Controller = "Activity",
86+
Action = "Learninghistory",
87+
IsActive = route => MatchRoute(route, "Activity", "Learninghistory"),
88+
},
89+
},
90+
},
91+
};
92+
}
93+
94+
/// <summary>
95+
/// GetMenuGroupByTitle.
96+
/// </summary>
97+
/// <param name="title">title.</param>
98+
/// <returns>string.</returns>
99+
public static SideNavigationGroup? GetMenuGroupByTitle(string title)
100+
{
101+
return GetGroupedMenus().FirstOrDefault(g =>
102+
string.Equals(g.GroupTitle, title, StringComparison.OrdinalIgnoreCase));
103+
}
104+
105+
private static bool MatchRoute(RouteValueDictionary route, string controller, string action)
106+
{
107+
var currentController = route["controller"]?.ToString();
108+
var currentAction = route["action"]?.ToString();
109+
110+
return string.Equals(currentController, controller, StringComparison.OrdinalIgnoreCase) &&
111+
string.Equals(currentAction, action, StringComparison.OrdinalIgnoreCase);
112+
}
113+
}
114+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
namespace LearningHub.Nhs.WebUI.Models.SideMenu
2+
{
3+
using System.Collections.Generic;
4+
5+
/// <summary>
6+
/// Defines the <see cref="SideNavigationGroup" />.
7+
/// </summary>
8+
public class SideNavigationGroup
9+
{
10+
/// <summary>
11+
/// Gets or sets a value indicating GroupTitle.
12+
/// </summary>
13+
public string GroupTitle { get; set; } = string.Empty;
14+
15+
/// <summary>
16+
/// Gets or sets a Items.
17+
/// </summary>
18+
public List<SideNavigationItem> Items { get; set; } = [];
19+
}
20+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
namespace LearningHub.Nhs.WebUI.Models.SideMenu
2+
{
3+
using System;
4+
using Microsoft.AspNetCore.Routing;
5+
6+
/// <summary>
7+
/// Defines the <see cref="SideNavigationItem" />.
8+
/// </summary>
9+
public class SideNavigationItem
10+
{
11+
/// <summary>
12+
/// Gets or sets a value indicating Text.
13+
/// </summary>
14+
public string Text { get; set; } = string.Empty;
15+
16+
/// <summary>
17+
/// Gets or sets a value indicating Controller.
18+
/// </summary>
19+
public string Controller { get; set; } = string.Empty;
20+
21+
/// <summary>
22+
/// Gets or sets a value indicating Action.
23+
/// </summary>
24+
public string Action { get; set; } = "Index";
25+
26+
/// <summary>
27+
/// Gets or sets a value indicating IsActiven.
28+
/// </summary>
29+
public Func<RouteValueDictionary, bool> IsActive { get; set; }
30+
}
31+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
namespace LearningHub.Nhs.WebUI.ViewComponents
2+
{
3+
using System.Collections.Generic;
4+
using System.Linq;
5+
using LearningHub.Nhs.WebUI.Models.SideMenu;
6+
using Microsoft.AspNetCore.Mvc;
7+
using Microsoft.AspNetCore.Mvc.Rendering;
8+
9+
/// <summary>
10+
/// Initializes a new instance of the <see cref="SideNavViewComponent"/> class.
11+
/// </summary>
12+
public class SideNavViewComponent : ViewComponent
13+
{
14+
/// <summary>
15+
/// The Invoke.
16+
/// </summary>
17+
/// <param name="groupTitle">group Title.</param>
18+
/// <returns>A representing the result of the synchronous operation.</returns>
19+
public IViewComponentResult Invoke(string? groupTitle = null)
20+
{
21+
var routeData = this.ViewContext.RouteData.Values;
22+
var groups = string.IsNullOrEmpty(groupTitle)
23+
? SideNavigationConfiguration.GetGroupedMenus().ToList()
24+
: SideNavigationConfiguration.GetMenuGroupByTitle(groupTitle) is { } singleGroup
25+
? new List<SideNavigationGroup> { singleGroup }
26+
: new List<SideNavigationGroup>();
27+
28+
var viewModel = new SideNavViewModel
29+
{
30+
Groups = groups,
31+
RouteData = routeData,
32+
};
33+
return this.View(viewModel);
34+
}
35+
}
36+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
@using LearningHub.Nhs.WebUI.Models.SideMenu
2+
@model SideNavViewModel
3+
4+
@foreach (var group in Model.Groups)
5+
{
6+
<ul class="nhsuk-list side-nav__list">
7+
@foreach (var item in group.Items)
8+
{
9+
var isActive = item.IsActive?.Invoke(Model.RouteData) ?? false;
10+
<li class="@(isActive ? "selected" : "")">
11+
<a asp-controller="@item.Controller" asp-action="@item.Action">
12+
@item.Text
13+
</a>
14+
</li>
15+
}
16+
</ul>
17+
}
18+
19+
20+
@* @await Component.InvokeAsync("SideNav", new { groupTitle = "Account" }) *@

0 commit comments

Comments
 (0)