Tucker Chase (CSC FI SPS MESLEO) 123bbdb9fe Merged PR 34240: Updates to PCR3 document section
Updates to PCR3 document section
2025-01-15 16:52:17 +01:00

159 lines
5.8 KiB
Plaintext

@inject MesaFabApprovalAuthStateProvider authStateProvider
@inject NavigationManager navigationManager
@inject IPCRBService pcrbService
@inject IUserService userService
@inject IECNService ecnService
@inject ISnackbar snackbar
<MudDialog>
<DialogContent>
<MudPaper Class="m-2 p-2">
<MudForm @bind-Errors="@errors">
<MudTextField T="string"
Label="Document Type"
@bind-Value="@document.DocType"
@bind-Text="@document.DocType"
Disabled
AutoGrow />
<MudTextField Label="Document Numbers - Rev. & Title"
@bind-Value="@document.DocNumbers"
@bind-Text="@document.DocNumbers"
Immediate
Required
AutoGrow
AutoFocus />
@if (DocNumberIsNA()) {
<MudTextField Label="Comments"
@bind-Value="@document.Comment"
@bind-Text="@document.Comment"
Required
RequiredError="You must provide a comment"
Immediate
AutoGrow />
} else {
<MudTextField @bind-Value="@document.ECNNumber"
Required
RequiredError="You must provide a valid ECN#"
Clearable
Variant="Variant.Outlined"
InputType="@InputType.Number"
Validation="@(new Func<int, Task<string>>(ECNNoIsValid))"
Label="ECN#"
Immediate
AutoGrow />
}
<MudCheckBox Label="Complete"
Color="Color.Tertiary"
@bind-Value=complete
LabelPosition="LabelPosition.Start" />
</MudForm>
</MudPaper>
</DialogContent>
<DialogActions>
@if (!string.IsNullOrWhiteSpace(document.DocNumbers) && ((DocNumberIsNA() && !string.IsNullOrWhiteSpace(document.Comment)) ||
(!DocNumberIsNA() && ecnNoIsValid))) {
<MudButton Variant="Variant.Filled"
Color="Color.Tertiary"
Class="m1-auto"
OnClick=Save>
@if (saveInProcess) {
<MudProgressCircular Class="m-1" Size="Size.Small" Indeterminate="true" />
<MudText>Processing</MudText>
} else {
<MudText>Save</MudText>
}
</MudButton>
}
<MudButton Variant="Variant.Filled"
Class="grey text-black m1-auto"
OnClick=Cancel>
Cancel
</MudButton>
</DialogActions>
</MudDialog>
@code {
[CascadingParameter]
MudDialogInstance MudDialog { get; set; }
[Parameter]
public required PCR3Document document { get; set; }
private string[] errors = { };
private bool complete = false;
private bool saveInProcess = false;
private bool ecnNoIsValid = true;
protected override async Task OnParametersSetAsync() {
complete = document.CompletedByID > 0;
}
private async Task Save() {
saveInProcess = true;
try {
if (authStateProvider.CurrentUser is null) {
await authStateProvider.Logout();
navigationManager.NavigateTo("login");
return;
}
if (!complete) {
document.CompletedByID = 0;
document.CompletedBy = null;
document.CompletedDate = DateTimeUtilities.MAX_DT;
}
if (complete && document.CompletedByID <= 0) {
document.CompletedByID = authStateProvider.CurrentUser.UserID;
document.CompletedBy = authStateProvider.CurrentUser;
document.CompletedDate = DateTime.Now;
}
if (string.IsNullOrWhiteSpace(document.DocNumbers)) {
throw new Exception("Document Numbers cannot be empty");
}
if (!DocNumberIsNA() && !ecnNoIsValid)
throw new Exception($"{document.ECNNumber} is not a valid ECN#");
if (DocNumberIsNA() && string.IsNullOrWhiteSpace(document.Comment))
throw new Exception("you must provide a comment");
await pcrbService.UpdatePCR3Document(document);
await pcrbService.GetPCR3DocumentsForPlanNumber(document.PlanNumber, true);
saveInProcess = false;
MudDialog.Close(DialogResult.Ok(document));
} catch (Exception ex) {
saveInProcess = false;
snackbar.Add($"Unable to save document, because {ex.Message}", Severity.Error);
}
}
private void Cancel() {
MudDialog.Close(DialogResult.Cancel());
}
private bool DocNumberIsNA() {
if (document.DocNumbers.ToLower().Equals("na") ||
document.DocNumbers.ToLower().Equals("n/a") ||
document.DocNumbers.ToLower().Equals("n a") ||
document.DocNumbers.ToLower().Equals("not applicable")) {
return true;
}
return false;
}
private async Task<string> ECNNoIsValid(int ecnNumber) {
string? result = await ecnService.ECNNumberIsValidStr(ecnNumber);
if (result is null) ecnNoIsValid = true;
else ecnNoIsValid = false;
StateHasChanged();
return result;
}
}