Début du projet !

javascript_error
Thomas Chazot 2 years ago
parent b1cc8d178c
commit 7d41d42d51

@ -1,5 +1,4 @@
 <CascadingValue Value="@this">
<CascadingValue Value="@this">
<div class="container"> <div class="container">
<div class="row"> <div class="row">
<div class="col-6"> <div class="col-6">
@ -49,6 +48,4 @@
</div> </div>
</div> </div>
</div> </div>
</CascadingValue> </CascadingValue>

@ -1,20 +1,16 @@
<script> window.Crafting =
{
AddActions: function (data) {
window.Crafting = data.forEach(element => {
{ var div = document.createElement('div');
AddActions: function (data) { div.innerHTML = 'Action: ' + element.action + ' - Index: ' + element.index;
data.forEach(element => { if (element.item) {
var div = document.createElement('div'); div.innerHTML += ' - Item Name: ' + element.item.name;
div.innerHTML = 'Action: ' + element.action + ' - Index: ' + element.index; }
if (element.item) { document.getElementById('actions').appendChild(div);
div.innerHTML += ' - Item Name: ' + element.item.name; });
}
document.getElementById('actions').appendChild(div);
});
}
} }
}
</script>

@ -1,10 +1,10 @@
using System; using BlazorApp.Components;
using BlazorApp.Models; using BlazorApp.Models;
using Blazorise;
using Microsoft.AspNetCore.Components; using Microsoft.AspNetCore.Components;
namespace BlazorApp.Components namespace BlazorApp.Components
{ {
public partial class CraftingItem public partial class CraftingItem
{ {
[Parameter] [Parameter]
@ -62,7 +62,4 @@ namespace BlazorApp.Components
Parent.Actions.Add(new CraftingAction { Action = "Drag Start", Item = this.Item, Index = this.Index }); Parent.Actions.Add(new CraftingAction { Action = "Drag Start", Item = this.Item, Index = this.Index });
} }
} }
} }

@ -33,7 +33,8 @@ namespace BlazorApp.Factories
EnchantCategories = model.EnchantCategories, EnchantCategories = model.EnchantCategories,
MaxDurability = model.MaxDurability, MaxDurability = model.MaxDurability,
StackSize = model.StackSize, StackSize = model.StackSize,
CreatedDate = DateTime.Now CreatedDate = DateTime.Now,
ImageBase64 = Convert.ToBase64String(model.ImageContent)
}; };
} }
@ -46,6 +47,7 @@ namespace BlazorApp.Factories
item.MaxDurability = model.MaxDurability; item.MaxDurability = model.MaxDurability;
item.StackSize = model.StackSize; item.StackSize = model.StackSize;
item.UpdatedDate = DateTime.Now; item.UpdatedDate = DateTime.Now;
item.ImageBase64 = Convert.ToBase64String(model.ImageContent);
} }
} }

@ -55,14 +55,8 @@
<p> <p>
<label> <label>
Current Item image: Current Item image:
@if (File.Exists($"{WebHostEnvironment.WebRootPath}/images/{itemModel.Name}.png")) <img src="data:image/png;base64, @(itemModel.ImageBase64)" class="img-thumbnail" title="@itemModel.DisplayName" alt="@itemModel.DisplayName" style="min-width: 50px; max-width: 150px" />
{
<img src="images/@(itemModel.Name).png" class="img-thumbnail" title="@itemModel.DisplayName" alt="@itemModel.DisplayName" style="max-width: 150px" />
}
else
{
<img src="images/default.png" class="img-thumbnail" title="@itemModel.DisplayName" alt="@itemModel.DisplayName" style="max-width: 150px" />
}
</label> </label>
</p> </p>
<p> <p>

