-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #686 from cabinetoffice/feature/radio-buttons-json…
…-data Introducing JsonValue field and extending ChoiceProviderStrategies to allow for json values in radio button options
- Loading branch information
Showing
23 changed files
with
1,988 additions
and
67 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
...tend/CO.CDP.OrganisationApp/Pages/Forms/ChoiceProviderStrategies/ChoiceProviderService.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
22 changes: 19 additions & 3 deletions
22
...CDP.OrganisationApp/Pages/Forms/ChoiceProviderStrategies/DefaultChoiceProviderStrategy.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,26 @@ | ||
namespace CO.CDP.OrganisationApp.Pages.Forms.ChoiceProviderStrategies; | ||
using CO.CDP.Forms.WebApiClient; | ||
namespace CO.CDP.OrganisationApp.Pages.Forms.ChoiceProviderStrategies; | ||
|
||
public class DefaultChoiceProviderStrategy() : IChoiceProviderStrategy | ||
{ | ||
public async Task<List<string>?> Execute(FormQuestionOptions options) | ||
public string AnswerFieldName { get; } = "OptionValue"; | ||
public async Task<Dictionary<string, string>?> Execute(FormQuestionOptions options) | ||
{ | ||
return await Task.FromResult(options.Choices.ToDictionary(c => c.Title, c => c.Title)); | ||
} | ||
|
||
public async Task<string?> RenderOption(CO.CDP.Forms.WebApiClient.FormAnswer? answer) | ||
{ | ||
return await RenderOption(answer?.OptionValue); | ||
} | ||
|
||
public async Task<string?> RenderOption(CO.CDP.OrganisationApp.Models.FormAnswer? answer) | ||
{ | ||
return await RenderOption(answer?.OptionValue); | ||
} | ||
|
||
private async Task<string?> RenderOption(string? optionValue) | ||
{ | ||
return await Task.FromResult(options.Choices.Select(c => c.Title).ToList()); | ||
return await Task.FromResult(optionValue); | ||
} | ||
} |
74 changes: 67 additions & 7 deletions
74
...ationApp/Pages/Forms/ChoiceProviderStrategies/ExclusionAppliesToChoiceProviderStrategy.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,24 +1,84 @@ | ||
namespace CO.CDP.OrganisationApp.Pages.Forms.ChoiceProviderStrategies; | ||
using CO.CDP.Forms.WebApiClient; | ||
using CO.CDP.Organisation.WebApiClient; | ||
using CO.CDP.Tenant.WebApiClient; | ||
using System.Text.Json; | ||
|
||
public class ExclusionAppliesToChoiceProviderStrategy(IUserInfoService userInfoService, IOrganisationClient organisationClient) : IChoiceProviderStrategy | ||
{ | ||
public async Task<List<string>?> Execute(FormQuestionOptions options) | ||
public string AnswerFieldName { get; } = "JsonValue"; | ||
|
||
public async Task<Dictionary<string, string>?> Execute(FormQuestionOptions options) | ||
{ | ||
var jsonSerializerOptions = new JsonSerializerOptions { PropertyNamingPolicy = JsonNamingPolicy.CamelCase }; | ||
|
||
var organisationId = userInfoService.GetOrganisationId(); | ||
if (organisationId != null) | ||
{ | ||
var connectedEntities = await organisationClient.GetConnectedEntitiesAsync((Guid)organisationId); | ||
List<string> returnList = [ | ||
(await organisationClient.GetOrganisationAsync((Guid)organisationId)).Name | ||
]; | ||
returnList.AddRange(connectedEntities.Select(entity => entity.Name)); | ||
var organisation = await organisationClient.GetOrganisationAsync((Guid)organisationId); | ||
|
||
var result = new Dictionary<string, string>(); | ||
|
||
result[JsonSerializer.Serialize(new { id = organisation.Id, type = "organisation" }, jsonSerializerOptions)] = organisation.Name; | ||
|
||
return returnList; | ||
foreach (var entity in connectedEntities) | ||
{ | ||
result[JsonSerializer.Serialize(new { id = entity.EntityId, type = "connected-entity" }, jsonSerializerOptions)] = entity.Name; | ||
} | ||
|
||
|
||
return result; | ||
} | ||
|
||
return null; | ||
} | ||
|
||
public async Task<string?> RenderOption(CO.CDP.Forms.WebApiClient.FormAnswer? answer) | ||
{ | ||
return await RenderOption(answer?.JsonValue); | ||
} | ||
|
||
public async Task<string?> RenderOption(CO.CDP.OrganisationApp.Models.FormAnswer? answer) | ||
{ | ||
return await RenderOption(answer?.JsonValue); | ||
} | ||
|
||
private async Task<string?> RenderOption(string? jsonValue) | ||
{ | ||
if (jsonValue != null) | ||
{ | ||
var jsonSerializerOptions = new JsonSerializerOptions | ||
{ | ||
PropertyNameCaseInsensitive = true | ||
}; | ||
|
||
ExclusionAppliesToChoiceProviderStrategyAnswer? answerValues = JsonSerializer.Deserialize<ExclusionAppliesToChoiceProviderStrategyAnswer>(jsonValue, jsonSerializerOptions); | ||
|
||
switch (answerValues?.Type) | ||
{ | ||
case "organisation": | ||
var organisation = await organisationClient.GetOrganisationAsync(answerValues.Id); | ||
return organisation.Name; | ||
|
||
case "connected-entity": | ||
var organisationId = userInfoService.GetOrganisationId(); | ||
if (organisationId != null) | ||
{ | ||
var connectedEntities = await organisationClient.GetConnectedEntitiesAsync((Guid)organisationId); | ||
var entity = connectedEntities.FirstOrDefault(e => e.EntityId == answerValues.Id); | ||
return entity?.Name; | ||
} | ||
|
||
break; | ||
} | ||
} | ||
|
||
return null; | ||
} | ||
} | ||
|
||
public class ExclusionAppliesToChoiceProviderStrategyAnswer() | ||
{ | ||
required public Guid Id { get; set; } | ||
required public string Type { get; set; } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.