@page "/CreateRecipe"
@using Microsoft.AspNetCore.Authorization;
@using Microsoft.AspNetCore.Components.WebAssembly.Authentication;
@using MyRecipes2.Shared;
@using System;
@using System.IO;
@using System.Text;
@inject HttpClient Http;
@inject AuthenticationStateProvider AuthenticationStateProvider;
@inject NavigationManager NavManager;
<div class="col-md-12 col-lg-8">
<!-- Recipe Name -->
<div style="margin-bottom: 1rem; margin-top: 1rem;">
<h2>Recipe Name</h2>
</div>
<div class="border-bottom p-1" style="margin-bottom: 1rem;">
<div class="form-group row">
<div class="input-group col-12">
<input @bind="Name" class="form-control" placeholder="Name">
</div>
<span class="text-danger">
@NameErrorText
</span>
</div>
</div>
<!-- Upload image input-->
<div style="margin-bottom: 1rem; margin-top: 1rem;">
<h2>Upload a picture of this recipe</h2>
</div>
<ImageEditor @ref="imageEditor"></ImageEditor>
<!-- Ingredients -->
<div style="margin-bottom: 1rem; margin-top: 1rem;">
<h2>Ingredients</h2>
</div>
<IngredientEditor @ref="ingredientEditor"></IngredientEditor>
<!-- Method -->
<div style="margin-bottom: 1rem;">
<h2>Methods</h2>
</div>
<MethodEditor @ref="methodEditor"></MethodEditor>
<div style="margin-bottom: 1rem;">
<h2>Meal Types</h2>
</div>
<MealTypeEditor @ref="mealTypeEditor"></MealTypeEditor>
<div style="text-align:center;">
<button class="btn btn-success mt-2 mb-2" @onclick="ClickCreate">Create</button>
</div>
</div>
@code {
ImageEditor imageEditor { get; set; }
IngredientEditor ingredientEditor { get; set; }
MethodEditor methodEditor { get; set; }
MealTypeEditor mealTypeEditor { get; set; }
private string Name { get; set; }
private bool IsEditMode { get; set; }
private string NameErrorText { get; set; }
protected bool IsValid()
{
bool result = true;
if (string.IsNullOrWhiteSpace(Name))
{
NameErrorText = "Please enter a name";
result = false;
}
return result;
}
protected async void ClickCreate()
{
if (!IsValid()) return;
AuthenticationState authState = await AuthenticationStateProvider.GetAuthenticationStateAsync();
var user = authState.User;
if (user.Identity.IsAuthenticated)
{
string userId = user.FindFirst(c => c.Type == "sub")?.Value;
CreateRecipeRequestDto request = new CreateRecipeRequestDto();
request.Name = Name;
request.CreatedById = userId;
request.Method = methodEditor.MethodSteps;
request.Ingredients = ingredientEditor.Ingredients;
request.ImageId = imageEditor.ImageId;
request.MealTypes = mealTypeEditor.SelectedMealTypes;
HttpResponseMessage message = await Http.PostAsJsonAsync<CreateRecipeRequestDto>("Recipe", request);
int recipeId = await message.Content.ReadFromJsonAsync<int>();
NavManager.NavigateTo($"ViewRecipe/{recipeId}");
}
}
protected void ClickEdit()
{
IsEditMode = true;
}
}