Skip to content

Commit

Permalink
Cleanup the SMS settings (#14171)
Browse files Browse the repository at this point in the history
  • Loading branch information
MikeAlhayek authored Aug 18, 2023
1 parent 1e350a8 commit 2349676
Show file tree
Hide file tree
Showing 7 changed files with 18 additions and 41 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@ namespace OrchardCore.Sms.Drivers;

public class SmsSettingsDisplayDriver : SectionDisplayDriver<ISite, SmsSettings>
{
public const string GroupId = "sms";

private readonly IHttpContextAccessor _httpContextAccessor;
private readonly IAuthorizationService _authorizationService;
private readonly IShellHost _shellHost;
Expand Down Expand Up @@ -55,14 +53,14 @@ public override async Task<IDisplayResult> EditAsync(SmsSettings settings, Build
.Select(provider => new SelectListItem(provider, provider))
.ToArray();
}).Location("Content:1")
.OnGroup(GroupId);
.OnGroup(SmsConstants.SettingsGroupId);
}

public override async Task<IDisplayResult> UpdateAsync(SmsSettings settings, BuildEditorContext context)
{
var user = _httpContextAccessor.HttpContext?.User;

if (!context.GroupId.Equals(GroupId, StringComparison.OrdinalIgnoreCase)
if (!context.GroupId.Equals(SmsConstants.SettingsGroupId, StringComparison.OrdinalIgnoreCase)
|| !await _authorizationService.AuthorizeAsync(user, SmsPermissions.ManageSmsSettings))
{
return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,14 +52,14 @@ public override async Task<IDisplayResult> EditAsync(TwilioSettings settings, Bu
model.AccountSID = settings.AccountSID;
model.HasAuthToken = !String.IsNullOrEmpty(settings.AuthToken);
}).Location("Content:5")
.OnGroup(SmsSettingsDisplayDriver.GroupId);
.OnGroup(SmsConstants.SettingsGroupId);
}

public override async Task<IDisplayResult> UpdateAsync(TwilioSettings settings, BuildEditorContext context)
{
var user = _httpContextAccessor.HttpContext?.User;

if (!context.GroupId.Equals(SmsSettingsDisplayDriver.GroupId, StringComparison.OrdinalIgnoreCase)
if (!context.GroupId.Equals(SmsConstants.SettingsGroupId, StringComparison.OrdinalIgnoreCase)
|| !await _authorizationService.AuthorizeAsync(user, SmsPermissions.ManageSmsSettings))
{
return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,14 @@
using Microsoft.Extensions.Localization;
using OrchardCore.Navigation;
using OrchardCore.Sms;
using OrchardCore.Sms.Drivers;

namespace OrchardCore.Admin;

public class AdminMenu : INavigationProvider
public class SmsAdminMenu : INavigationProvider
{
protected readonly IStringLocalizer S;

public AdminMenu(IStringLocalizer<AdminMenu> stringLocalizer)
public SmsAdminMenu(IStringLocalizer<SmsAdminMenu> stringLocalizer)
{
S = stringLocalizer;
}
Expand All @@ -29,7 +28,7 @@ public Task BuildNavigationAsync(string name, NavigationBuilder builder)
.Add(S["SMS"], S["SMS"].PrefixPosition(), sms => sms
.AddClass("sms")
.Id("sms")
.Action("Index", "Admin", new { area = "OrchardCore.Settings", groupId = SmsSettingsDisplayDriver.GroupId })
.Action("Index", "Admin", new { area = "OrchardCore.Settings", groupId = SmsConstants.SettingsGroupId })
.Permission(SmsPermissions.ManageSmsSettings)
.LocalNav()
)
Expand Down
2 changes: 1 addition & 1 deletion src/OrchardCore.Modules/OrchardCore.Sms/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public override void ConfigureServices(IServiceCollection services)
}

services.AddScoped<IPermissionProvider, SmsPermissionProvider>();
services.AddScoped<INavigationProvider, AdminMenu>();
services.AddScoped<INavigationProvider, SmsAdminMenu>();
services.AddScoped<IDisplayDriver<ISite>, SmsSettingsDisplayDriver>();
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
<span asp-validation-for="DefaultProvider"></span>
</div>

<script at="Foot">
<script at="Foot" asp-name="sms-settings-providers">
document.addEventListener('DOMContentLoaded', () => {
let menu = document.getElementById('@Html.IdFor(m => m.DefaultProvider)');
Expand All @@ -19,13 +19,12 @@
menu.addEventListener('change', (e) => {
for (let i = 0; i < settingContainers.length; i++) {
settingContainers[i].classList.add('d-none');
}
let wrapper = document.getElementById('SmsSettingsContainer' + e.target.value);
if (wrapper) {
wrapper.classList.remove('d-none');
let container = settingContainers[i];
if (container.getAttribute('data-provider-name') == e.target.value) {
container.classList.remove('d-none');
} else {
container.classList.add('d-none');
}
}
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

@model TwilioSettingsViewModel

<div class="sms-settings-container d-none" id="SmsSettingsContainer@(SmsConstants.TwilioServiceName)">
<div class="sms-settings-container d-none" data-provider-name="@(SmsConstants.TwilioServiceName)">
<h4>@T["Twilio Account Info"]</h4>
<div class="mb-3" asp-validation-class-for="PhoneNumber">
<label asp-for="PhoneNumber">@T["Phone Number"]</label>
Expand All @@ -28,24 +28,3 @@
}
</div>
</div>

<script at="Foot">
document.addEventListener('DOMContentLoaded', () => {
let menu = document.getElementById('@Html.IdFor(m => m.DefaultProvider)');
let settingContainers = document.getElementsByClassName('sms-settings-container');
menu.addEventListener('change', (e) => {
for (let i = 0; i < settingContainers.length; i++) {
settingContainers[i].classList.add('d-none');
}
let wrapper = document.getElementById('SmsSettingsContainer' + e.target.value);
if (wrapper) {
wrapper.classList.remove('d-none');
}
});
});
</script>
2 changes: 2 additions & 0 deletions src/OrchardCore/OrchardCore.Sms.Core/SmsConstants.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,6 @@ public class SmsConstants
public const string TwilioServiceName = "Twilio";

public const string ConsoleServiceName = "Console";

public const string SettingsGroupId = "sms";
}

0 comments on commit 2349676

Please sign in to comment.