|
| 1 | +@page "/contracts/new" |
| 2 | +@rendermode InteractiveServer |
| 3 | +@using System.ComponentModel.DataAnnotations |
| 4 | +@inject IMediator Mediator |
| 5 | +@inject NavigationManager NavigationManager |
| 6 | + |
| 7 | +<PageTitle>Create New Contract</PageTitle> |
| 8 | + |
| 9 | +<div class="space-y-6"> |
| 10 | + <!-- Page Header --> |
| 11 | + <div class="flex items-center justify-between"> |
| 12 | + <div> |
| 13 | + <h1 class="text-2xl font-bold text-gray-900">Create New Contract</h1> |
| 14 | + <p class="text-gray-600 mt-1">Add a new contract to the estate</p> |
| 15 | + </div> |
| 16 | + <button class="btn btn-secondary" @onclick="Cancel"> |
| 17 | + Cancel |
| 18 | + </button> |
| 19 | + </div> |
| 20 | + |
| 21 | + @if (!string.IsNullOrWhiteSpace(errorMessage)) |
| 22 | + { |
| 23 | + <div class="bg-red-50 border border-red-200 text-red-700 px-4 py-3 rounded-lg"> |
| 24 | + <p class="font-medium">Error</p> |
| 25 | + <p class="text-sm">@errorMessage</p> |
| 26 | + </div> |
| 27 | + } |
| 28 | + |
| 29 | + <!-- Form --> |
| 30 | + <div class="bg-white rounded-lg shadow-md p-6"> |
| 31 | + <EditForm Model="@model" OnValidSubmit="@HandleSubmit"> |
| 32 | + <DataAnnotationsValidator /> |
| 33 | + |
| 34 | + <!-- Contract Details Section --> |
| 35 | + <div class="mb-6"> |
| 36 | + <h3 class="text-lg font-semibold text-gray-900 mb-4">Contract Details</h3> |
| 37 | + <div class="space-y-4"> |
| 38 | + <div> |
| 39 | + <label class="block text-sm font-medium text-gray-700 mb-1"> |
| 40 | + Description <span class="text-red-500">*</span> |
| 41 | + </label> |
| 42 | + <InputText @bind-Value="model.Description" class="input w-full" placeholder="Enter contract description" /> |
| 43 | + <ValidationMessage For="@(() => model.Description)" class="text-red-600 text-sm mt-1" /> |
| 44 | + </div> |
| 45 | + |
| 46 | + <div> |
| 47 | + <label class="block text-sm font-medium text-gray-700 mb-1"> |
| 48 | + Operator <span class="text-red-500">*</span> |
| 49 | + </label> |
| 50 | + @if (isLoadingOperators) |
| 51 | + { |
| 52 | + <div class="text-sm text-gray-500">Loading operators...</div> |
| 53 | + } |
| 54 | + else if (operators != null && operators.Any()) |
| 55 | + { |
| 56 | + <InputSelect @bind-Value="model.OperatorId" class="input w-full"> |
| 57 | + <option value="">-- Select Operator --</option> |
| 58 | + @foreach (var op in operators) |
| 59 | + { |
| 60 | + <option value="@op.OperatorId">@op.Name</option> |
| 61 | + } |
| 62 | + </InputSelect> |
| 63 | + <ValidationMessage For="@(() => model.OperatorId)" class="text-red-600 text-sm mt-1" /> |
| 64 | + } |
| 65 | + else |
| 66 | + { |
| 67 | + <p class="text-sm text-red-600">No operators available. Please create an operator first.</p> |
| 68 | + } |
| 69 | + </div> |
| 70 | + </div> |
| 71 | + </div> |
| 72 | + |
| 73 | + <!-- Submit Button --> |
| 74 | + <div class="flex justify-end space-x-4 pt-4 border-t"> |
| 75 | + <button type="button" class="btn btn-secondary" @onclick="Cancel" disabled="@isSaving"> |
| 76 | + Cancel |
| 77 | + </button> |
| 78 | + <button type="submit" class="btn btn-primary" disabled="@isSaving"> |
| 79 | + @if (isSaving) |
| 80 | + { |
| 81 | + <span class="inline-block animate-spin rounded-full h-4 w-4 border-b-2 border-white mr-2"></span> |
| 82 | + <span>Saving...</span> |
| 83 | + } |
| 84 | + else |
| 85 | + { |
| 86 | + <span>Create Contract</span> |
| 87 | + } |
| 88 | + </button> |
| 89 | + </div> |
| 90 | + </EditForm> |
| 91 | + </div> |
| 92 | +</div> |
| 93 | + |
| 94 | +@code { |
| 95 | + private CreateContractFormModel model = new(); |
| 96 | + private bool isSaving = false; |
| 97 | + private bool isLoadingOperators = true; |
| 98 | + private string? errorMessage; |
| 99 | + private List<OperatorModel>? operators; |
| 100 | + |
| 101 | + protected override async Task OnInitializedAsync() |
| 102 | + { |
| 103 | + await LoadOperators(); |
| 104 | + } |
| 105 | + |
| 106 | + private async Task LoadOperators() |
| 107 | + { |
| 108 | + try |
| 109 | + { |
| 110 | + isLoadingOperators = true; |
| 111 | + var result = await Mediator.Send(new Queries.GetOperatorsQuery(CorrelationIdHelper.New(), "stubbed-token", Guid.Parse("11111111-1111-1111-1111-111111111111"))); |
| 112 | + if (result.IsSuccess) |
| 113 | + { |
| 114 | + operators = result.Data; |
| 115 | + } |
| 116 | + } |
| 117 | + finally |
| 118 | + { |
| 119 | + isLoadingOperators = false; |
| 120 | + } |
| 121 | + } |
| 122 | + |
| 123 | + private async Task HandleSubmit() |
| 124 | + { |
| 125 | + isSaving = true; |
| 126 | + errorMessage = null; |
| 127 | + |
| 128 | + try |
| 129 | + { |
| 130 | + var estateId = Guid.Parse("11111111-1111-1111-1111-111111111111"); |
| 131 | + var accessToken = "stubbed-token"; |
| 132 | + |
| 133 | + // Create contract |
| 134 | + var createCommand = new Commands.CreateContractCommand( |
| 135 | + CorrelationIdHelper.New(), |
| 136 | + accessToken, |
| 137 | + estateId, |
| 138 | + model.Description!, |
| 139 | + Guid.Parse(model.OperatorId!) |
| 140 | + ); |
| 141 | + |
| 142 | + var createResult = await Mediator.Send(createCommand); |
| 143 | + |
| 144 | + if (!createResult.IsSuccess) |
| 145 | + { |
| 146 | + errorMessage = createResult.Message ?? "Failed to create contract"; |
| 147 | + return; |
| 148 | + } |
| 149 | + |
| 150 | + // Navigate to contracts list with success |
| 151 | + NavigationManager.NavigateTo("/contracts"); |
| 152 | + } |
| 153 | + catch (Exception ex) |
| 154 | + { |
| 155 | + errorMessage = $"An error occurred: {ex.Message}"; |
| 156 | + } |
| 157 | + finally |
| 158 | + { |
| 159 | + isSaving = false; |
| 160 | + } |
| 161 | + } |
| 162 | + |
| 163 | + private void Cancel() |
| 164 | + { |
| 165 | + NavigationManager.NavigateTo("/contracts"); |
| 166 | + } |
| 167 | + |
| 168 | + public class CreateContractFormModel |
| 169 | + { |
| 170 | + [Required(ErrorMessage = "Description is required")] |
| 171 | + public string? Description { get; set; } |
| 172 | + |
| 173 | + [Required(ErrorMessage = "Operator is required")] |
| 174 | + public string? OperatorId { get; set; } |
| 175 | + } |
| 176 | +} |
0 commit comments