|
| 1 | +@page "/admin/product" |
| 2 | +@page "/admin/product/{id:int}" |
| 3 | +@inject IProductService ProductService |
| 4 | +@inject IProductTypeService ProductTypeService |
| 5 | +@inject ICategoryService CategoryService |
| 6 | +@inject NavigationManager NavigationManager |
| 7 | +@inject IJSRuntime JSRuntime |
| 8 | + |
| 9 | +@if (loading) |
| 10 | +{ |
| 11 | + <span>@msg</span> |
| 12 | +} |
| 13 | +else |
| 14 | +{ |
| 15 | + @if (product.Editing) |
| 16 | + { |
| 17 | + <h3>Edit "@product.Title"</h3> |
| 18 | + } |
| 19 | + else if (product.IsNew) |
| 20 | + { |
| 21 | + <h3>Create a new Product</h3> |
| 22 | + } |
| 23 | + |
| 24 | + <EditForm Model="product" OnValidSubmit="AddOrUpdateProduct"> |
| 25 | + <DataAnnotationsValidator></DataAnnotationsValidator> |
| 26 | + <div class="mb-0"> |
| 27 | + <label for="title">Title</label> |
| 28 | + <InputText id="title" @bind-Value="product.Title" class="form-control"></InputText> |
| 29 | + </div> |
| 30 | + <div class="mb-0"> |
| 31 | + <label for="imageUrl">Image Url</label> |
| 32 | + <InputText id="imageUrl" @bind-Value="product.ImageUrl" class="form-control"></InputText> |
| 33 | + </div> |
| 34 | + <div class="mb-0"> |
| 35 | + <img src="@product.ImageUrl" /> |
| 36 | + </div> |
| 37 | + <div class="mb-0"> |
| 38 | + <label for="description">Description</label> |
| 39 | + <InputTextArea id="description" @bind-Value="product.Description" class="form-control"></InputTextArea> |
| 40 | + </div> |
| 41 | + <hr /> |
| 42 | + <div class="header"> |
| 43 | + <div class="col">Product Type / Variant</div> |
| 44 | + <div class="col">Price</div> |
| 45 | + <div class="col">Original Price</div> |
| 46 | + <div class="col">Visible</div> |
| 47 | + <div class="col"></div> |
| 48 | + </div> |
| 49 | + @foreach (var variant in product.Variants) |
| 50 | + { |
| 51 | + <div class="row"> |
| 52 | + <div class="col"> |
| 53 | + <InputSelect disabled="@variant.Deleted" @bind-Value="variant.ProductTypeId" class="form-control"> |
| 54 | + @foreach (var productType in ProductTypeService.ProductTypes) |
| 55 | + { |
| 56 | + <option value="@productType.Id.ToString()">@productType.Name</option> |
| 57 | + } |
| 58 | + </InputSelect> |
| 59 | + </div> |
| 60 | + <div class="col"> |
| 61 | + <InputNumber @bind-Value="variant.Price" class="form-control" disabled="@variant.Deleted"></InputNumber> |
| 62 | + </div> |
| 63 | + <div class="col"> |
| 64 | + <InputNumber @bind-Value="variant.OriginalPrice" class="form-control" disabled="@variant.Deleted"></InputNumber> |
| 65 | + </div> |
| 66 | + <div class="col col-visible"> |
| 67 | + <InputCheckbox @bind-Value="variant.Visible" style="transform:scale(1.5,1.5);" disabled="@variant.Deleted"></InputCheckbox> |
| 68 | + </div> |
| 69 | + <div class="col"> |
| 70 | + <button type="button" class="btn btn-primary" disabled="@variant.Deleted" @onclick="@(() => RemoveVariant(variant.ProductTypeId))"> |
| 71 | + <i class="oi oi-trash"></i> |
| 72 | + </button> |
| 73 | + </div> |
| 74 | + </div> |
| 75 | + } |
| 76 | + <button type="button" class="btn btn-primary" @onclick="AddVariant"> |
| 77 | + <i class="oi oi-plus"></i> Add Variant |
| 78 | + </button> |
| 79 | + <hr /> |
| 80 | + <div class="mb-0"> |
| 81 | + <label for="category">Category</label> |
| 82 | + <InputSelect id="category" @bind-Value="product.CategoryId" class="form-control"> |
| 83 | + @foreach (var category in CategoryService.AdminCategories) |
| 84 | + { |
| 85 | + <option value="@category.Id">@category.Name</option> |
| 86 | + } |
| 87 | + </InputSelect> |
| 88 | + </div> |
| 89 | + <hr /> |
| 90 | + <div class="form-check"> |
| 91 | + <InputCheckbox id="featured" @bind-Value="product.Featured" class="form-check-input"></InputCheckbox> |
| 92 | + <label for="featured" class="form-check-label">Featured</label> |
| 93 | + </div> |
| 94 | + <div class="form-check"> |
| 95 | + <InputCheckbox id="visible" @bind-Value="product.Visible" class="form-check-input"></InputCheckbox> |
| 96 | + <label for="visible" class="form-check-label">Visible</label> |
| 97 | + </div> |
| 98 | + <hr /> |
| 99 | + <button type="submit" class="btn btn-primary float-end">@btnText</button> |
| 100 | + <ValidationSummary></ValidationSummary> |
| 101 | +</EditForm> |
| 102 | + @if (!product.IsNew) |
| 103 | + { |
| 104 | + <button type="button" class="btn btn-danger float-start" @onclick="DeleteProduct"> |
| 105 | + Delete Product |
| 106 | + </button> |
| 107 | + } |
| 108 | +} |
| 109 | + |
| 110 | +@code { |
| 111 | + [Parameter] |
| 112 | + public int Id { get; set; } |
| 113 | + |
| 114 | + Product product = new Product(); |
| 115 | + bool loading = true; |
| 116 | + string btnText = ""; |
| 117 | + string msg = "Loading..."; |
| 118 | + |
| 119 | + protected override async Task OnInitializedAsync() |
| 120 | + { |
| 121 | + await ProductTypeService.GetProductTypes(); |
| 122 | + await CategoryService.GetAdminCategories(); |
| 123 | + } |
| 124 | + |
| 125 | + protected override async Task OnParametersSetAsync() |
| 126 | + { |
| 127 | + if (Id == 0) |
| 128 | + { |
| 129 | + product = new Product { IsNew = true }; |
| 130 | + btnText = "Create Product"; |
| 131 | + } |
| 132 | + else |
| 133 | + { |
| 134 | + Product dbProduct = (await ProductService.GetProduct(Id)).Data; |
| 135 | + if (dbProduct == null) |
| 136 | + { |
| 137 | + msg = $"Product with Id '{Id}' does not exist!"; |
| 138 | + return; |
| 139 | + } |
| 140 | + product = dbProduct; |
| 141 | + product.Editing = true; |
| 142 | + btnText = "Update Product"; |
| 143 | + } |
| 144 | + loading = false; |
| 145 | + } |
| 146 | + |
| 147 | + void RemoveVariant(int productTypeId) |
| 148 | + { |
| 149 | + var variant = product.Variants.Find(v => v.ProductTypeId == productTypeId); |
| 150 | + if (variant == null) |
| 151 | + { |
| 152 | + return; |
| 153 | + } |
| 154 | + if (variant.IsNew) |
| 155 | + { |
| 156 | + product.Variants.Remove(variant); |
| 157 | + } |
| 158 | + else |
| 159 | + { |
| 160 | + variant.Deleted = true; |
| 161 | + } |
| 162 | + } |
| 163 | + |
| 164 | + void AddVariant() |
| 165 | + { |
| 166 | + product.Variants |
| 167 | + .Add(new ProductVariant { IsNew = true, ProductId = product.Id }); |
| 168 | + } |
| 169 | + |
| 170 | + async void AddOrUpdateProduct() |
| 171 | + { |
| 172 | + if (product.IsNew) |
| 173 | + { |
| 174 | + var result = await ProductService.CreateProduct(product); |
| 175 | + NavigationManager.NavigateTo($"admin/product/{result.Id}"); |
| 176 | + } |
| 177 | + else |
| 178 | + { |
| 179 | + product.IsNew = false; |
| 180 | + product = await ProductService.UpdateProduct(product); |
| 181 | + NavigationManager.NavigateTo($"admin/product/{product.Id}", true); |
| 182 | + } |
| 183 | + } |
| 184 | + |
| 185 | + async void DeleteProduct() |
| 186 | + { |
| 187 | + bool confirmed = await JSRuntime.InvokeAsync<bool>("confirm", |
| 188 | + $"Do you really want to delete '{product.Title}'?"); |
| 189 | + if (confirmed) |
| 190 | + { |
| 191 | + await ProductService.DeleteProduct(product); |
| 192 | + NavigationManager.NavigateTo("admin/products"); |
| 193 | + } |
| 194 | + } |
| 195 | +} |
0 commit comments