@ -46,11 +46,6 @@ namespace BlazorApp.Pages
var fileContent = await File.ReadAllBytesAsync($"{WebHostEnvironment.WebRootPath}/images/default.png"); var fileContent = await File.ReadAllBytesAsync($"{WebHostEnvironment.WebRootPath}/images/default.png");
if (File.Exists($"{WebHostEnvironment.WebRootPath}/images/{itemModel.Name}.png"))
{
fileContent = await File.ReadAllBytesAsync($"{WebHostEnvironment.WebRootPath}/images/{item.Name}.png");
}
// Set the model with the item // Set the model with the item
itemModel = ItemFactory.ToModel(item, fileContent); itemModel = ItemFactory.ToModel(item, fileContent);
} }

@ -4,7 +4,7 @@
<PageTitle>Index</PageTitle> <PageTitle>Index</PageTitle>
<h1>Hello, world!</h1> <h1>Bonjour, monde!</h1>
<!-- <!--
<ShowItems Items="Cakes"> <ShowItems Items="Cakes">

@ -8,37 +8,6 @@ namespace BlazorApp.Pages
{ {
public partial class Index public partial class Index
{ {
/*
public List<Cake> Cakes { get; set; }
protected override Task OnAfterRenderAsync(bool firstRender)
{
LoadCakes();
StateHasChanged();
return base.OnAfterRenderAsync(firstRender);
}
public void LoadCakes()
{
Cakes = new List<Cake>
{
// items hidden for display purpose
new Cake
{
Id = 1,
Name = "Red Velvet",
Cost = 60
},
new Cake
{
Id = 2,
Name = "Black Forest",
Cost = 50
},
};
}
*/
[Inject] [Inject]
public IDataService DataService { get; set; } public IDataService DataService { get; set; }
@ -46,12 +15,19 @@ namespace BlazorApp.Pages
private List<CraftingRecipe> Recipes { get; set; } = new List<CraftingRecipe>(); private List<CraftingRecipe> Recipes { get; set; } = new List<CraftingRecipe>();
protected override async Task OnInitializedAsync() protected override async Task OnAfterRenderAsync(bool firstRender)
{ {
await base.OnInitializedAsync(); base.OnAfterRenderAsync(firstRender);
if (!firstRender)
{
return;
}
Items = await DataService.List(0, await DataService.Count()); Items = await DataService.List(0, await DataService.Count());
Recipes = await DataService.GetRecipes(); Recipes = await DataService.GetRecipes();
StateHasChanged();
} }
} }
} }

@ -0,0 +1,37 @@
@page "/inventory"
@using BlazorApp.Components;
<h3>Inventory</h3>
<div class="css-grid">
<CraftingItem Index="0" />
<CraftingItem Index="1" />
<CraftingItem Index="2" />
<CraftingItem Index="3" />
<CraftingItem Index="4" />
<CraftingItem Index="5" />
<CraftingItem Index="6" />
<CraftingItem Index="7" />
<CraftingItem Index="8" />
<CraftingItem Index="9" />
<CraftingItem Index="10" />
<CraftingItem Index="11" />
<CraftingItem Index="12" />
<CraftingItem Index="13" />
<CraftingItem Index="14" />
<CraftingItem Index="15" />
<CraftingItem Index="16" />
<CraftingItem Index="17" />
<!--
<CraftingItem Index="18" />
<CraftingItem Index="19" />
<CraftingItem Index="20" />
<CraftingItem Index="21" />
<CraftingItem Index="22" />
<CraftingItem Index="23" />
<CraftingItem Index="24" />
<CraftingItem Index="25" />
<CraftingItem Index="26" />
-->
</div>

@ -0,0 +1,11 @@
using System;
using BlazorApp.Components;
namespace BlazorApp.Pages
{
public partial class Inventory
{
}
}

@ -0,0 +1,6 @@
.css-grid {
grid-template-columns: repeat(6,minmax(0,1fr));
gap: 10px;
display: grid;
width: 40%;
}

