Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
49 changes: 31 additions & 18 deletions website/MyWebApp/Controllers/AdminBlockTemplateController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using Microsoft.EntityFrameworkCore;
using Microsoft.AspNetCore.Http;
using System.Collections.Generic;
using System.Linq;
using System.IO;
using System.Text.Json;
using MyWebApp.Data;
Expand Down Expand Up @@ -160,14 +161,19 @@ public async Task<IActionResult> AddToPage(int id)

[HttpPost]
[ValidateAntiForgeryToken]
public async Task<IActionResult> AddToPage(int id, int pageId, string zone, int? roleId)
public async Task<IActionResult> AddToPage(int id, List<int> pageIds, string zone, string role)
{
var template = await _db.BlockTemplates.FindAsync(id);
var page = await _db.Pages.FindAsync(pageId);
if (template == null || page == null)
if (template == null) return NotFound();
if (pageIds == null || pageIds.Count == 0)
{
return NotFound();
await LoadPagesAsync();
ViewBag.BlockId = id;
ModelState.AddModelError("pageIds", "Page selection required");
return View();
}
var roleEntity = await _db.Roles.FirstOrDefaultAsync(r => r.Name == role);
int? roleId = roleEntity?.Id;
zone = zone?.Trim() ?? string.Empty;
if (string.IsNullOrEmpty(zone))
{
Expand All @@ -176,21 +182,28 @@ public async Task<IActionResult> AddToPage(int id, int pageId, string zone, int?
ModelState.AddModelError("zone", "Zone required");
return View();
}
var sort = await _db.PageSections
.Where(s => s.PageId == pageId && s.Zone == zone)
.Select(s => s.SortOrder)
.DefaultIfEmpty(-1)
.MaxAsync() + 1;
var section = new PageSection
if (pageIds.Contains(0))
{
PageId = pageId,
Zone = zone,
SortOrder = sort,
Html = template.Html,
Type = PageSectionType.Html,
RoleId = roleId
};
_db.PageSections.Add(section);
pageIds = await _db.Pages.Select(p => p.Id).ToListAsync();
}
foreach (var pageId in pageIds)
{
var sort = await _db.PageSections
.Where(s => s.PageId == pageId && s.Zone == zone)
.Select(s => s.SortOrder)
.DefaultIfEmpty(-1)
.MaxAsync() + 1;
var section = new PageSection
{
PageId = pageId,
Zone = zone,
SortOrder = sort,
Html = template.Html,
Type = PageSectionType.Html,
RoleId = roleId
};
_db.PageSections.Add(section);
}
await _db.SaveChangesAsync();
return RedirectToAction(nameof(Index));
}
Expand Down
33 changes: 25 additions & 8 deletions website/MyWebApp/Views/AdminBlockTemplate/AddToPage.cshtml
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,14 @@
<form asp-action="AddToPage" method="post">
<input type="hidden" name="id" value="@ViewBag.BlockId" />
<div>
<label>Page</label>
<select name="pageId">
<label>Associated Page(s)</label>
<input type="text" id="page-display" readonly />
</div>
<div>
<label>Assign To Pages</label>
<select id="page-select" name="pageIds" multiple size="5">
<option value="0">All Pages</option>
@foreach (var p in ViewBag.Pages as List<Page>)

Check warning on line 17 in website/MyWebApp/Views/AdminBlockTemplate/AddToPage.cshtml

View workflow job for this annotation

GitHub Actions / build

Dereference of a possibly null reference.
{
<option value="@p.Id">@p.Slug</option>
}
Expand All @@ -21,13 +26,25 @@
</div>
<div>
<label>Role</label>
<select name="roleId">
<option value="">(none)</option>
@foreach (var r in ViewBag.Roles as List<Role>)
{
<option value="@r.Id">@r.Name</option>
}
<select name="role" required>
<option value="">Select role</option>
<option value="Admin">Admin</option>
<option value="Editor">Editor</option>
<option value="Viewer">Viewer</option>
</select>
</div>
<button type="submit">Add</button>
</form>

@section Scripts {
<script>
const select = document.getElementById('page-select');
const display = document.getElementById('page-display');
function updateDisplay() {
const names = Array.from(select.selectedOptions).map(o => o.text);
display.value = names.join(', ');
}
select.addEventListener('change', updateDisplay);
updateDisplay();
</script>
}
4 changes: 4 additions & 0 deletions website/MyWebApp/Views/AdminPageSection/Edit.cshtml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@
<h2>Edit Section</h2>
<form asp-action="Edit" method="post" enctype="multipart/form-data">
<input type="hidden" asp-for="Id" />
<div>
<label>Associated Page</label>
<input type="text" readonly value="@pages.FirstOrDefault(p => p.Id == Model.PageId)?.Slug" />

Check warning on line 14 in website/MyWebApp/Views/AdminPageSection/Edit.cshtml

View workflow job for this annotation

GitHub Actions / build

Possible null reference argument for parameter 'source' in 'Page? Enumerable.FirstOrDefault<Page>(IEnumerable<Page> source, Func<Page, bool> predicate)'.
</div>
<div>
<label>Page</label>
<select asp-for="PageId" asp-items="@(new SelectList(pages, "Id", "Slug"))"></select>
Expand Down
11 changes: 5 additions & 6 deletions website/MyWebApp/Views/Shared/_SectionEditor.cshtml
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,11 @@
</div>
<div>
<label>Role</label>
<select name="@($"{prefix}RoleId")">
<option value="">(none)</option>
@foreach (var r in roles)
{
<option value="@r.Id" selected="@(Model.RoleId == r.Id)">@r.Name</option>
}
<select name="@($"{prefix}RoleId")" required>
<option value="">Select role</option>
<option value="1" selected="@(Model.RoleId == 1)">Admin</option>
<option value="2" selected="@(Model.RoleId == 2)">Editor</option>
<option value="3" selected="@(Model.RoleId == 3)">Viewer</option>
</select>
</div>
<div>
Expand Down
Loading