@ -53,9 +53,9 @@
<DataGridColumn TItem="Item" Field="@nameof(Item.Id)" Caption="#" /> <DataGridColumn TItem="Item" Field="@nameof(Item.Id)" Caption="#" />
<DataGridColumn TItem="Item" Field="@nameof(Item.Id)" Caption="Image"> <DataGridColumn TItem="Item" Field="@nameof(Item.Id)" Caption="Image">
<DisplayTemplate> <DisplayTemplate>
@if (File.Exists($"{WebHostEnvironment.WebRootPath}/images/{context.Name}.png")) @if (!string.IsNullOrWhiteSpace(context.ImageBase64))
{ {
<img src="images/@(context.Name).png" class="img-thumbnail" title="@context.DisplayName" alt="@context.DisplayName" style="max-width: 150px" /> <img src="data:image/png;base64, @(context.ImageBase64)" class="img-thumbnail" title="@context.DisplayName" alt="@context.DisplayName" style="min-width: 50px; max-width: 150px" />
} }
else else
{ {

@ -25,7 +25,7 @@ builder.Services
builder.Services.AddBlazoredLocalStorage(); builder.Services.AddBlazoredLocalStorage();
builder.Services.AddScoped<IDataService, DataLocalService>(); builder.Services.AddScoped<IDataService, DataApiService>();
builder.Services.AddBlazoredModal(); builder.Services.AddBlazoredModal();

@ -0,0 +1,61 @@
using System;
using BlazorApp.Components;
using BlazorApp.Factories;
using BlazorApp.Models;
namespace BlazorApp.Services
{
public class DataApiService : IDataService
{
private readonly HttpClient _http;
public DataApiService(
HttpClient http)
{
_http = http;
}
public async Task Add(ItemModel model)
{
// Get the item
var item = ItemFactory.Create(model);
// Save the data
await _http.PostAsJsonAsync("https://codefirst.iut.uca.fr/containers/container-blazor-web-api-julienriboulet/api/Crafting/", item);
}
public async Task<int> Count()
{
return await _http.GetFromJsonAsync<int>("https://codefirst.iut.uca.fr/containers/container-blazor-web-api-julienriboulet/api/Crafting/count");
}
public async Task<List<Item>> List(int currentPage, int pageSize)
{
return await _http.GetFromJsonAsync<List<Item>>($"https://codefirst.iut.uca.fr/containers/container-blazor-web-api-julienriboulet/api/Crafting/?currentPage={currentPage}&pageSize={pageSize}");
}
public async Task<Item> GetById(int id)
{
return await _http.GetFromJsonAsync<Item>($"https://codefirst.iut.uca.fr/containers/container-blazor-web-api-julienriboulet/api/Crafting/{id}");
}
public async Task Update(int id, ItemModel model)
{
// Get the item
var item = ItemFactory.Create(model);
await _http.PutAsJsonAsync($"https://codefirst.iut.uca.fr/containers/container-blazor-web-api-julienriboulet/api/Crafting/{id}", item);
}
public async Task Delete(int id)
{
await _http.DeleteAsync($"https://codefirst.iut.uca.fr/containers/container-blazor-web-api-julienriboulet/Crafting/{id}");
}
public async Task<List<CraftingRecipe>> GetRecipes()
{
return await _http.GetFromJsonAsync<List<CraftingRecipe>>("https://codefirst.iut.uca.fr/containers/container-blazor-web-api-julienriboulet/api/Crafting/recipe");
}
}
}

@ -14,6 +14,11 @@
<span class="oi oi-list-rich" aria-hidden="true"></span> List <span class="oi oi-list-rich" aria-hidden="true"></span> List
</NavLink> </NavLink>
</div> </div>
<div class="nav-item px-3">
<NavLink class="nav-link" href="inventory">
<span class="oi oi-briefcase" aria-hidden="true"></span> Inventory
</NavLink>
</div>
<div class="nav-item px-3"> <div class="nav-item px-3">
<NavLink class="nav-link" href="" Match="NavLinkMatch.All"> <NavLink class="nav-link" href="" Match="NavLinkMatch.All">
<span class="oi oi-home" aria-hidden="true"></span> Home <span class="oi oi-home" aria-hidden="true"></span> Home

Loading…
Cancel
Save